Friday, March 30, 2012

Getting Started: ELENA DSA Script, part 2

In the previous post I showed how to create a pure dynamic code combining different group objects with each other without actually writing the code. So Hello world program will look like this:

&eval &nil sys'dynamics'castvariable 
  &nil sys'dynamics'batch
     &sys'vm'routines'ereturnprop "Hello World" &wrap 
        ~sys'dynamics'group_member ^append
     &write &nil 'program'output &wrap 
       ~sys'dynamics'group_member ^append
     ~sys'dynamics'group_member ^append 
  &get &nil 'program'input &wrap
  ~sys'dynamics'group_member ^append 
&wrap &nil ^eval

which is equivalent to the following ELENA code:

__wrap(__eval, __cast(
         __batch(__wrap(sys'vm'routines'ereturnprop, "Hello World"), 
                 __wrap(__write, 'program'output)),
         __wrap(__get, 'program'input))) eval.

In this example we use another group object - cast. A cast collection repeats a received message for every its member.

Inline script is not intended for direct usage so we should use a grammar. We could use a grammar script provided in the language - default.vl2

Let's start VM terminal

elt

Now we may load existing grammar scripts with a command - c

-cscripts\default.vl2

Now let's start a VM

.start

Our program consists of two actions - printing a message and waiting for any key. So let's assign a variable output and input to these actions

output = [ sys'vm'routines'ereturnprop->"Hello world" 
            write->'program'output ]
input = get -> program'input

And combine them into the program

action = < output input >
program = eval->action

So now we could execute our program

~program::eval

As you see with a help of the grammar our program becomes much simpler. The grammar is quite simple

a -> b 

creates a wrap,

[ ... ] 

creates a batch,

< ... > 

create a cast

variable =  

assigns the expression to the variable, and

obj::verb 

sends a message verb to obj

No comments:

Post a Comment