Skip to content

Syntax of sNEK

Jose Guaro edited this page Oct 28, 2019 · 5 revisions

Syntax of sNEK

There are 8 main basic expressions in sNEK:

  1. "Atoms": Strings, Numbers, Booleans, Variable references
  2. Function Definitions
  3. Function Calls
  4. Arithmetic operations (+,-,*, <, > , =)
  5. Scope Declarations
  6. Set expressions
  7. While loops
  8. Conditionals

A .snek file can be any combination/collection of any of these expressions.

Atoms

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

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

Function calls in sNEK are very similar to most other MLs, and of the format:

(func_name arg1 arg2 .... )

Arithmetic Operations

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.

Scope Declarations

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.

Set Expressions

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

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:

  1. Execute condition. Check condition result.
  2. If result is true, execute expr1 to exprn sequentially. If result is false, end the loop.
  3. Repeat 1.

**Note: ** exprn cannot be a function definition, or a type error will be thrown.

Conditionals

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.