-
Notifications
You must be signed in to change notification settings - Fork 24
Syntax
- Variables
- Comments
- Scope
- Object Initializer
- Array Initializer
- String Escape Sequences
- Constants
- Keywords
Variables can defined with the var
statement.
var a;
You can initialize variables with a value in the statement as well.
var a = 123;
Multiple variables can be defined in a single statement.
var a = 123, b;
Read-only variables can be defined with const
. These must have initializers.
const x = 10;
Comments can be included in code in two ways.
Prefixing text with //
will tell the compiler to ignore the rest of the line:
// this is ignored
var a = 1; // this text is also ignored (but not the var!)
Block comments can span multiple lines and are created by surrounding text with /*
and */
.
/* this is a ignored
var a = 0; still ignored
the line below isn't ignored */
var a = 0;
These can also be used to create a comment in the middle of a line.
var a /* declare a */ = 0;
Block comments also support nesting.
/* the following line is ignored
var a /* declare a */ = 0; */
Mond uses block-level lexical scoping for named functions/sequences and variables declared with var
/const
.
var a = 1;
{
// curly braces create blocks
var b = 2;
print(b); // prints 2
//var a = 5; // 'a' is already defined, will error
}
print(a); // prints 1
//print(b); // 'b' is not defined, will error
Objects can be created in expressions like this:
var obj = { a: 1, b: 2 };
print(obj.a); // prints 1
The value name can be left out if you have the value in a variable:
var x = 15, y = 25;
var obj = { x, y };
print(obj.y); // prints 25
A trailing comma is allowed if you have at least one value.
Arrays can be created in expressions like this:
var array = [1, 2, 3];
print(array[1]); // prints 2
A trailing comma is allowed if you have at least one value.
Special characters can be included in strings by using escape sequences. To use an escape sequence, prefix the Format string from the following table with \
.
Format | Output |
---|---|
\ |
\ |
/ |
/ |
" |
" |
' |
' |
b |
Backspace |
f |
Form Feed |
n |
Newline |
r |
Carriage Return |
t |
Horizontal Tab |
uXXXX |
Unicode character from hexadecimal value XXXX
|
Constants can be defined exactly like [[var
|Syntax#variables]] but they use the const
keyword instead of var
.
The following constants can be referenced from scripts:
- [[
global
|Globals]] undefined
null
true
false
NaN
Infinity
Keywords are reserved words that can not be used as identifiers. Mond reserves the following as keywords:
var
const
fun
return
seq
yield
if
else
for
foreach
in
while
do
break
continue
switch
case
default
debugger
global
undefined
null
true
false
NaN
Infinity