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:

CharacterDescription
\aBell (7)
\bBackspace (8)
\fForm Feed (12)
\nLine Feed (10)
\rCariage Return (13)
\tTab (9)
\vVertical Tab (11)
\wWhitespace (32)
\'single quote
\"double quotes

Characters

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:

OperatorMessageDescription
obj1 [ obj2 ]_atIndex
not obj_notLogical negation
- obj_negUnary minus
obj1 * obj2_mulMultiplication
obj1 / obj2_divDivision
obj1 % obj2_modModulus
obj1 + obj2_addAddition
obj1 - obj2_subSubstraction
obj1 << obj2_shlBitwise shift left
obj1 >> obj2_shrBitwise shift right
obj1 < obj2_ltLess than
obj1 > obj2_gtGreater than
obj1 <= obj2_leLess than or equal to
obj1 >= obj2_geGreater than or equal to
obj1 == obj2_eqEqual to
obj1 <> obj2_neNot equal to
obj1 & obj2_andBitwise and
obj1 ^ obj2_xorBitwise exclusive or
obj1 | obj2_orBitwise 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:

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.