Introduction to ELENA Programming Language – DEVELOPPARADISE
13/06/2018

Introduction to ELENA Programming Language

Introduction

ELENA is a general-purpose, object-oriented, polymorphic language with late binding. It features message dispatching/manipulation, dynamic object mutation, a script engine / interpreter and group object support.

 In this article I will present the basic concepts of how to code in ELENA and also will present the basic concepts of purely object-oriented programming.

Features of ELENA language

  • Pure polymorphic object oriented language
  • Dynamic “class mutation”
  • Message dispatching
  • Virtual machine
  • Command line 32-bit compiler
  • GUI IDE & Debugger
  • Unicode support (utf-8)
  • Complete source code
  • Number of samples
  • Script Engine / Interpreter
  • Virtual Machine Terminal

Where can I get the executable of compiler and source code?

ELENA development community

Build the ELENA compiler

The ELENA compiler was fully implemented in C++ to compile the compiler is not required external dependencies, such as Flex, Bison… you just need Visual Studio 2015/2017.

Alex implemented its own syntax generator, assembler and the tools he needed.

To build the compiler you can open the CMD, go to root elena folder and type:

  • for VS2015 : recompile.bat / for VS2017 : recompile15.bat

or, to generate release files

  • for VS2015 : vs_release.bat %cd% / for VS2017 : vs_release15.bat %cd%

Yet another programming language

“Probably every person hearing about a new language asks this question: why yet another programming language? I cannot say about all but me. When I decided 10 years ago to create a new programming language I had several reasons. First of all it was a perfect way to learn C++ (as many Russians I came from Pascal & Delphi world). But most of all I was interested in object-oriented programming. For me it was a perfect chance to learn more about it. I was fascinated with Smalltalk and decided to do something similar. Writing the compiler is a quite challenging task but it cannot be compared with the task of creating a language or a programming paradigm (after all the most valuable things in our world are ideas). So after several attempts to invent / create something original I decided to follow other way: implement something simple and gradually modify it in hope to find something interesting. So step by step the language grew (practically any part of the compiler / language was rewritten / refactored at least several times) and now it has nothing common with the one I decided at the beginning. And though many of its ideas which I thought were original are used in other languages ELENA has its own individuality and style.

So how could I answer on this question? Is ELENA yet another programming language. The answer is not simple. Yes, ELENA may be considered as another amateur language with small source code base and lack of bug tracking functionality. And no, ELENA is a conceptual language. It is designed to examine / prove feasibility of several basic concepts. From the beginning I tried to make the language as dynamic as possible. My ultimate goal is to create the programming systems with truly open architecture, the systems which can be modified / extended in run time, the system where a big number of “simple” objects actively cooperate with each other forming group objects, able to self-modify. Will the language become something more than experimental one? Only time will show. Honestly I have no idea where the language will be in the next several years. One thing I may say definitely – it will be different. ” – http://elenalang.blogspot.com.br/2010/07/yet-another-programming-language.html

First steps

It is extremely important before beginning the program in a 100% object-oriented language, know the basic concepts of a truly object-oriented language.

Basic OOP Concepts and Terminology

What are the basic conceps of ELENA ? As SmallTalk, the basic concepts are:

  • Objects
  • Classes
  • Fields
  • References
  • Methods
  • Messages
  • Inheritance
  • Receiver
  • Dynamic Binding

Objects

The ELENA “object” is very similar to the .Net or C++ object, in particular, each object is stored in memory and has several fields or methods, which can point to other objects. Each object is an instance of a class.

Classes

An object is lika a C ‘struct’ or a Ada record, the only difference between a record and a object is that the objects contains a field that identifies which class the object belongs to. In C++, C#, ELENA or Smalltalk, there is a single hidden field in every object. In ELENA this field is called the “dispatch table reference” and, inside of “dispatch table” have the informations about class. In other languages, this hidden field is called the “V-table pointer” or “dispatch table pointer”.

Fields

