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 ( , : = + - * ...
Comments start with two slashes and finish at the end of the line.
// This is a comment
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
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
"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 |
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) |
true false
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.
ref functionName
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
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
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 |
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.
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.
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.
expression
Evaluates the expression.
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.
var name = expression
Declares a variable and sets its initial value.
A variable cannot be used before it is defined.
if cond1 ... elsif cond2 ... elsif ... ... else ... end
Where:
while cond ... end
Repeat the block as long as the boolean expression cond evaluates to true.
break
The break statement can interrupt a while loop.
return return expr
Returns a value from a function or a method.
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
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.
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.
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
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'.
ClassName basicNew
Creates a new instance of ClassName with all instance variables initialized to nil.