-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar.txt
66 lines (48 loc) · 1.96 KB
/
grammar.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
statements : NEWLINE* statement (NEWLINE+ statement)* NEWLINE*
statement : KEYWORD:RETURN expr?
: KEYWORD:CONTINUE
: KEYWORD:BREAK
: expr
expr : IDENTIFIER EQ expr
: comp-expr ( ( KEYWORD:AND | KEYWORD:OR ) comp-expr )*
comp-expr : KEYWORD:NOT comp-expr
: arith-expr ( ( EE | LT | LTE | GT | GTE ) arith-expr )*
arith-expr : term ( (PLUS | MINUS) term )*
term : factor ( ( MUL | DIV ) factor )*
factor : ( PLUS | MINUS ) factor
: power
power : call-expr ( POW factor )*
call-expr : atom ( LPAREN ( expr ( COMMA expr )* )? RPAREN )?
atom : INT | FLOAT | STRING
: IDENTIFIER // variable access
: LPAREN expr RPAREN
: list-expr
: map-expr
: if-expr
: for-expr
: while-expr
: func-def
: index-expr
list-expr : LSQUARE ( expr ( COMMA expr )* )? RSQUARE
map-expr : LBRACE ( expr COLON expr (COMMA expr COLON expr)* )? RBRACE
if-expr : KEYWORD:IF expr KEYWORD:THEN
( statement elif-expr | else-expr?)
| ( NEWLINE statements KEYWORD:END | elif-expr | else-expr)
elif-expr : KEYWORD:ELIF expr KEYWORD:THEN
( statement elif-expr | else-expr?)
| ( NEWLINE statements KEYWORD:END | elif-expr | else-expr)
else-expr : KEYWORD:ELSE
statement
| (NEWLINE statements KEYWORD:END)
for-expr : KEYWORD:FOR IDENTIFIER EQ expr KEYWORD:TO expr
(KEYWORD:STEP expr)? KEYWORD:THEN
statement
| (NEWLINE statements KEYWORD:END)
while-expr : KEYWORD:WHILE expr KEYWORD:THEN
statement
| (NEWLINE statements KEYWORD:END)
func-def : KEYWORD:FUN IDENTIFIER?
LPAREN ( IDENTIFIER ( COMMA IDENTIFIER )* )? RPAREN
(ARROW expr)
| (NEWLINE statements KEYWORD:END)
index-expr : atom LSQUARE expr RSQUARE ( EQ expr )? // a[0]