Fields, also known as the instance variables, are the variables declared within a class. These variables can only be read with the use of methods to assign or read the contents of the field.

References

In C++, we talk about “pointers” to objects while in C# we talk about “references” to objects. Both concepts basically the same and references are implemented by straight pointers, which are just memory address. In ELENA, we use references.

Method

Method is like a function, except that it is applied to a specific object. We also say that the method is “invoked” on an object or “sent to” an object. The object in question is called the “receiver”. Every method is invoked on receiving object. In C++ or C#, the receiver is called “this”, in ELENA – “self”.

Methods in ELENA are similar to methods in C# and C++, where they are called “member functions”. Methods may take arguments and always return a result (if no result provided “self” reference is returned). The method body is a sequence of executable statements. Methods are invoked from expression, just as in other languages.

Messages

There is an important distinction between “methods” and “messages”. A method is a body of code while a message is something that is sent. A method is similar to a function. in this analogy, sending a message is similar to calling a function. An expression which invokes a method is called a “message sending expression”.

ELENA terminology makes a clear distinction between “message” and “method”. A message-sending expression will send a message to the object. How the object responds to the message depends on the class of the object. Objects of differents classes will respond to the same message differently, since they will invoke different methods.

The “Hello World!” application

A great start for when we are studying or just knowing any new programming language, is always the famous program Hello world!“.

In order to create our first program, we have to get the ELENA compiler, for this, you just need to download the compiler in this link.

After download and extracted all the content to the folder of your choice, 

Important: You need to put the “bin” folder in your system path.

With the bin folder in your path system, this is necessary to able the generate executable to access the elena runtime than is used for example, when a exception throws to show in what class & method the exception happens.

Let’s start, first of all, we need to open the ELENA IDE, for this, you can type in CMD “elide” or, just open the executable called “elide”.

After open the elide IDE, we will create a new project, for this, we just need to go to menu “File->New->Project”, when you click in the project menu, will open a dialog like this:

Introduction to ELENA Programming Language

What every field means:

  • Type: Is the type of application than you will create, like GUI, Console, Console App running over VM ….
  • The namespace of application
  • The checkbox to warning about unresolved references
  • Additional options for the compiler
  • Target file name: the output file name
  • Output path: output of ELENA objects
  • Enable or disable the debugger
  • Command line arguments to test the application

For this article we will use these specific settings:

Introduction to ELENA Programming Language

After you click in “ok” we need to create a new source file, for this, just go to menu “File->Source file” or use the shortcut “Ctrl+N”.

Now, we can start to code… this is the code for Hello World

