Monday, June 6, 2016

Tutorial : dynamic code generation in ELENA

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:

   var tape := system'dynamic'Tape::(
                    "Hello World", 
   tape eval.

The result will be:

Hello World

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[1]. 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:

import system'dynamic.

   var tape := Tape::(

   tape eval:"Hello again".

The result is:

Hello again

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?

   "Hello again"          <- stack top

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:

   2                       <- stack top
   "Hello again"           

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:

2 var &args:(self,"Hello again").

So after the extension is executed our stack looks like this:

   "Hello again"           <- stack top
   "Hello again"           

And before the final message:

   system'console          <- stack top
   "Hello again"           
   "Hello again"           

Our second group object is system'dynamic'Struct. Lets' consider a simple use case:

    var s := Struct::(%x,2,%y,3).    
    var x := s x.
    var y := s y.

As you may see it is equivalent to the following code:

    var s := {  x = 2. y = 3. }
    var x := s x.
    var y := s y.

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:

   var object := system'dynamic'Struct::( 
   object print:"Hello again".

Which is equivalent to the plain code:

var object := { print : s [ console writeLine:s. ] }.

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

Tuesday, May 17, 2016

ELENA 2.0.1 is out now

A new major release of ELENA programming language is available : 2.0.1

What's new:

    • new statement #extern - exclude the code from GC
    • new simplified syntax to declare typified variable / field
    • new simplified syntax to declare subject
    • typecasting message is auto generated
    • #201 : shorthand property settings
    • #54 : templates
    • #231 - new syntax : Signature / Message constants
    • #128 : constructors may now return constant value
    • #136 : refactoring access to primitive managed code (core_routines)
    • #40 : self is typified for type extensions
  • ELC
    • binary incompatible due to implementing issues #217 and #54
    • #217 : output code optimization
    • critical bug fixes in COREX
    • critical bug fixes in GCX
    • #228 : safe region
  • LIB
    • system : BitArray, BitArray32
    • system'routines : PatternFilter2,PatternFilter3,PatternFilter4, PatternFilter5, PatternFilter6
    • system'routines : enumerable.filter&pattern[1]
    • system'routines :[1]
    • system'routines : enumerable.order&by[1]
    • system'routines : enumerable.get&count
    • system'io : stream.set&length[1]
    • system :[1] supports now an integer parameter
    • system'text : TextBuffer => TextBuilder
    • system'io : Console output operation can be redirected now
    • system'collections : #234 issue - listof:subject
  • IDE
    • issue #4 : IDE and Chinese characters
    • Menu - View - ELENA Interactive (#58: vm console)
  • Samples
    • rosetta sample : Top rank per group
    • rosetta sample : Truncatable primes
    • rosetta sample : Visualize a tree
  • ElenaScript
    • #65 issue : script engine is optimized

Wednesday, February 10, 2016

ELENA 1.9.25 : what's new

The latest release contains several critical bug fixes in the garbage collection algorithm. Currently GC for single thread application looks pretty stable. GC for multi-threading applications is still not 100% operational. It works quite ok for anagram sample (probably the most memory consuming STA program up to date). But chatserver (multi-threading program) crashes. GC is the prime suspect. Lock implementation was fixed as well.

Several critical bug fixes were done for structure fields (#216) and open arguments(#211).

Several samples were ported to Linux platform. It is still behind Windows one. When Linux debugger will be finished, probably the work will be accelerated.

A lot of small enhancements were done for LIB30. There are 100 Rosetta code task implemented for ELENA now. As a result new functionality was introduced in system'routines, extensions packages.

Tuesday, February 9, 2016

ELENA 1.9.25 is out (win32 / linux32)

ELENA 1.9.25 is out

A new release of ELENA programming language is available : 1.9.25

Linux Debian package includes now examples (\home\elena-lang) and LIB30 source code (\usr\local\src\elena).

For windows the path to elenart.dll should be included into PATHS. When the setup file is used it is done automatically.

What's new:

  • ELC
    • binary incompatible due to implementing issues #134 and #136
    • issue #132 : GC hotfix
    • issue #216 : operation with structure fields in stacksafe method
    • issue #211 : Message call sequence with open arguments
    • critical bug fix in lock statement
  • LIB
    • system'routines : enumerable.sequenceEqual[1], indexable.reverse&at[2]
    • system'routines : enumerable.accumulate&with[2]
    • extensions : textwriter.write&args[1,], textwriter.writeLine&args[1,]
    • extensions'text : (literal/wide).toByteArray
    • system : Message.get&literal$
    • system'io : MemoryBuffer
    • #213 : system'MethodNotFoundException provides the message name now
  • Samples
    • linux : anonymrec sample ported
    • linux : aplusb sample ported
    • linux : arraycallback sample ported
    • linux : arithmeticint sample ported
    • linux : arithmmean sample ported
    • linux : arrayconcat sample ported
    • linux : arraymode sample ported
    • linux : arrays sample ported
    • linux : associativearrays sample ported
    • linux : amb sample ported
    • linux : anagram sample ported
    • linux : arithmeval sample ported