-
Notifications
You must be signed in to change notification settings - Fork 1
/
mylang.grammar
101 lines (85 loc) · 1.47 KB
/
mylang.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
program
-> statements {% id %}
statements
-> _ statement _ {%
data => [data[1]]
%}
| _ statement _ "\n" statements {%
data => [data[1], ...data[4]]
%}
statement
-> var_assignment {% id %}
| print_statement {% id %}
| while_loop {% id %}
while_loop
-> "while" __ binary_expression __ "{" _ "\n" statements "\n" "}" {%
data => {
return {
type: "while_loop",
condition: data[2],
body: data[7]
}
}
%}
print_statement
-> "print" __ expression {%
data => {
return {
type: "print_statement",
expression: data[2]
}
}
%}
expression
-> unary_expression {% id %}
| binary_expression {% id %}
unary_expression
-> number {% id %}
| identifier {% id %}
operator
-> "+" {% id %}
| "-" {% id %}
| "*" {% id %}
| "/" {% id %}
| ">=" {% id %}
| "<=" {% id %}
| ">" {% id %}
| "<" {% id %}
| "=" {% id %}
binary_expression
-> unary_expression _ operator _ expression {%
data => {
return {
type: "binary_expression",
left: data[0],
operator: data[2],
right: data[4],
}
}
%}
var_assignment
-> identifier _ ":=" _ expression {%
data => {
return {
type: "var_assignment",
varname: data[0],
value: data[4]
}
}
%}
identifier
-> [a-z]:+ {%
data => data[0].join("")
%}
number
-> digits "." digits {%
data => Number(data[0] + "." + data[2])
%}
| digits {%
data => Number(data[0])
%}
digits ->[0-9]:+ {%
data => data[0].join("")
%}
_ -> [ \t]:*
__ -> [ \t]:+