-
Notifications
You must be signed in to change notification settings - Fork 0
Syntax of sNEK
There are 8 main basic expressions in sNEK:
- "Atoms": Strings, Numbers, Booleans, Variable references
- Function Definitions
- Function Calls
- Arithmetic operations (+,-,*, <, > , =)
- Scope Declarations
- Set expressions
- While loops
- Conditionals
A .snek
file can be any combination/collection of any of these expressions.
There are three main primitive values currently supported by sNEK:
-
strings - series of symbols surrounded by single quotes. (Ex:
'Im a string'
) - booleans - true and false.
- integers - 64-bit integers.
Lastly, variable references can return any of the above types.
A program consisting of just Atoms is completely valid in sNEK! It's just not meaningful.
Function Definitions are of the format:
(def func_name ((var1: typeOfVar) (var2:typeofVar) : return_type ...)
expr1
....
exprn
)
where expr1
to exprn
is a basic sNEK expression as listed above.
return_type
can be replaced with the keyword void
to signify that a function has no intended return value.
Here's an example of a function that adds all integers from 0 to an integer n (inclusive):
(def sum (n:int) : int
(
if (= n 0) 0 (+ n (sum (dec n)))
)
)
To create a function that has no arguments, then it must follow the format:
( def no_args void : return_type
expr1
....
exprn
)
Function calls in sNEK are very similar to most other MLs, and of the format:
(func_name arg1 arg2 .... )
To use a binary operator - one of the following: (+,-,*, <, > , =) - format it in prefix notation.
For example, say we'd like to check whether the value that a
holds is equal to b
. If so, we'll write: (= a b)
. This expression evaluates to the boolean true
if a
and b
are equal, and false
if they're not.
To create a new scope for a set of variables, we write the following:
( let ((var1:typeOfVar1) (var2:typeOfVar2) .... )
expr1
....
exprn
)
where expr1
to exprn
is a basic sNEK expression as listed above.
In the following scope, the variables var1
, var1
,etc. are only accesible to the expressions in the second line and before the last parenthesis.
If var1
shares the same name with a variable outside this scope, the expressions expr1
to exprn
will prioritize referring to var1
in the inner-most scope.
**Note: ** exprn
cannot be a function definition, or a type error will be thrown.
A set
expression allows us to change the value of a variable.
A set
expression if of the format (set varName expression)
where expression
is one the basic sNEK expressions listed above.
(set var 10)
changes the value that the variable var
is holding to be the integer 10.
Note: if var
was declared to be of a certain type, a set
expression must set the var
to be of a value of the same type it was declared as.
While loops are of the format:
( while (some_boolean_expression)
expr1
...
exprn
)
where expr1
to exprn
is a basic sNEK expression as listed above.
A while-loop executes as such:
- Execute condition. Check condition result.
- If result is
true
, executeexpr1
toexprn
sequentially. If result isfalse
, end the loop. - Repeat 1.
**Note: ** exprn
cannot be a function definition, or a type error will be thrown.
Conditionals are of the format:
( if (some_boolean_expression)
exprTrue
exprFalse
)
where expr1
and expr2
are basic sNEK expressions as listed above.
exprTrue
will only execute if some_boolean_expression
evaluates to true
.
Else, exprFalse
is executed.