Wednesday, March 29, 2017
Saturday, March 11, 2017
In this tutorial I will show how multi-methods are implemented in ELENA
To take part in parameter dispatching classes should be declared with dispatchable attribute.
To dispatch the parameter we have to send cast message to it:
This approach works quite good for a single parameter. For several parameters this approach works not so good. It is possible to resolve them using several nested closures. Currently general solution is not possible. But basic data types support limited two parameter dispatching:
The result will be:
Friday, March 10, 2017
In this short article I will show how to implement event handler using a new attribute : event
To declare a new event handler we should declare the field with event attribute:
The first parameter indicates the action type (func1 is an action with a single generic parameter), the second one - the property name to be used for attaching the handler.
To attach the handler we have to call the property with our action:
The simple example code is below:
Thursday, February 9, 2017
In this tutorial we will see how to program a simple state machine using Mixins and message dispatching.
Let's just remember that a generic message in ELENA can be qualified.
"subj" is a subject class which qualifies the generic message "eval". In the expression "o::subj" we create a temporal mixin object (so called a temporal mutation). As a result "eval" message is turned into state0'eval and is sent to the instance of MyClass.
This principle will help us to create our simple state machine.
The key element is a message dispatching expression:
This code is equivalent to our mutation expression:
As a result we create a state machine without branching operators, using only ELENA dispatching routines.
Thursday, January 5, 2017
In this article we will see that system'dynamic'Tape mixin can be used to implement Turing machine. To demonstrate this Brainf**k interpreter will be created.
In the previous tutorial it was shown that system'dynamic'Tape and system'dynamic'Struct mix-in objects can be used to execute the dynamic code without reflection and run-time compilation. This approach allows us actually to build new code even in stand-alone application. But it is quite clear that it is not very practical. Let's implement the following code:
The equivalent Tape will look like this:
The more complex code the more bulky sequence should be created. Of course we should not write the whole code using Tape, still the task to write and debug it is quite challenging. But on the other hand, the computer may easily generate it using ELENA Script Engine.
ELENA Script Engine is a simple context-free parser. Basing on the user-defined grammar it will generate the output which could be interpreted by ELENA VM (stand-alone version will be implemented later). The commands are quite simple : load the symbol, create the array, send a message.
Our simple grammar may look like this:
Let's try to use it to communicate with our code. We will have to create a new console application (type : console, namespace - mytest) which contains the following code:
After the project is compiled, we have to select - View - ELENA Interactive and in the console window we will type the following command:
The result is 7
The script engine can be used from the application as well. But before we have to switch our project type to vmconsole (select Project - Options, in the Project dialog select vm_console in type combobox and press ok)
Now write the following code in program symbol :
The result is the same:7
Now we are ready to go to our main task : Brainf**k interpreter.
Let's create a new vm_console project. We will start with the simple loopback sample in BF: [,.]
Note %"tapeOp.jumpif[1,]" extension is used for jumping (i.e. changing the index of the next element to be loaded by Tape)
If we will try to implement the tape for more complicated sample (e.g. hello world) the array will be much bigger. So it is time to use the script engine.
Let's start with a simpler task : instead of generating the tape directly (like we did it in the previous example) we will generate the set of instruction for our help object : BFTape.
The grammar will look like this:
After executing the following code:
The following tape will be created:
which we could execute:
Note that even for our simple task the resulting tape is quite big. Secondly for context-free grammar it is not possible to generate the tape directly (because of the loop). So how can we overcome these drawbacks?
Let's try it once again. First we will need another helper:
The new grammar will be much simpler:
After executing the similar code as above:
we will receive the following tape:
which is much simpler.
The tape may be executed like this:
So instead of trying to construct the tape directly we use the script engine to generate a list of instruction how to create the required tape. The main advantages of this approach : the required grammar is context free and simple, the generated list of instruction may be used for another target. For example if we replace TapeAssembler with TapeCGenerator
we may easily create the code generator, which will generate the following c function:
That's it. In this tutorial we have demonstrated the way to generate dynamic code in ELENA using mix-in Tape
Monday, June 6, 2016
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 a message or an 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 read its content: first it puts the literal constant into the stack, then system'console symbol. 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::tape_var, where  indicates an open argument list) is found, the stack looks like this:
The extension tapeOp::tape_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.
Next tutorial - Dynamic code generation in ELENA, part 2