Syntax Reference
This page summarizes the currently implemented syntax.
Program
program ::= statement*
Variable declarations
declaration ::= "let" identifier (":" type)? "=" expression ";"
| "mut" identifier (":" type)? "=" expression ";"
Assignment
assignment ::= identifier "=" expression ";"
Types
type ::= "int"
| "float"
| "bool"
| "string"
| "void"
| "[" type "]"
Imports
import_statement ::= "import" string "as" identifier ";"
An import is resolved relative to the file that contains it. The .vsp
extension is optional, and math and ./math both resolve to a sibling
math.vsp when written inside the same directory.
If statement
if_statement ::= "if" "(" expression ")" "{"
statement*
"}"
("else" "{"
statement*
"}")?
While statement
while_statement ::= "while" "(" expression ")" "{"
statement*
"}"
Function
function ::= ("export")? ("extern")? "fn" identifier "(" parameter_list? ")"
":" type (";" | "{" statement* "}")
parameter_list ::= parameter ("," parameter)*
parameter ::= identifier ":" type
("=" expression)?
Return
return_statement ::= "return" expression? ";"
Expressions
Conceptually, expression precedence is:
expression
→ comparison
→ addition
→ multiplication
→ unary
→ exponentiation
→ postfix
→ primary
Postfix expressions include calls and indexing.
Primary expressions
primary ::= integer
| float
| boolean
| string
| identifier
| array
| "(" expression ")"
Array
array ::= "[" (expression ("," expression)*)? "]"
Function call
call ::= expression "(" (expression ("," expression)*)? ")"
Index
index ::= expression "[" expression "]"
Member access
member ::= expression "." identifier
Module functions can be called through an imported alias:
import "math" as m;
let result = m.square(5.0);