Introduction to ELENA Programming Language

0
103

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?

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:

  • recompile.bat

or, to generate release files

  • vs_release.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 objet. 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

Field is also known as the instance variable, ie, are the variables declared within a class. These variables can only be lit 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 object”, but ELENA does not use this terminology, the “this” terminology makes for awkward wording.

Methods in ELENA are similar to methods in C# and C++, where they are called “member functions”. Methods may take arguments and may return a result. 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”. but C# or C++ terminology sometimes confuses these concepts. A message-sending expression will send a message to the object. How the object responds to the message depends on the clas 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:

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:

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

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

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 method
  4. “Hello World!”: Is the parameter of method
  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.

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.

program =
[
 console writeLine:"Hello World!".
 console readChar.
].

Second:

import system.

program =
[
 console writeLine:"Hello World!" readChar.
].

This second way to write the same program works well because ELENA is a fully object oriented language, so, we call the method writeLine with parameter “Hello World”, so, we send a message writeLine with parameter “Hello World” after the execution of this message the system execute the next message, the next message is readChar.

Creating variables

Like in SmallTalk, ELENA don’t have types, because everything is a object, so, to create variables we just need to use a special ‘keyword’

var

The sintax 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.

program =
[
 var aNumber := console writeLine:"Type a number: " readLine:(Integer new).
 console writeLine:"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 “readLine:<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 real: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.

program =
[
 var aTrue := true.
 var aFalse := false.
 
 (aTrue && aFalse)
 ? [ console writeLine:"aTrue && aFalse is: ":(aTrue && aFalse). ]
 ! [ console writeLine:"aTrue && aFalse is: ":(aTrue && aFalse). ].
 
 (aTrue || aFalse)
 ? [ console writeLine:"aTrue || aFalse is: ":(aTrue || aFalse). ]
 ! [ console writeLine:"aTrue || aFalse is: ":(aTrue || aFalse). ].
 
 console readChar.
].

To make clear the use of conditional operator we will implement a example program, this program will implement a system with many conditions and variables statement.

If you preffer, you can create a new project for this example… (:

import system.
import extensions.
import system'culture.

program =
[
 console writeLine:"Hi!! :)".
 var aYourName := console writeLine:"Whats your name ?" readLine.
 
 console writeLine:"How old are you ?".
 var aYourAge := console readLine:(Integer new).
 
 var aNumberOfChildrens := 0.
 
 ((aYourAge >= 0) && (aYourAge <= 10))
 ? [ console writeLine:"You're still very young!!". ]
 ! [
 ((aYourAge >= 11) && (aYourAge <= 20))
 ? [ console writeLine:"Adolescence, come a lot of people angry?". ]
 ! [
 ((aYourAge >= 21) && (aYourAge <= 30))
 ? [
 console writeLine:"Do you already have children ? ( yes/no )".
 var aHaveChildren := console readLine.
 ((aHaveChildren toLower &locale:currentLocale) == "yes")
 ? [ aNumberOfChildrens := console writeLine:"Cool!! how many kids do you have ?" readLine:(Integer new). ].
 ]
 ! [
 ((aYourAge >= 31) && (aYourAge <= 40))
 ? [ console writeLine:"The first 40 years of childhood are the most difficult !!!". ]
 ! [
 ((aYourAge >= 41) && (aYourAge <= 50))
 ? [ console writeLine:"Life begins at 40!". ]
 ! [
 ((aYourAge >= 51) && (aYourAge <= 60))
 ? [
 console writeLine:"Enjoy your LIFE !!!".
 ]
 ! [
 (aYourAge >= 61)
 ? [ console writeLine:"You are at the age to enjoy and relax !!". ].
 ].
 ].
 ].
 ].
 ].
 ].
 
 console readChar.
].

Maybe you‘re wondering, that is confusing to read this code, why not use something like elseif?

The answer to that is, there is no something like elseifin in ELENA or SmallTalk, then, when necessary, you have to write a series of nested if statements, or review the logic of your program.

Template based if-else condition

Elena language has a template based ifs conditions, this has ben madded to make the language more similar with SmallTalk and some other languages, the usage os if templates is really simple, let’s take a look:

var a1 := true.
var a2 := false. if(a1 == true) [ console writeLine:"a1 == true". ]; [ console writeLine:"a1 != true". ]

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 system.

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

So, to make more clear the usage of loop, let’s write a simple program to naviagete on array.

import system.

program =
[
 var aArray := Array new:200.
 
 var aI := 0.
 while(aI < (aArray length))
 [
 aArray@aI := aI * 10.
 aI := aI + 1.
 ].
 
 aI := 0.
 while (aI < (aArray length))
 [
 console write:("At index " + (aI literal) + " has the value:").
 console writeLine:(aArray@aI).
 aI := aI + 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 writeLine:"MyFirstClass instancied!!! ehhhhhhh!!".
 ]
}

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
{
 object field1.
 object field2.
 
    constructor new
    [
        console writeLine:"MyFirstClass instancied!!! ehhhhhhh!!".
    ]
 
 constructor new myFirstParameter:aParameter1 mySecondParameter:aParameter2
 [
 console writeLine:"MyFirstClass instancied!!! with 2 parameters!!".
 console writeLine:"My first parameter.: ":(aParameter1 literal).
 console writeLine:"My second parameter: ":(aParameter2 literal).
 
 field1 := aParameter1.
 field2 := aParameter2.
 ]
}

program =
[
 var myFirstClass := MyFirstClass new.
 var myFirstClassWith2Parameters := MyFirstClass new &myFirstParameter:500 &mySecondParameter:200.
 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
{
 object field1.
 object field2.
 
 constructor new
 [
 console writeLine:"MyFirstClass instancied!!! ehhhhhhh!!".
 
 field1 := 0.
 field2 := 0.
 ]
 
 constructor new myFirstParameter:aParameter1 mySecondParameter:aParameter2
 [
 console writeLine:"MyFirstClass instancied!!! with 2 parameters!!".
 console writeLine:"My first parameter.: ":(aParameter1 literal).
 console writeLine:"My second parameter: ":(aParameter2 literal).
 
 field1 := aParameter1.
 field2 := aParameter2.
 ]
 
 addTwoNumbers
 [
 ^(field1 + field2).
 ]
 
 addTwoNumbers number1:aNumber1 number2:aNumber2
 [
 ^(aNumber1 + aNumber2).
 ]
 
 addTwoNumbersAndPrintIt
 [
 var addResult := self addTwoNumbers.
 console writeLine:"Result of ":(field1 literal):"+":(field2 literal):"=":addResult.
 ]
 
 addTwoNumbersAndPrintIt number1:aNumber1Argument number2:aNumber2Argument
 [
 var addResult := self addTwoNumbers &number1:aNumber1Argument &number2:aNumber2Argument.
 console writeLine:"Result of ":(aNumber1Argument literal):"+":(aNumber2Argument literal):"=":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:

program =
[ 
 var myFirstClass := MyFirstClass new.
 var myFirstClassWith2Parameters := MyFirstClass new myFirstParameter:500 mySecondParameter:200.
 
 // Working just with the myFirstClassInstance
 myFirstClass addTwoNumbersAndPrintIt.
 myFirstClass addTwoNumbersAndPrintIt number1:20 number2:20.
 
 console writeLine:"---------------------------------------".
 
 // Working just with the myFirstClassWith2Parameters
 myFirstClassWith2Parameters addTwoNumbersAndPrintIt.
 myFirstClassWith2Parameters addTwoNumbersAndPrintIt number1:15 number2: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
{
 object theField1.
 object theField2.
 
 constructor new
 [
 theField1 := "".
 theField2 := "".
 ]
 
 constructor new fieldBaseClass1:aField1
 [
 console writeLine:"fieldBaseClass1 = ":aField1.
 
 theField1 := aField1.
 theField2 := "".
 ]
 
 constructor new fieldBaseClass1:aField1 fieldBaseClass2:aField2
 [
 console writeLine:"fieldBaseClass1 = ":aField1.
 console writeLine:"fieldBaseClass2 = ":aField2.
 
 theField1 := aField1.
 theField2 := aField2.
 ]
 
 printFields
 [
 console writeLine:(self field1).
 console writeLine:(self field2).
 ]
 
 field1 = theField1.
 field2 = theField2.
}

class MyFirstDerivedClass :: MyFirstBaseClass
{
 constructor new
 [
 console writeLine:"Constructor of MyFirstDerivedClass called with no parameters".
 ]
 
 constructor new field1:aField1
 <= new fieldBaseClass1:(aField1 + " -- Called from DerivedClass"))
 [
 console writeLine:"Constructor of MyFirstDerivedClass called with 1 parameters".
 console writeLine:"field1 = ":aField1.
 ]
 
 constructor new field1:aField1 field2:aField2
 <= new fieldBaseClass1:(aField1 + " -- Called from DerivedClass") fieldBaseClass2:(aField2 + " -- Called from DerivedClass"))
 [
 console writeLine:"Constructor of MyFirstDerivedClass called with 2 parameters".
 console writeLine:"field1 = ":aField1.
 console writeLine:"field2 = ":aField2.
 ]
}

program =
[
 var myFirstDerivedClassInstance := nil.
 
 var numberOfArgsToInitInInstance := 0.
 var continueLoop := true.
 while(continueLoop)
 [
 numberOfArgsToInitInInstance :=
 console writeLine:("Enter with how much variables you want to use in instance init ? (0/1/2)") readLine:(Integer new).
 
 if((numberOfArgsToInitInInstance >= 0) && (numberOfArgsToInitInInstance <= 2))
 ? [ continueLoop := false. ].
 ].
 
 // This is a 'swith'...
 numberOfArgsToInitInInstance =>
 0 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new. ]
 1 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new field1:"Argument1 from program". ]
 2 ? [ myFirstDerivedClassInstance := MyFirstDerivedClass new field1:"Argument1 from program" field2:"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

  • 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

LEAVE A REPLY