Skip to content
Leonardo Laguna Ruiz edited this page Feb 27, 2017 · 12 revisions

Comments

// Line Comment

/* Block Comment */

/* /* Nested Comment */ */

Literals and builtin types

// Unit (or empty value)
val u : unit  = ();

// Integers
val x : int = 0;
val y : int = 1;
val z : int = 3942;

// Real (floating or fixed point)
val a : real = 1.0;
val b : real = 2.0;
val c : real = 3.1416;

// Booleans
val k : bool = true;
val l : bool = false;

Numbers without the decimal dot are integers and cannot be mixed with real numbers.

Operators

Operators require that both arguments are the same type.

Arithmetic (for int and real types)

- Addition       : '+'
- Subtraction    : '-'
- Multiplication : '*'
- Division       : '/'
- Modulo         : '%'

e.g.

val x = 1 + 2;
val z = -1.0; // Unary minus
val y = 3.8 * 56.0;

Logic operators (for bool type)

- And : '&&'
- Or  : '||'
- Not : 'not' // 'not' is a function

e.g.

val l = not(1.0 > 3.0) || false;

Relational operators (for int and real types)

- Equal            : '=='
- Unequal          : '<>'
- Less than        : '<'
- Greater than     : '>'
- Less than Eq.    : '<='
- Greater than Eq. : '>='

Expressions

If-expressions

If expressions require an else branch.

val x = if y > 0.0 then 3.0 else 4.0; 

Tuples

val x,y,z = 1,2,3;
val (x,y,z) : (int,int,int) = 1,2,3;
val point : (real,real,real) = 0.0, 1.0, 2.0;

Arrays

Array types are in the form array(type,size) e.g. array(int,3) it's an array of 3 integers.

val x = [1,2,4];
val y : array(real,3) = [1.0, 2.0, 4.0];

Function calls

Function calls are special in Vult since they create implicit context. These context can be anonymous or named.

val x = foo();     // anonymous context
val y = bob:foo(); // context with name 'bob'
val z = bob:foo(); // this call is performed in the context 'bob' 

Statements

Variable declarations

When declaring a variable you can either provide a type or let the type inference decide on the type.

val x = 0;          // The type is inferred to be 'int'
val y : real = 1.0; // The type is specified to be 'real'

Memory variables

The value of variable declarations is lost when the function returns. On the contrary, the value of memory variables in stored in the function context.

// mem variables can reference themselves and they are always initialized to zero
mem x = x + 1;

// you can specify the type as well
mem y : real = 0.0;

Assignments

Once a variable is declared you can change it's value with =. The type of a variable cannot be changed.

val x = 0;
x = 1;

// Assigning tuples

a,b,c = 0,1,2;

If you want to ignore a value you have to assign it to _.

val x,_ = foo(); // 'foo' returns a tuple
_ = bar();

If-statements

If-statements may not have else branch and if the body is a single statement curly braces are not required.

// simple if-statement
if(x > 0)
   return 0;

if(y > 0) {
   y = 0;
   x = 1;
}
else {
   y = 1;
   x = 2;
}

Function declarations and Return

Functions are defined with the keyword fun and return values with return.

fun add(a, b) {
   return a+b;
}

Functions may include type definitions.

fun add(a:int, b:int) : int {
   return a+b;
}

Functions that do not return any value have type unit.

fun foo() : unit {
}

Functions sharing context

When functions declare memory variables that need to be accessed from other functions they need to be linked with the word and. For example, when making a counter you may need a function to reset it.

fun counter() {
   mem x = x + 1;
   return x; 
}
and reset() {
   x = 0;
}

fun test() {
   val x = c:counter();
   _ = c:reset();
}

You can notice that it is not necessary to declare the memory variable x in the function reset.

Clone this wiki locally