public program = [     system'console writeLine("Hello World!"). ].

After you write this code, you need to save the project and the source code in some directory and you just need to build it.

Understanding the code

public program =

By default, the “program” is considerated the entry point of the program, but, it’s possible to change this.

system'console writeLine("Hello World!").

In this line of code, we have a very intresting informations to look at:

  1. “system”: It is a namespace, namespace itself may contain sub elements separated by apostrophes
  2. “console”: Is a class
  3. “writeLine”: Is a message
  4. “Hello World!”: Is the parameter of the message
  5. “.”: Terminator

What this mean for us ?
That means than we send a message to method writeLine of class console in namespace system with parameter “Hello World!”.

Like in other languages, like C#, C++, Python we can add a special keyword to “import” some namespace, with this we can ‘reduce’ code.

So, the hello world example with “import” keyword will be like:

import system.  public program = [     console writeLine("Hello World!"). ].

The unique problem than we have with this program is than we can’t read the output…, so, to fix this we just need to use one more method, and, we can use this method in two ways.

First:

import system.  public program = [     console writeLine("Hello World!").     console readChar. ].

Second:

import system.  public program = [     console writeLine("Hello World!"); readChar. ].

This second way to write the same program works well because in ELENA every method returns an object.

Creating variables

Like in Smalltalk, in ELENA everything is a object, so, to create variables we just need to use a special ‘keyword’

var

The syntax of this keyword is, basically:

var <variable name> := <Class instance>.

So, as example, let’s write a simple program to read something from the keybord.

If you preffer, you can create a new console project for this example.

import system. import extensions.  public program = [     var aNumber := console write("Type a number: "); readLineTo(Integer new).     console printLine("You typed " + aNumber).     console readChar.  ].

Really simple, right ?
So, in this example we need to import a one more namespace, the namespace extensions contains the definition for the “readLineTo(<type>)”.

A variable can contains any object types, for example:

var x := IntNumber new. var y := 0. var k := system'calendar'Date new. var n := k year. var s := "Hello World". var u := 0fffffh. var l := 3.1415r. var m := RealNumber new(3.14r). var j := true. var h := false.

Conditional operator, if and else

Conditional operations in ELENA are defined as follows:

(<myCondition>) ? [ doSomething in case TRUE ] ! [ doSomehting in ELSE ].

It’s more simple to see this, in this simple example:

import system. import extensions.   public program = [     var aTrue := true.     var aFalse := false.          (aTrue && aFalse)         ? [ console printLine("aTrue && aFalse is: ",aTrue && aFalse) ]         ! [ console printLine("aTrue && aFalse is: ",aTrue && aFalse) ].              (aTrue || aFalse)         ? [ console printLine("aTrue || aFalse is: ",aTrue || aFalse) ]         ! [ console printLine("aTrue || aFalse is: ",aTrue || aFalse) ].          console readChar. ].

Template based if-else condition

To simplify the code, we may use a code template – if. Let’s take a look:

var a1 := true. var a2 := false.   if(a1 == true)  [      console printLine("a1 == true").  ];  if(a2 == false) // Else if case  [  ];  [ // Else case     console printLine("a1 != true").  ] 
Let’s implement an example program containing many conditions and variable statements.
import extensions.  public program = [   console writeLine:"Hi!! :)".   var aYourName := console writeLine:"Whats your name ?"; readLine.   console writeLine:"How old are you ?".   var aYourAge := console readLineTo(Integer new).     var aNumberOfChildrens := 0.   if((aYourAge >= 0) && (aYourAge <= 10))   [     console writeLine:"You're still very young!!"   ];   if((aYourAge >= 11) && (aYourAge <= 20))   [     console writeLine:"Adolescence, come a lot of people angry?"   ];   if((aYourAge >= 21) && (aYourAge <= 30))   [     console writeLine:"Do you already have children ? ( yes/no )".     var aHaveChildren := console readLine.     if(aHaveChildren lowerCase == "yes")     [       aNumberOfChildrens := console writeLine:"Cool!! how many kids do you have ?";0                                      readLineTo(Integer new)     ].   ];   if((aYourAge >= 31) && (aYourAge <= 40))   [     console writeLine:"The first 40 years of childhood are the most difficult !!!"   ];   if((aYourAge >= 41) && (aYourAge <= 50))   [     console writeLine:"Life begins at 40!"   ];   if((aYourAge >= 51) && (aYourAge <= 60))   [     console writeLine:"Enjoy your LIFE !!!"   ];   if(aYourAge >= 61)   [     console writeLine:"You are at the age to enjoy and relax !!"   ].   console readChar. ].

Loops

Like in other programming languages, ELENA have a support for loops, let’s take a look at the while loop, this loop is implemented using a template while.

while(<condition>) [ /* ... loop code... */ ].

So, to make clear the usage of loop, let’s write a simple program to:

import system. import extensions.   public program = [     var aArray := Array new:200.          var i := 0.     while(i < aArray length)     [        aArray[i] := i * 10.        i := i + 1     ].            i := 0.     while (i < aArray length)     [       console printLine("At index ", i, " has the value:", aArray[i]).       i := i + 1     ].          console readChar. ].

Classes and Methods and Fields

“ELENA is an object-oriented language. To create a program we have to declare new classes.

A class encapsulates data (fields) with code (methods) to access it. In most cases it is not possible to get a direct access to the class content. Usually the field refers to another class and so on until we reach “primitive” ones which content are considered as raw data (e.g. numeric or literal values).

Classes form the inheritance tree. There is the common super class – system’Object. ELENA does not support multiple inheritance, though it is possible to inherit the code using a dispatch handler (mixins / group objects). When the parent is not provided the class inherits directly system’Object (the super class).

A class instance can be created with the help of the special methods – constructors. A constructor is used mostly to initialize the class fields. There are special types of classes which do not have constructors and can be used directly (nested classes, extensions, closures). A class itself is considered as a stateless object.” – https://github.com/ELENA-LANG/elena-lang/wiki/ELENA-Programming-Language#13-classes-symbols-nested-classes-and-closures

To understand this information, we will write some classes, to make clearer its implementation and use.

You can create a new project to create the classes that will create as example.

For the first example, we will create a simple class.

import system. import extensions.  class MyFirstClass {     constructor new     [         console printLine("MyFirstClass instancied!!! ehhhhhhh!!").     ] }  public program = [     var myFirstClass := MyFirstClass new.     console readChar. ].

So, when you execute the program you will see the message on console:

MyFirstClass instancied!!! ehhhhhhh!!

Really simple, right ? (:

So, now that we know how to create a class, we will add methods in this class and we will create another constructor for this class, passing two arguments, those two parameters will be saved in two different fields

First of all, we will create the new constructor with the new parameters, the class will look like:

import system. import extensions.  class MyFirstClass {     int field1.     int field2.          constructor new     [         console printLine("MyFirstClass instancied!!! ehhhhhhh!!").     ]          constructor new(int aParameter1, int aParameter2)     [         console printLine("MyFirstClass instancied!!! with 2 parameters!!").         console printLine("My first parameter.: ", aParameter1).         console printLine("My second parameter: ", aParameter2).                  field1 := aParameter1.         field2 := aParameter2.     ] }  public program = [     var myFirstClass := MyFirstClass new.     var myFirstClassWith2Parameters := MyFirstClass new(500, 300).     console readChar. ].

Notice that in program, we created a new variable to store the new instance of the class, which is initialized with two parameters.

Now let’s implement some methods to work with these two values that were passed by parameter.

class MyFirstClass {     int field1.     int field2.          constructor new     [         console printLine("MyFirstClass instancied!!! ehhhhhhh!!").                  field1 := 0.         field2 := 0.     ]          constructor new(int aParameter1, int aParameter2)     [         console printLine("MyFirstClass instancied!!! with 2 parameters!!").         console printLine("My first parameter.: ", aParameter1).         console printLine("My second parameter: ", aParameter2).                  field1 := aParameter1.         field2 := aParameter2.     ]          addTwoNumbers     [         ^ field1 + field2.     ]          addTwoNumbers(int aNumber1, int aNumber2)     [         ^ aNumber1 + aNumber2.     ]          addTwoNumbersAndPrintIt     [         var addResult := self addTwoNumbers.         console printLine("Result of ", field1, "+", field2, "=", addResult).     ]          addTwoNumbersAndPrintIt(int aNumber1Argument, int aNumber2Argument)     [         var addResult := self addTwoNumbers(aNumber1Argument, aNumber2Argument).         console printLine(Result of ", aNumber1Argument,"+",aNumber2Argument,"=",addResult).     ] }

Ok, now we have 4 methods, and we want to use this methods. Using the methods have no secret, we are using methods and classes that begin to write the codes in Elena for this article!! (:

So, the usage sample of this class is:

public program = [         var myFirstClass := MyFirstClass new.     var myFirstClassWith2Parameters := MyFirstClass new(500, 200).          // Working just with the myFirstClassInstance     myFirstClass addTwoNumbersAndPrintIt.     myFirstClass addTwoNumbersAndPrintIt(20, 20).          console writeLine:"---------------------------------------".          // Working just with the myFirstClassWith2Parameters     myFirstClassWith2Parameters addTwoNumbersAndPrintIt.     myFirstClassWith2Parameters addTwoNumbersAndPrintIt(15, 320).          console readChar. ].

One of the basic principles of object-oriented programming is inheritance between classes, so let’s implement two classes, a base class and a derived class.

import system. import extensions.  class MyFirstBaseClass {     literal theField1.     literal theField2.          constructor new     [         theField1 := "".         theField2 := "".     ]          constructor new(literal aField1)     [         console printLine("fieldBaseClass1 = ", aField1).                  theField1 := aField1.         theField2 := "".     ]          constructor new(literal aField1, literal aField2)     [         console printLine("fieldBaseClass1 = ", aField1).         console printLine("fieldBaseClass2 = ", aField2).                  theField1 := aField1.         theField2 := aField2.     ]          printFields     [         console printLine(self field1).         console printLine(self field2).     ]          field1 = theField1.     field2 = theField2. }  class MyFirstDerivedClass :: MyFirstBaseClass {     constructor new     [         console writeLine:"Constructor of MyFirstDerivedClass called with no parameters".     ]          constructor new(literal aField1)         <= new(aField1 + " -- Called from DerivedClass");     [         console printLine("Constructor of MyFirstDerivedClass called with 1 parameters").         console printLine("field1 = ", aField1).     ]          constructor new(literal aField1, literal aField2)         <= new(aField1 + " -- Called from DerivedClass",                 aField2 + " -- Called from DerivedClass");     [         console printLine("Constructor of MyFirstDerivedClass called with 2 parameters").         console printLine("field1 = ", aField1).         console printLine("field2 = ", aField2).     ] }  public program = [     var myFirstDerivedClassInstance := nil.          var numberOfArgsToInitInInstance := 0.     var continueLoop := true.     while(continueLoop)     [         numberOfArgsToInitInInstance :=             console printLine("Enter with how much variables you want to use in instance init ? (0/1/2)");                      readLineTo(Integer new).                      if((numberOfArgsToInitInInstance >= 0) && (numberOfArgsToInitInInstance <= 2))            [ continueLoop := false ].     ].          // This is a 'swith'...     numberOfArgsToInitInInstance =>         0 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new ];         1 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new("Argument1 from program") ];         2 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new("Argument1 from program",                                                                       "Argument2 from program") ];           ! [ console writeLine:"invalid option..." ].                myFirstDerivedClassInstance printFields.          console readChar. ].

More examples

You can get more examples and learn more about ELENA language in Rosseta Code:

http://rosettacode.org/wiki/Category:Elena

More articles about Elena programming language

Any question ?

Feel free to ask!

Who uses ELENA language and for what?

That is a question that has been on everyone’s mind, when they see something about a new language .

Well, I use the language, and develop business programs in ELENA language

The programs that I developed in ELENA control all payments received by automakers banks such as Ford, GM, (Mercedes) Fundo Estrela, (Citroën and Peugeot) PSA Bank, (Fiat, Chrysler and used cars by Fiat) FIDIS Bank, this means that if you live in Brazil and bought a car from one of these manufacturers, their payment has been processed by a program written in ELENA.

And I wrote some prograns to Bradesco Bank and HSBC bank to process and generate payment data.

Open source projects in Elena

History

  • 13-Jun-18
    – Updated to be compatible with ELENA 3.4
    – fixing spelling
  • 26-Jun-17
    – Added community group
  • 19-Jun-17
    – Added new version of compiler to download
  • 09-Jun-17
    – Updated all the article to the new version of Elena language
    – Added the new release of elena langauge to download
    – Added section “if-else template”
    – Added section “Open source projects in Elena”
  • 17-Jan-17
    – Added link to the new Elena article in Code Project
  • 12-Dec-16
    – Updated elena compiler to the last version
  • 22-Sep-16:
    – Added link to rosseta code
  • 12-Sep-16:
    – Article created