Control Flow
if
/else
if
statements are written using a ?
character, and else
is written as
,,
. Blocks are enclosed in curly brackets.
else if
can be written using ,, ?
.
? x < \ {
"x is negative"!;
} ,, ? x > \ {
"x is positive"!;
} ,, {
"x = 0"!;
}
foreach
loop
foreach
loops are written using ...
, and enclosed in curly brackets.
They must be paired with a ->?
operator, indicating the object to iterate over.
arr: [];
... char ->? "string" {
arr+: [char];
}
== arr :: ["s", "t", "r", "i", "n", "g"]
Comprehensions
Array Comprehensions
Array comprehensions are a way to create an array based on another iterable. Uses may include performing an operation on each item of the iterable, or creating a subsequence of those items that satisfy a certain condition.
They are written similarly to foreach
loops; they can come in two forms,
as follows:
[expression ... member ->? iterable]
[expression ... member ->? iterable ? condition]
For example, say we want to create an array of square numbers. Here are two equivalent approaches:
input: [/, /\, //, /\\, /\/];
== Approach 1
arr: [];
... n ->? input {
arr+: [n ++ n];
}
== Approach 2
arr: [n ++ n ... n ->? input];
In both cases, arr
is equal to [1, 4, 9, 16, 25]
.
Now suppose we want to filter this result to only the odd-numbered items. There are again two equivalent approaches:
arr: [/, /\\, /\\/, /\\\\, //\\/];
== Approach 1
filtered: [];
... n ->? arr {
? n --- /\ :: / {
filtered+: [n];
}
}
== Approach 2
filtered: [n ... n ->? arr ? n --- /\ :: /];
In both cases, filtered
is equal to [1, 9, 25]
.
Table Comprehensions
Table comprehensions have a similar syntax to array comprehensions:
{{key -> value ... member ->? iterable}}
{{key -> value ... member ->? iterable ? condition}}
For example, both of the following approaches are equivalent:
== Approach 1
tab: {{}};
... x ->? [/\, /\\, //\] {
tab<<x>>: x ++ x;
}
== Approach 2
tab: {{x -> x ++ x ... x ->? [/\, /\\, //\]}};
In both cases, tab
is equal to {{2 -> 4, 4 -> 16, 6 -> 36}}
.
while
loop
while
loops are written with ..
, and enclosed in curly brackets.
The loop condition follows the ..
.
An infinite loop is created when no condition is given.
x: \;
.. x < /\/\ {
x+: /\;
x!;
}
== prints 2, 4, 6, 8, 10
break
/continue
break
statements are written with <-
, and terminate the enclosing loop
immediately. They can be used in both foreach
and while
loops.
x: \;
.. x < /\/ {
x+: /;
? x :: // { <- }
x!;
}
This program will print 1, 2, and then terminate the while
loop on the third
iteration, before printing 3.
continue
statements are written with ->
, and immediately finish the current
iteration of the enclosing loop. These can also be used in both for
and
while
loops.
x: \;
.. x < /\/ {
x+: /;
? x :: // { -> }
x!;
}
This program will print 1, 2, skip the third iteration of the while
loop,
then print 4, 5, and end the loop normally.
Note
Both <-
and ->
do not need a semicolon at the end of the statement
– it's optional.
try
/catch
try
-catch
statements are used for error handling.
try
clauses are written with ??
, and enclosed in curly brackets.
If, during execution of the contents of the try
clause, an error is thrown,
the rest of the clause is skipped, the error will be silenced, and the adjoining
catch
clause will be executed. catch
clauses are written with !!
, and are
also enclosed in curly brackets.
?? {
== error prone code here...
/ -- \;
"unreachable"!;
} !! {
"error caught"!;
}