Language Reference
1. Lexical Conventions
1.1. Instructions
Instructions are terminated by the end of line. The end of line is ignored when something more is expected, e.g. a line terminated with ( , : = + - * ...
1.2. Comments
Comments start with two slashes and finish at the end of the line.
// This is a comment
1.3. Identifiers
An identifier is a sequence of letters, digits and underscores starting with a letter or underscore. Identifiers are case sensitives.
hello_world hello123 helloWorld HelloWorld _hello
1.4. Keywords
List of reserved keywords:
and attr break class cond do else elsif end false function if method nil not or return true var while yield
2. Expressions
2.1. Literals
Integers
An integer is made of one or more digits starting with an optional minus sign.Strings
"abc"
A string can includes special escaped characters:
Character | Description |
---|---|
\a | Bell (7) |
\b | Backspace (8) |
\f | Form Feed (12) |
\n | Line Feed (10) |
\r | Cariage Return (13) |
\t | Tab (9) |
\v | Vertical Tab (11) |
\w | Whitespace (32) |
\' | single quote |
\" | double quotes |
Characters
- $ followed by a regular character
- \ followed by a character:
Character Description \a Bell (7) \b Backspace (8) \f Form Feed (12) \n Line Feed (10) \r Cariage Return (13) \t Tab (9) \v Vertical Tab (11) \w Whitespace (32) - \ followed by an integer value corresponding to the UCS-2 character value.
Booleans
true false
Nil
nil
nil is the unique instance of the Nil class. It is a special value that means nothing. Instance variables are initialized with this value and some methods such as search functions can return nil when an object is not found.
Function References
ref functionName
2.2. Functions, variables and parameters
Variables, parameters and functions without arguments are evaluated by using its name:
name
Evaluating a function with parameters:
name (arg1, arg2, ...)
A function can have an optional block in addition to others parameters:
name (arg1, arg2, ...) do instruction1 instruction2 ... end
2.3. Messages
A message is sent to an object (the receiver) by appending its name:
receiver name
Sending a message with parameters:
receiver name (arg1, arg2, ...)
A message can have an optional block in addition to others parameters:
receiver name (arg1, arg2, ...) do instruction1 instruction2 ... end
2.4. Operators
Operators are just syntactic sugar to message sending. For instance, x + y is strictly equivalent to x _add (y), i.e. sending the message _add to x with argument 'y'.
This is the list of operators from the highest precedence to the lowest, with their respective message names:
Operator | Message | Description |
---|---|---|
obj1 [ obj2 ] | _at | Index |
not obj | _not | Logical negation |
- obj | _neg | Unary minus |
obj1 * obj2 | _mul | Multiplication |
obj1 / obj2 | _div | Division |
obj1 % obj2 | _mod | Modulus |
obj1 + obj2 | _add | Addition |
obj1 - obj2 | _sub | Substraction |
obj1 << obj2 | _shl | Bitwise shift left |
obj1 >> obj2 | _shr | Bitwise shift right |
obj1 < obj2 | _lt | Less than |
obj1 > obj2 | _gt | Greater than |
obj1 <= obj2 | _le | Less than or equal to |
obj1 >= obj2 | _ge | Greater than or equal to |
obj1 == obj2 | _eq | Equal to |
obj1 <> obj2 | _ne | Not equal to |
obj1 & obj2 | _and | Bitwise and |
obj1 ^ obj2 | _xor | Bitwise exclusive or |
obj1 | obj2 | _or | Bitwise or |
2.5. Other
And
cond1 and cond2
cond1 and cond2 are boolean expressions. The expression is true if and only if both expressions are true. The second expression is evaluated only if the first expression is true.
Or
cond1 or cond2
cond1 and cond2 are boolean expressions. The expression is true if one of the expressions is true. The second expression is evaluated only if the first expression is false.
Conditional Operator
cond1 cond expr1 else expr2
cond1 is a boolean expression. If cond1 is true, the value of the expression is expr1, otherwise it is expr2.
3. Instructions
3.1. Expression
expression
Evaluates the expression.
3.2. Assign
leftExpression = rightExpression
Assigns an expression to a variable.
The right expression can be anything, but the left expression must be a local variable or an instance variable.
3.3. Defining a Local Variable
var name = expression
Declares a variable and sets its initial value.
A variable cannot be used before it is defined.
3.4. If
if cond1 ... elsif cond2 ... elsif ... ... else ... end
Where:
- cond1, cond2 are boolean expressions.
- elsif and else statements are optionals.
3.5. While
while cond ... end
Repeat the block as long as the boolean expression cond evaluates to true.
3.6. Break
break
The break statement can interrupt a while loop.
3.7. Return
return return expr
Returns a value from a function or a method.
3.8. Yield
yield yield e1 yield e1, e2, ..., en
This statement evaluates the block passed to the function or the method. The arguments of the yield are passed to the block as parameters. If there is more than one yield, all occurrences must have the same number of arguments.
function loop (n) var i = 1 while i <= n yield i i = i + 1 end function main var sum = 0 loop (10) do x sum = sum + x end
4. Functions
function name instr1 instr2 ... end function name (param1, param2, ...) instr1 instr2 ... end
Defines a function with or without parameters.
The function declaration is followed by the list of instructions to execute.
5. Classes
A class is a record of one or more values (the instance variables) and associates functions (the methods) to it.
class name attr instname ... method metname (param1, param2, ...) instr1 instr2 ... end end
Defines a class with instance variables and methods.
The class ends when a new class or a function is declared, there is no keyword or punctuation sign to mark the end of a class.
5.1. Instance Variables
attr name
An instance variable is accessed by sending a message to an object:
anObject name
Change the value with a regular assign:
anObject name = value
5.2. Methods
method name instr1 instr2 ... end method name (param1, param2, ...) instr1 instr2 ... end
Defines a method with or without parameters.
The method declaration is followed by the list of instructions to execute. The list of instructions ends when a new declaration starts, there is no keyword or punctuation sign to mark the end of a method.
Methods are like functions but are evaluated by sending a message to a receiver. The receiver will be assigned to the implicit parameter 'self'.
5.3. Creating a New Instance
ClassName basicNew
Creates a new instance of ClassName with all instance variables initialized to nil.