-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar
53 lines (49 loc) · 1.9 KB
/
grammar
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
program := statement*
;; Statements
statement := if_stmnt |
while_stmnt |
for_stmnt |
class_def |
func_def |
block_stmnt |
print_stmnt |
switch_stmnt |
flow_stmnt |
do_while_stmnt |
import_stmnt |
new_assign_stmnt|
reassign_stmnt |
expr_stmnt
if_stmnt := "if" "(" or_expr ")" block_stmnt ("elif" "(" or_expr ")" block_stmnt)* ( "else" block_stmnt)?
while_stmnt := "while" "(" or_expr ")" block_stmnt
class_def := "class" ID "{" func_def* "}"
func_def := "func" ID "(" ID? ("," ID)* ")" "{" statement* "}"
block_stmnt := "{" statement* "}"
print_stmnt := "print" or_expr ";"
switch_stmnt := "switch" "(" or_expr ")" ("case" or_expr ":" block_stmnt)* "default" ":" block_stmnt
flow_stmnt := ("break" | "continue" | "return" ( or_expr )? ) ";"
do_while_stmnt := "do" block_stmnt "while" "(" or_expr ")"
import_stmnt := "import" ID ("as" ID)? ";"
new_assign_stmnt := "let" access "=" expr_stmnt
reassign_stmnt := access ( "=" | "+=" | "-=") expr_stmnt
expr_stmnt := or_expr ";"
;; Expressions
assign_expr := or_expr (( '=' | "+=" | "-=" | "*=" | "/=" ) or_expr ) ';'
or_expr := and_expr ("||" and_expr)*
and_expr := eq_expr ("&&" eq_expr)*
eq_expr := rel_expr ( ("=="|"!=") rel_expr)*
rel_expr := add_expr ( ("<" | ">" | "<=" | ">=") add_expr)*
add_expr := term (("+" | "-") term)*
term := factor (("*" | "/") factor)*
factor := access | "(" or_expr ")" | ("!" | "-") or_expr | atom
access = (ID | "this" "." ID) (ID | "[" or_expr "]" | "(" arglist ")")*
arglist := (or_expr ("," or_expr)*)?
atom := | NUM | FLOAT | STR | BOOL | ARRAY | "nil"
;; Primitives
BOOL := "false" | "true"
ID := LETTER (LETTER | NUM | "_" | "$" | "@")*
ARRAY := "[" ( or_expr ( "," or_expr )* )? "]"
STR := "\"" any character* "\""
NUM := DIGIT (DIGIT)*
FLOAT := DIGIT (DIGIT)* "." (DIGIT)*
DIGIT := 1 | 2 | 3| 4 | 5 | 6 | 7 | 8 | 9 | 0