-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.txt
36 lines (32 loc) · 1.52 KB
/
syntax.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
program: declaration*
declaration: varDeclaration | funDeclaration | statement
varDeclaration: "var" IDENTIFIER ("=" expression)? ";"
funDeclaration: "fun" function
function: IDENTIFIER "(" parameters? ")" blockStatement
parameters: IDENTIFIER ("," IDENTIFIER)*
statement: expressionStatement | printStatement | blockStatement | ifStatement | whileStatement | forStatement
expressionStatement: expression ";"
printStatement: "print" expression ";"
blockStatement: "{" declaration* "}"
ifStatement: "if" "(" expression ")" statement ("else" statement)?
whileStatement: "while" "(" expression ")" statement
forStatement: "for" "(" (varDeclaration | expressionStatement | ";") expression? ";" expression ")" statement
breakStatement: "break" ";"
continueStatement: "continue" ";"
expression: comma | lambda
lambda: "fun" "(" parameters? ")" blockStatement
comma: comma "," assignment | assignment
assignment: IDENTIFIER "=" assignment | ternary | logicalOr
ternary: expression "?" expression ":" expression
logicalOr: logicalAnd ("or" logicalAnd)*
logicalAnd: equality ("and" equality)*
equality: comparison (("!=" | "==" ) comparison)*
comparison: term (("<" | ">" | "<=" | ">=") term)*
term: factor (("-" | "+") factor)*
factor: unary (("*" | "/") unary)*
unary: ("-" | "!") unary | call
call: primary ("(" arguments? ")")* | array
array: primary "[" expression "]"
arguments: expression ("," expression)*
primary: STRING | NUMBER | "true" | "false" | "nil" | IDENTIFIER | "(" expression ")" | "[" arrayElements? "]"
arrayElements: primary ("," primary)*