Friday, June 10, 2011

ELENA Virtual Machine Script

The last year in the article The Next Big Thing I mentioned the intention to create a special VM script to manipulate with the objects. It took me half a year to come up with the possible solution. And though it is a very early alpha a new tool introduced in the current release (bin\elt.exe) put this idea into practice.

So what is ELENA script? First of all it should not be confused with a traditional script language, it has not its own interpreter, grammar, libraries. It is pure a set of instruction for ELENA VM to load existing symbols into the memory and send them messages. So its prime goal is to assemble objects in VM memory with the help of special rules (if it is done from ELENA program it could be considered as a self assembling, hence its name - DSA (dynamic self assembling) rule). The possibility to work with your own objects directly without need to write a special test program could be useful as well. I hope it will find an application for code generations, open architecture systems (as a way to build complex group objects) and so on.

The actual system consists of VM terminal (elt.exe) and a special script engine (elenasm.dll) working with ELENA Virtual Machine (elenavm.dll). It is designed to be used directly from ELENA program as well (of course if the program is VM client). There are actually two types of script: inline one (which can be used directly with VM without need for the script engine) and grammar driven (where context-free and DSA rules modify the script text to create an inline one, so it could be considered as a additional layer between the client and VM).

Before I will show how it looks to work with VM terminal I would like to notice that at the moment it is very early alpha version and it is still unstable. Secondly direct script requires some knowledge of ELENA stack machine: a symbol reference should be always preceded by a parameter (if there is no parameter, nil constant should be used), the same is true for the message call. Thirdly only generic messages can be used (without a subject qualifier; actually it was designed for such a use from the beginning). Note also, that the current implementation allows to work only with console applications. There should be a special VM client for GUI applications. And finally it is presumed that you works with LIB2 programs (if you need to use LIB, please change libpath in elt.cfg to ..\lib)

So let's start the terminal. Inline script should be executed with an option "-i"

First of all we need to start a virtual machine and load default template

-i @config win32_vm_console2 @start
The canonical hello world sample will be looked like this:

-i &nil 'program'output "Hello World!!" ^write

Let's print the sum of two numbers:

-i &nil 'program'output 2 3 ^add ^write

Now let's do a bit more complex task and call existing application (it should be compiled before). First of all you have to tell VM where your compiled modules are located

-i @use "..\examples\binary"

And now let's execute the program symbol:

-i &nil binary'program &nil ^evaluate

As you see it is not very convenient way to write the expression (but it could be ok for computer programs). So let's try to use some grammar. I would like to note that the script engine support simple context free grammar. There are several restriction: only one terminal symbol (it should be first) and non-terminal (with except of AB type rule) can be used. So only the following type of rules are allowed:

  • S::=aA
  • S::=a
  • S::=A
  • S::=AB
  • S::=$eps

So let's define our simple rule

-g print ::= "?" expression;
-g expression ::= $literal;

(Note $literal is a special terminal mask accepting any literal constant)

DSA rules are used to provide required symbols and operations.

-g print => &nil 'program'output $body ^write;

(Note $body indicates the place where rule content is inserted)

Now let's mark print as a start rule

-g start ::= print

Let's test if our code works

? "hello world!!"

You could load existing scripts as well

-c..\examples\opencalc\opencalc.vl2

And now you may calculate some basic arithmetic expression

?2*3+4*5

You may look how your expression is translated into direct script with the following command -ton

-ton
?2*3+4*5

You will see the script listing. To turn the tracing off type the following

-toff

To exit the program, type

-q

That's all I would like to show in this tutorial. In near future I will show some more complex samples (for example I will implement bf interpreter)

P.S. Note that in the current implementation several key features are lacking, for example it is not possible to declare variables. I'm going to implement them in the recent releases. Once again it is still unclear if this turns into something more than a nice toy but it is the direction ELENA Program Language Project is moving to.

No comments:

Post a Comment