Category:Elena: Difference between revisions

Content added Content deleted
No edit summary
Line 57: Line 57:
Several parameters can be passed in the message as well:
Several parameters can be passed in the message as well:


control foreach: (1,2,3) &do:printingLn.
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:
The generic message can have several parameters as well:
Line 89: Line 87:
class DerivedClass :: BaseClass
class DerivedClass :: BaseClass
{
{
constructor new &field1:aField2 &field2:aField2
constructor new field1:aField2 field2:aField2
[
[
theField1 := aField1.
theField1 := aField1.
Line 95: Line 93:
]
]
add &field1:aField2 &field2:aField2
add field1:aField2 field2:aField2
= MyClass new &Field1:(theField1 + aField1) &Field2:(theField2 + 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)
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
var anObject := DerivedClass new field1:1 field2:1. // DerivedClass is a symbol


Singletons cannot have constructors and their symbols can be used directly
Singletons cannot have constructors and their symbols can be used directly
Line 108: Line 106:
{
{
sumOf:a:b
sumOf:a:b
= a add &field1:b &field2:a.
= a add field1:b field2:a.
}
}
Line 117: Line 115:
In general the symbol is a named expression and can be used to declare initialized objects, constants, reusable expressions and so on.
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.
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.
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.
static SingletonClass = DerivedClass new field:0 field:0.


== Code blocks ==
== Code blocks ==
Line 127: Line 125:
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.


printAckermann &n:n &m:m
printAckermann n:n m:m
[
[
control forrange &int:0 &int:n &do: (&int:i)
control forrange int:0 int:n do: (&int:i)
[
[
control forrange &int:0 &int:m &do: (&int:j)
control forrange int:0 int:m do: (&int:j)
[
[
...
...
console writeLine.
console writeLine
].
].
].
].
Line 145: Line 143:
...
...
^ aRetVal / anArray length.
^ aRetVal / anArray length
]
]


Line 151: Line 149:


Number = convertor toReal:theValue.
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.
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.
Line 196: Line 188:


(m == 0)
(m == 0)
? [ n + 1 ]
? [ ^n + 1 ]
! [ m + n ].
! [ ^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.
Note: the main difference between using explicit messages and conditional operators is that the compiler may optimize the resulting code in the later case.
Line 204: Line 196:


(m == 0)
(m == 0)
! [ m / n ].
! [ ^m / n ].


It is possible to use *if* template code :
It is possible to use *if* template code :
Line 218: Line 210:
if (aChar >= 48) and:(aChar < 58)
if (aChar >= 48) and:(aChar < 58)
[
[
theToken += aChar.
theToken append:aChar.
];
];
[
[
Exception new:"Invalid expression" raise.
Exception new:"Invalid expression"; raise.
]
]


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
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
if ((x >= 0)and:[ array@x != 0])
if ((x >= 0)and:$(array@x != 0))
[
[
...
...
Line 232: Line 224:
A switch statement can be implemented using => operator
A switch statement can be implemented using => operator


^ aBulls =>
aBulls =>
-1 [ consoleEx writeLine:"Not a valid guess.". ^ true. ];
-1 [ consoleEx writeLine:"Not a valid guess.". ^ true ];
4 [
4 [
consoleEx writeLine:"Congratulations! You have won!".
consoleEx writeLine:"Congratulations! You have won!".
^ false.
^ false
];
];
! [
! [
theAttempt += 1.
theAttempt append:1.
consoleEx writeLine:
consoleEx writeLine:
Line 245: Line 237:
: " bulls and " : aCows : " cows".
: " bulls and " : aCows : " cows".
^ true.
^ true
].
].