-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.lark
47 lines (34 loc) · 1.09 KB
/
grammar.lark
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
%import common.WS
%import common.DIGIT
%import common.CNAME
%import common.NUMBER
%import common.ESCAPED_STRING
%import common.FLOAT
%import common.LETTER
LITERAL: NUMBER | ESCAPED_STRING | "true" | "false" | "'" /./ "'"
IDENTIFIER: CNAME
let_stmt: "let" IDENTIFIER IDENTIFIER "=" expr ";"
mut_stmt: "mut" IDENTIFIER "=" expr ";"
print_stmt: "print" expr ";"
!noop_stmt: "noop" ";"
!return_stmt: "return" expr ";"
func_def_stmt: "fn" IDENTIFIER "(" (IDENTIFIER IDENTIFIER)? ("," IDENTIFIER IDENTIFIER)* ")" ":" IDENTIFIER block
!?sum_expr: sum_expr "+" prod_expr
| sum_expr "-" prod_expr
| prod_expr
!?prod_expr: prod_expr "*" cast_expr
| prod_expr "/" cast_expr
| prod_expr "%" cast_expr
| cast_expr
?cast_expr: "cast<" IDENTIFIER ">" primary
| primary
?primary: LITERAL
| "-" primary -> unary_minus
| "!" primary -> unary_negation
| IDENTIFIER -> read_var
| "(" sum_expr ")"
!expr: sum_expr | LITERAL
?statement: print_stmt | let_stmt | mut_stmt | noop_stmt | return_stmt
!block: "{" statement* "}"
program: func_def_stmt+
%ignore WS