Are there any interpreted languages โ€‹โ€‹in which you can dynamically modify the interpreter?

I thought about this recording (apparently) of Mark Twain , in which he begins to write in English, but throughout the text there are changes to the writing rules, so that by the end it ends with something, probably best described as pseudo-Germanic.

This made me wonder if there is an interpreter for a particular language that has access to the interpreter itself, so you can change the syntax and structure of the language as you go. For example, often the if clause is a keyword; is there a language that would allow you to change or redefine this on the fly? Imagine the beginning of a console session in one language, and towards the end, working in another.

Obviously, you can write an interpreter and run it, and perhaps there is no specific difference between this and the modification of the interpreter. I am not sure about that. Perhaps there are restrictions on modifications that you can make dynamically for any interpreter?

These more open questions aside, I just wanted to know if there are any well-known interpreters that allow this at all? Or perhaps this ability is just a matter of degree, and my question is poorly posed.

+4
source share
3 answers

Of course, there are languages โ€‹โ€‹in which such a type of self-modifying behavior at the level of the language syntax itself is possible. Lisp programs can contain macros that allow, among other things, the creation of new control structures on the fly, to the extent that two Lisp programs that depend on extensive macro programs can look almost as if they were written in two different languages. Forth is somewhat similar to the fact that the Forth interpreter provides a basic set of about a dozen or so primitive operations by which a program should be built in the language of the problem domain (often some real interaction that must be performed accurately and programmatically, for example, industrial robotics ) The Forth programmer creates an interpreter that understands a language specific to the problem that he or she is trying to solve, then writes higher-level programs in that language.

In general, the general idea here is that languages โ€‹โ€‹or systems that view code and data as equivalent and give the user the same power to change one as the other. Each Lisp program is a Lisp data structure, for example. This is different from a language such as Java, in which there is a sharp difference between the program code and the data that it manages.

The related object refers to a self-modifying low-level code , which was a fairly common method among assembler programmers during the days of minicomputers with complex instruction sets that poured a bit into the early 8-bit and 16-bit microcomputer worlds. In this programming idiom, in order to save speed or memory, a program would be written with an โ€œawarenessโ€ of the location where its compiled or interpreted instructions would be stored in memory, and could change the actual byte of machine-level instructions in byte to affect its behavior on to fly.

+5
source

Fort is the most obvious thing I can think of. It is concatenative and stack-based, with the word being the main atom. Thus, you write a stream of words, and they are executed in the order in which they are written when the stack is manipulated explicitly to pass parameters, results, etc. So a simple Forth program might look like this:

 6 3 + . 

These are the words 6 , 3 , + and . . Two numbers push their values โ€‹โ€‹onto the stack. The plus symbol pushes the last two elements from the stack, adds them and pushes the result. A full stop displays everything at the top of the stack.

The fundamental part of the Fort is that you define your own words. Since all words are first-class members of the runtime, you are actually creating application-specific grammar. Having defined the corresponding words, you can get the code:

 red circle draw 

To expand the red circle.

The fort interprets every sequence of words when it encounters them. However, he distinguishes between compilation time and ordinary words. Compilation words do things like a sequence of words compiled and stored as a new word. Thus, the equivalent of defining routines in the classical procedural language. They are also a means of implementing governance structures. But you can also define your own compilation words.

As a final result, the Forth program usually determines its entire grammar, including the corresponding control words.

You can read the basic introduction here .

+2
source

Prolog is a homo- icon language , allowing you to reject meta-widths (IM) in various ways. Meta interpreter - interpretation of the interpreter is a common and useful native construct in Prolog.

See this page for this argument. An illustration of an interesting and practical technique is a partial implementation:

The overhead associated with the implementation of these things using IM can be compiled using partial estimation methods.

+2
source

Source: https://habr.com/ru/post/1488180/


All Articles