-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.ebnf
40 lines (33 loc) · 1.68 KB
/
grammar.ebnf
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
(* Funcy Reference Grammar *)
module = { incl }, { stmt_func }, EOF ;
incl = "include", LITERAL_STR, ";" ;
stmt = (
stmt_func | stmt_block | stmt_if | stmt_while | stmt_nop | stmt_let |
stmt_return | stmt_break | stmt_continue | stmt_expr
) ;
stmt_func = "func", IDENTIFIER, "(", [ decl, { ",", decl } ], ")", stmt_block ;
stmt_block = "{", { stmt }, "}" ;
stmt_if = "if", expr_paren, stmt, [ "else", stmt ] ;
stmt_while = "while", expr_paren, stmt ;
stmt_nop = ";" ;
stmt_let = "let", decl, [ "=", expr ], ";" ;
stmt_return = "return", [ expr ], ";" ;
stmt_break = "break", ";" ;
stmt_continue = "continue", ";" ;
stmt_expr = expr, ";" ;
decl = [ "mut" ], IDENTIFIER ;
expr_paren = "(", expr, ")" ;
expr = expr_assignment ;
(* Expressions by increasing precedence level. *)
expr_assignment = expr_logical_or, [ ( "%=" | "&=" | "*=" | "+=" | "-=" | "/=" | "=" | "|=" ), expr_assignment ] ;
expr_logical_or = expr_logical_and, { "||", expr_logical_and } ;
expr_logical_and = expr_eager_or, { "&&", expr_eager_or } ;
expr_eager_or = expr_eager_and, { "|", expr_eager_and } ;
expr_eager_and = expr_equality, { "&", expr_equality } ;
expr_equality = expr_comparison, { ( "!=" | "==" ), expr_comparison } ;
expr_comparison = expr_sum, { ( "<" | "<=" | ">" | ">=" ), expr_sum } ;
expr_sum = expr_term, { ( "+" | "-" ), expr_term } ;
expr_term = expr_prefix, { ( "%" | "*" | "/" ), expr_prefix } ;
expr_prefix = ( "!" | "+" | "-" ), expr_prefix | expr_call ;
expr_call = expr_primary, { "(", [ expr, { ",", expr } ], ")" } ;
expr_primary = expr_paren | LITERAL_INT | LITERAL_CHR | LITERAL_STR | IDENTIFIER | "false" | "true" ;