-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
95 lines (72 loc) · 1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Statement : Statements
| LetStatement // let a = 3;
| ReturnStatement // return a;
| ExpressionStatement // 5 + 5;
| AssignStatement // a = 4;
| BlockStatement // { let b = 4; a + b; }
| /* nothing */
;
Statements : Statement Statements
|
;
LetStatement : "let" Identifier "=" Expression
;
ReturnStatement : "return" Expression ";"
;
ExpressionStatement : Expression ";"
;
AssignStatement : Identifier "=" Expression
| Identifier "+=" Expression
| Identifier "-=" Expression
| Identifier "*=" Expression
| Identifier "/=" Expression
;
BlockStatement : "{" Statements "}"
;
Expression : NULLLITERAL
| BOOLEAN
| INTEGERLITERAL
| FLOATLITERAL
| STRINGLITERAL
| Identifier
| HashLiteral
| ArrayLiteral
| IndexExpression
| FunctionLiteral
| CallExpression
| IfExpression
| WhileExpression
| PrefixExpression
| InfixExpression
;
Expressions : Expression Expressions
|
;
Identifiers : Identifier Identifiers
|
;
HashLiteral : "{" Pairs "}"
;
Pairs : Pair Pairs
|
;
Pair : Expression ":" Expression
;
ArrayLiteral : "[" Expressions "]"
;
IndexExpression : "[" Expression "]"
;
FunctionLiteral | "fn" "(" Identifiers ")" "{" BlockStatement "}"
;
CallExpression : FunctionLiteral "(" Expressions ")"
;
IfExpression : "if" "(" Expression ")" BlockStatement
| "if" "(" Expression ")" "{" BlockStatement "}" "else" "{" BlockStatement "}"
WhileExpression : "while" "(" Expression ")" "{" BlockStatement "}"
;
PrefixExpression : PrefixLiteral Expression
;
PrefixLiteral : "!" | "-" ;
InfixExpression: Expression InfixLiteral Expression
;
InfixLiteral : "+" | "-" | "*" | "/" ;