In this tutorial we will see how to generate a new code in run-time.
ELENA does not support run-time compilation but it is possible to create group objects which will interpret their content as some kind of program. system'dynamic'Tape and system'dynamic'Struct are two array classes with custom dispatch methods to be used for such tasks.
Let's start with Tape class. It copies its content into the stack until it encounters the message or extension and send it to the top object in the stack.
Here the simple program:
The result will be:
How it works? When eval message is sent to the tape, it starts to execute its content: first it puts the literal constant into the stack, then it calls system'console symbol and saves the result into the stack as well. The next is a message reference - writeLine. At this point the tape sends the message to the object on the stack top - system'console. The message parameter counter is 1 so "Hello World" constant is a message parameter. After the operation both objects are removed from the stack.
Let's make the situation a little bit more complex - the message parameter should be printed:
The result is:
What is happening here? Before the tape starts to execute its content it puts the parameters and self variable into the stack as well. So what is in the stack before the execution is started?
Note that nil reference is in the stack bottom.
When the first message (in our case it is an extension - tapeOp.var, where  indicates an open argument list) is found, the stack looks like this:
The extension tapeOp.var is used to access the tape stack which is considered as an open argument list. The extension target - numeric constant 2 - specifies the argument index. The equivalent plain code looks like this:
So after the extension is executed our stack looks like this:
And before the final message:
Our second group object is system'dynamic'Struct. Lets' consider a simple use case:
As you may see it is equivalent to the following code:
How it works? If the incoming message verb is GET (note that x and get&x are the same), the object seeks for the equivalent subject in its body and returns the following object. Otherwise it looks for the message subject, sends generic EVAL message with incoming message parameters to the next array item and returns the result.
So we may rewrite our example like this:
Which is equivalent to the plain code:
In this tutorial we have learned how dynamically build and execute the code without actually compiling it.