Keywords & grammar

Reserved words

These identifiers are reserved by the lexer and can't be used as variable, function, struct or parameter names:

KeywordUsed for
letMutable variable declaration
constImmutable variable declaration
if / elseif / elseConditional branches (note: elseif, one word)
whileLoop while a condition holds
foreachLoop over an array, map or string
break / continueLoop control (only valid inside a loop body)
returnReturn from a function
funcIntroduces a function declaration, paired with define
defineIntroduces a function's parameter list and body
structStruct declaration
nullThe null literal
true / falseBoolean literals
output, request, typeof, assert and require are not keywords -they're ordinary identifiers that happen to be pre-bound to built-in functions in every scope. See the built-ins reference.

Comments

Only single-line // comments exist -there's no /* block */ form.

Literal forms

FormExample
Number42, 3.14 -base-10 only, no hex/octal/binary/underscored digits
String"text" -double quotes only, see escape sequences
Booleantrue, false
Nullnull
Array[1, 2, 3]
Map{ key: value } -literal keys must be bare identifiers
Tuple(1, 2), one-element (1,), empty (,,)
Functiondefine(a, b) { ... }

Grammar summary

An informal summary, not a formal EBNF grammar -see the parser source for the exact rules.

program     ::= statement*

statement   ::= letDecl | constDecl | funcDecl | structDecl
              | ifStmt | whileStmt | foreachStmt
              | breakStmt | continueStmt | returnStmt
              | exprStmt | block

letDecl     ::= "let" (IDENTIFIER | destructurePattern) "=" expression ";"
constDecl   ::= "const" (IDENTIFIER | destructurePattern) "=" expression ";"
funcDecl    ::= "func" IDENTIFIER "=" "define" "(" params? ")" block
structDecl  ::= "struct" IDENTIFIER "{" (field | funcDecl)* "}"
field       ::= IDENTIFIER ("=" expression)? ";"
params      ::= param ("," param)*
param       ::= IDENTIFIER ("=" expression)?

ifStmt      ::= "if" "(" expression ")" block
                ("elseif" "(" expression ")" block)*
                ("else" block)?
whileStmt   ::= "while" "(" expression ")" block
foreachStmt ::= "foreach" "(" IDENTIFIER ":" expression ")" block
returnStmt  ::= "return" expression? ";"
block       ::= "{" statement* "}"

destructurePattern ::= "(" IDENTIFIER "," (IDENTIFIER ",")* ")"
                      | "(" IDENTIFIER ",)"   // single-element form

expression  ::= assignment
assignment  ::= (call ".")? IDENTIFIER ("=" | "+=" | "-=" | "*=" | "/=" | "%=") assignment
              | IDENTIFIER ("++" | "--")
              | logicalOr
logicalOr   ::= logicalAnd ("||" logicalAnd)*
logicalAnd  ::= equality ("&&" equality)*
equality    ::= comparison (("==" | "!=") comparison)*
comparison  ::= term (("<" | "<=" | ">" | ">=") term)*
term        ::= factor (("+" | "-") factor)*
factor      ::= unary (("*" | "/" | "%") unary)*
unary       ::= ("!" | "-") unary | call
call        ::= primary ("(" args? ")" | "." IDENTIFIER | "[" expression "]")*
primary     ::= NUMBER | STRING | BOOLEAN | "null" | IDENTIFIER
              | "define" "(" params? ")" block
              | "[" (expression ("," expression)*)? "]"
              | "{" (IDENTIFIER ":" expression ("," IDENTIFIER ":" expression)*)? "}"
              | "(" tupleOrGroup ")"