Category:Elena: Difference between revisions
Line 20: | Line 20: | ||
All referring entities in the language are objects. A variable is a reference to the object allocated in the program heap. The literal and numeric constants are references to the objects allocated in the static memory. |
All referring entities in the language are objects. A variable is a reference to the object allocated in the program heap. The literal and numeric constants are references to the objects allocated in the static memory. |
||
Namespaces |
== Namespaces == |
||
-- |
|||
Any ELENA program or library consists of modules (files with .NL extension) containing |
Any ELENA program or library consists of modules (files with .NL extension) containing |
||
Line 33: | Line 32: | ||
(the new module namespace consists of the root one plus the folder relative path) |
(the new module namespace consists of the root one plus the folder relative path) |
||
Messaging |
== Messaging == |
||
-- |
|||
As in the most of dynamic object-oriented languages the main way to interact with objects in ELENA is sending a message. Unlike others the message name is structured and consists of a verb, a signature and a parameter counter. The verb defines a message action, for example read or write some data. There are only limited set of possible verbs (e.g. eval[uate], add, set, get, run, seek and so on). In general the signature is user defined and describes the message parameters. It can be used to define some custom |
As in the most of dynamic object-oriented languages the main way to interact with objects in ELENA is sending a message. Unlike others the message name is structured and consists of a verb, a signature and a parameter counter. The verb defines a message action, for example read or write some data. There are only limited set of possible verbs (e.g. eval[uate], add, set, get, run, seek and so on). In general the signature is user defined and describes the message parameters. It can be used to define some custom |
||
Line 68: | Line 66: | ||
Classes, Roles and Symbols |
== Classes, Roles and Symbols == |
||
-- |
|||
ELENA is an object-oriented language, so to create a program we have to declare new classes. |
ELENA is an object-oriented language, so to create a program we have to declare new classes. |
||
Line 127: | Line 124: | ||
#static SingletonClass = DerivedClass new &field:0 &field:0. |
#static SingletonClass = DerivedClass new &field:0 &field:0. |
||
Code blocks |
== Code blocks == |
||
-- |
|||
ELENA code block consists of a sequence of statements. The block is enclosed in square brackets and may contain nested sub code blocks (which in fact are inline action classes). The statement terminator is a dot. |
ELENA code block consists of a sequence of statements. The block is enclosed in square brackets and may contain nested sub code blocks (which in fact are inline action classes). The statement terminator is a dot. |
||
Line 168: | Line 164: | ||
Conditional branching |
== Conditional branching == |
||
-- |
|||
ELENA like Smalltalk does not support any special language constructs to implement the conditional branching. Instead special Boolean symbols (system’true and system’false) are used. All conditional operations should return these symbols as a result. |
ELENA like Smalltalk does not support any special language constructs to implement the conditional branching. Instead special Boolean symbols (system’true and system’false) are used. All conditional operations should return these symbols as a result. |
||
Revision as of 08:07, 13 June 2014
![Language](http://static.miraheze.org/rosettacodewiki/thumb/2/27/Rcode-button-language-crushed.png/64px-Rcode-button-language-crushed.png)
This programming language may be used to instruct a computer to perform a task.
Official website |
---|
Execution method: | Compiled (bytecode) |
---|---|
Garbage collected: | Yes |
Type safety: | Safe |
Type strength: | Strong |
Type expression: | Implicit |
Type checking: | Dynamic |
See Also: |
Overview
ELENA is a general-purpose, object-oriented, polymorphic language with late binding. It features multiple dispatching, context-dependent roles, dynamic inheritance and group object support.
The language treats any program as a set of objects (class instances). The program flow is a process of interaction between objects by sending each other messages. A message may have attached information (a message parameter). An object may react on message if it has an appropriate message handler (a method). If the object reacts on the message it is treated as successful otherwise unsuccessful. In its turn the method may send messages to other objects and so on until the flow reaches the method written by external tools (meta method). If the message is unsuccessful the flow is considered to be broken. It's possible to declare alternative flow which are executed if previous ones are broken. The method has only one input parameter and one output parameter (the method may return itself).
Every object may be formed up with other objects characterizing its internal state. They in turn may be formed with others and so on until meta objects which internal states are considered as raw data.
All referring entities in the language are objects. A variable is a reference to the object allocated in the program heap. The literal and numeric constants are references to the objects allocated in the static memory.
Namespaces
Any ELENA program or library consists of modules (files with .NL extension) containing classes and symbols. Every class or symbol may be referred by its namespace (or to put it other way around a symbol namespace is a path to the symbol module).
All source files (files with .L extension) located in the same folder are compiled into the corresponding module. A project file (a file with .PRJ extension) defines the root namespace and the output type (stand-alone executable, VM executable or a library). The project may produce several modules if it contains the files located in sub folders (the new module namespace consists of the root one plus the folder relative path)
Messaging
As in the most of dynamic object-oriented languages the main way to interact with objects in ELENA is sending a message. Unlike others the message name is structured and consists of a verb, a signature and a parameter counter. The verb defines a message action, for example read or write some data. There are only limited set of possible verbs (e.g. eval[uate], add, set, get, run, seek and so on). In general the signature is user defined and describes the message parameters. It can be used to define some custom action as well (e.g. writeLine, which in fact is eval&writeLine(1)). If the signature is not provided the message is considered to be generic and can be qualified (for example by dispatching).
If the object wants to handle the message it has to contain the method with the same name. If no method mapping was found the flow is considered to be broken and the control goes to the next alternative flow (exception handler) or the program is stopped.
The simple code to send a message looks like this:
console write:"Hello World".
Note: "write" is a generic message; a literal constant is a parameter.
Several messages can be send in one statement, the parameter itself may be result of object interactions:
console write "2 + 2 =" write:(2 add:2).
We could use operators to have the shorter code:
console << "2+2=" << 2 + 2.
Note: In most cases "<<" is a synonym to "write" and "+" to "add".
Several parameters can be passed in the message as well:
control foreach: (1,2,3) &do:printingLn.
Ampersand is used to indicate that the signature has several arguments (subjects). The actual message name is eval&foreach&do(2).
The generic message can have several parameters as well:
consoleEx writeLine:”a+b=”: (a + b).
Classes, Roles and Symbols
ELENA is an object-oriented language, so 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 (it makes sense for dynamic languages when in the most cases code is generic and can be applied for different "types"). Usually the fields refer to another classes and so on until we reach "primitive" ones which content are considered as raw data (e.g. numeric or literal values).
To work with the class we have to create its instance with the help of the special methods - constructors. A constructor is used mostly to initialize the class fields. There are special type of classes which do not have fields and constructors and can be used directly (roles).
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 redirect handler (so called "horizontal inheritance"). When the parent is not provided the class inherits directly system'Object (the super class).
#class BaseClass { #field theField1. #field theField2. #method field1 = theField1. #method field2 = theField. } #class DerivedClass : BaseClass { #constructor new &field1:aField2 &field2:aField2 [ theField1 := aField1. theField2 := aField2. ] #method add &field1:aField2 &field2:aField2 = MyClass new &Field1:(theField1 + aField1) &Field2:(theField2 + aField2). }
To create a class instance we have to send a message (usually new) to its symbol (a class symbol is declared implicitly for every class and can be used as a normal one)
#var anObject := DerivedClass new &field1:1 &field2:1. // DerivedClass is a symbol
Roles cannot have constructors and their symbols can be used directly
#class(role)ClassHelper { #method sumOf:anObject1:anObject2 = anObject1 add &field1::anObject2 &field2::anObject1. } ... #var aSum := ClassHelper sumOf:anObject1:anObject2.
In general the symbol is a named expression and can be used to declare initialized objects, constants, reusable expressions and so on.
#symbol ZeroClass = DerivedClass new &field:0 &field:0.
A static symbol is the class instance which state is preserved. There could be only one instance of static symbol.
#static SingletonClass = DerivedClass new &field:0 &field:0.
Code blocks
ELENA code block consists of a sequence of statements. The block is enclosed in square brackets and may contain nested sub code blocks (which in fact are inline action classes). The statement terminator is a dot.
#method printAckermann &n:n &m:m [ control forrange &int:0 &int:n &do: (&int:i) [ control forrange &int:0 &int:m &do: (&int:j) [ ... console writeLine. ]. ]. ]
When a method should return a result (other than self) return statement is used. It should be the last statement in the block.
[ ... ^ aRetVal / anArray length. ]
If the code block contains only return statement the simplified syntax can be used:
#method Number = convertor toReal:theValue.
or there is an alternative block expression
[ convertor toReal:theValue ]
Note: it should not end with the terminator symbol
It is possible to declare the block variable and assigns the value to it. The variable name must be unique within the code block scope.
#var aRetVal := Integer new:0.
Conditional branching
ELENA like Smalltalk does not support any special language constructs to implement the conditional branching. Instead special Boolean symbols (system’true and system’false) are used. All conditional operations should return these symbols as a result.
There are three branching methods : then[1] , then&else[2], else[1]
(m == 0) then: [ n + 1 ] &else: [ m + n ].
Note that code in square brackets are in fact nested action classes ( an action class is a class supporting evaluate message). So this code is can be written in this form:
(m == 0) then: { eval [ ^ n + 1. ] } &else: { eval [ ^ m + n. ] }.
This expression can be written using special operators
(m == 0) ? [ n + 1 ] ! [ m + n ].
Note: the main difference between using explicit messages and conditional operators is that the compiler may optimize the resulting code in the later case.
We could omit true or else part
(m == 0) ! [ m / n ].
Boolean symbols supports basic logical operations (AND, OR, XOR and NOT), so several conditions can be checked
(aChar >= 48) and:(aChar < 58) ? [ theToken += aChar. ] ! [ #throw Exception new:"Invalid expression". ]
Note that in this case both condition will be evaluated even if the first one is false If we want to use short-circuit evaluation expression brackets should be used
(x >= 0)and:[ array@x != 0] ? [ ... ]
A switch statement can be implemented using => operator
^ aBulls => -1 ? [ consoleEx writeLine:"Not a valid guess.". ^ true. ] 4 ? [ consoleEx writeLine:"Congratulations! You have won!". ^ false. ] ! [ theAttempt += 1. consoleEx writeLine: "Your Score is " : aBulls : " bulls and " : aCows : " cows". ^ true. ].
Subcategories
This category has the following 3 subcategories, out of 3 total.
@
- Elena examples needing attention (empty)
- Elena Implementations (1 P)
- Elena User (2 P)
Pages in category "Elena"
The following 200 pages are in this category, out of 241 total.
(previous page) (next page)2
A
- A+B
- ABC problem
- Abstract type
- Abundant, deficient and perfect number classifications
- Accumulator factory
- Ackermann function
- Add a variable to a class instance at runtime
- AKS test for primes
- Amb
- Amicable pairs
- Anagrams
- Anonymous recursion
- Apply a callback to an array
- Arithmetic evaluation
- Arithmetic/Integer
- Array concatenation
- Array length
- Arrays
- Associative array/Creation
- Associative array/Iteration
- Averages/Arithmetic mean
- Averages/Median
- Averages/Mode
- Averages/Root mean square
- Averages/Simple moving average
B
C
- Caesar cipher
- Calendar - for "REAL" programmers
- Call a function
- Call an object method
- Case-sensitivity of identifiers
- Catamorphism
- Character codes
- Check that file exists
- Classes
- Closures/Value capture
- Collections
- Combinations
- Command-line arguments
- Comments
- Compare a list of strings
- Compound data type
- Conway's Game of Life
- Copy a string
- Create a file
- Create a two-dimensional array at runtime
D
E
F
G
H
I
J
L
M
P
R
- Random number generator (included)
- Random numbers
- Read a file line by line
- Real constants and functions
- Reflection/List methods
- Reflection/List properties
- Remove duplicate elements
- Repeat a string
- Respond to an unknown method call
- Return multiple values
- Reverse a string
- Reverse words in a string
- Roman numerals/Decode
- Roman numerals/Encode
- Rot-13
- Run-length encoding
- Runtime evaluation
- Runtime evaluation/In an environment
S
- Search a list
- Search a list of records
- Send an unknown method call
- Sequence of primes by trial division
- Set of real numbers
- Short-circuit evaluation
- Simple windowed application
- Singleton
- Singly-linked list/Element definition
- Singly-linked list/Element insertion
- Singly-linked list/Traversal
- Sleep
- Sockets
- Sort an array of composite structures
- Sort an integer array
- Sort disjoint sublist
- Sort three variables
- Sort using a custom comparator
- Sorting algorithms/Bogosort
- Sorting algorithms/Bubble sort
- Sorting algorithms/Cocktail sort
- Sorting algorithms/Comb sort
- Sorting algorithms/Counting sort
- Sorting algorithms/Gnome sort
- Sorting algorithms/Insertion sort
- Sorting algorithms/Pancake sort
- Sorting algorithms/Quicksort
- Sorting algorithms/Selection sort
- Sorting algorithms/Sleep sort
- Sorting algorithms/Stooge sort
- Stack
- Stack traces
- String append
- String case
- String comparison
- String concatenation
- String interpolation (included)
- String length
- String matching
- String prepend
- Strip a set of characters from a string
- Strip whitespace from a string/Top and tail
- Substring
- Substring/Top and tail