-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar
85 lines (64 loc) · 1.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
statements: statement+
statement:
| compound_stmt
| simple_stmts
simple_stmts: simple_stmt NEWLINE
simple_stmt:
| declaration
| assignment
| expression
| return_stmt
compound_stmt:
| function_def
| if_stmt
| while_stmt
| match_stmt
declaration: 'let' ID ['=' expression]
assignment: ID '=' expression
function_def: 'fn' ID '(' [ID (',' | ID)*] ')' ':' block
if_stmt:
| 'if' expression ':' block elif_stmt
| 'if' expression ':' block [else_stmt]
elif_stmt:
| 'elif' expression ':' block elif_stmt
| 'elif' expression ':' block [else_stmt]
else_stmt: 'else' ':' block
while_stmt: 'while' expression: block
match_stmt: 'match' expression ':' NEWLINE INDENT (match)+ DEDENT
match: '|' (expression | '*') '->' expression
block: NEWLINE INDENT statements DEDENT
return_stmt: `return` expression
expression:
| term
| expression '+' term
| expression '-' term
| expression '>' term
| expression '>=' term
| expression '<' term
| expression '<=' term
| expression '==' term
| '!' term // NOT IMPLEMENTED YET
| expression 'or' term
| expression 'and' term
term:
| factor
| term '*' factor
| term '/' factor
| term '//' factor
| term '%' factor
factor:
| primary
| '+' factor
| '-' factor
primary:
| atom
| ID '(' [expression (',' | expression)*] ')'
atom:
| ID
| INTEGER
| FLOAT
| STRING
| 'true'
| 'false'
| None // NOT IMPLEMENTED YET
| '(' expression ')'