This repository has been archived by the owner on Mar 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
251 lines (227 loc) · 5.71 KB
/
parser.y
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
%{
package arnoldc
func setProgram(l yyLexer, v Program) {
l.(*ArnoldC).program = v
}
%}
%union{
str string
strs []string
integer int
value Value
values []Value
statement Statement
statements []Statement
expression Expression
expressions []Expression
block Block
blocks []Block
function Method
functions []Method
program Program
}
%token LexError
%token <str> Variable
%token <str> String
%token <integer> Integer
%token MAIN_OPEN MAIN_CLOSE
%token METHOD_OPEN METHOD_CLOSE DECLARE_PARAMETER END_PARAMETER_DECLARATION RETURN
%token CALL_METHOD ASSIGN_FROM_CALL
%token PRINT
%token TRUE FALSE
%token DECLARE INITIALIZE
%token ASSIGNMENT FIRST_OPERAND ASSIGNMENT_END
%token ADD SUBTRACT MULTIPLY DIVIDE
%token EQUAL_TO GREATER_THAN OR AND
%token IF ELSE END_IF IF_TRUE IF_FALSE
%token WHILE END_WHILE
%type <integer> arithmetic_token
%type <strs> parameters
%type <value> value number
%type <values> values
%type <statements> statements arithmetics
%type <expression> expression arithmetic invoke
%type <block> block
%type <function> main method
%type <functions> methods
%type <program> program
%start program
%%
program: main
{
setProgram(yylex, Program{Main: $1})
}
| main methods
{
setProgram(yylex, Program{Main: $1, Methods: $2})
}
main: MAIN_OPEN statements MAIN_CLOSE
{
$$ = Method{Name: "", Statements: $2}
}
methods: method
{
$$ = []Method{$1}
}
| methods method
{
$1 = append($1, $2)
$$ = $1
}
method: METHOD_OPEN Variable statements METHOD_CLOSE
{
$$ = Method{$2, []string{}, $3}
}
| METHOD_OPEN Variable parameters END_PARAMETER_DECLARATION statements METHOD_CLOSE
{
$$ = Method{$2, $3, $5}
}
parameters: DECLARE_PARAMETER Variable
{
$$ = []string{$2}
}
| parameters DECLARE_PARAMETER Variable
{
$1 = append($1, $3)
$$ = $1
}
statements: expression
{
$$ = []Statement{$1}
}
| block
{
$$ = []Statement{$1}
}
| statements expression
{
$1 = append($1, $2)
$$ = $1
}
| statements block
{
$1 = append($1, $2)
$$ = $1
}
expression: PRINT value
{
$$ = Expression{PRINT, []Value{$2}}
}
| DECLARE Variable INITIALIZE number
{
$$ = Expression{DECLARE, []Value{VariableValue{$2}, $4}}
}
| RETURN value
{
$$ = Expression{RETURN, []Value{$2}}
}
| invoke
invoke: CALL_METHOD Variable
{
$$ = Expression{CALL_METHOD, []Value{VariableValue{$2}}}
}
| CALL_METHOD Variable values
{
$3 = append([]Value{VariableValue{$2}}, $3...)
$$ = Expression{CALL_METHOD, $3}
}
| ASSIGN_FROM_CALL Variable CALL_METHOD Variable
{
$$ = Expression{ASSIGN_FROM_CALL, []Value{VariableValue{$2}, VariableValue{$4}}}
}
| ASSIGN_FROM_CALL Variable CALL_METHOD Variable values
{
$5 = append([]Value{VariableValue{$2}, VariableValue{$4}}, $5...)
$$ = Expression{ASSIGN_FROM_CALL, $5}
}
block: ASSIGNMENT Variable FIRST_OPERAND number arithmetics ASSIGNMENT_END
{
$$ = Block{ASSIGNMENT, []Value{VariableValue{$2}}, append([]Statement{Expression{FIRST_OPERAND, []Value{$4}}}, $5...)}
}
| IF number statements END_IF
{
$$ = Block{IF, []Value{$2}, []Statement{Block{IF_TRUE, []Value{}, $3}}}
}
| IF number statements ELSE statements END_IF
{
$$ = Block{IF, []Value{$2}, []Statement{Block{IF_TRUE, []Value{}, $3}, Block{IF_FALSE, []Value{}, $5}}}
}
| WHILE number statements END_WHILE
{
$$ = Block{WHILE, []Value{$2}, $3}
}
arithmetics: arithmetic
{
$$ = []Statement{$1}
}
| arithmetics arithmetic
{
$1 = append($1, $2)
$$ = $1
}
arithmetic: arithmetic_token number
{
$$ = Expression{$1, []Value{$2}}
}
arithmetic_token: SUBTRACT
{
$$ = SUBTRACT
}
| ADD
{
$$ = ADD
}
| MULTIPLY
{
$$ = MULTIPLY
}
| DIVIDE
{
$$ = DIVIDE
}
| EQUAL_TO
{
$$ = EQUAL_TO
}
| GREATER_THAN
{
$$ = GREATER_THAN
}
| OR
{
$$ = OR
}
| AND
{
$$ = AND
}
number: Variable
{
$$ = VariableValue{$1}
}
| Integer
{
$$ = IntegerValue{$1}
}
| TRUE
{
$$ = IntegerValue{1}
}
| FALSE
{
$$ = IntegerValue{0}
}
values: value
{
$$ = []Value{$1}
}
| values value
{
$1 = append($1, $2)
$$ = $1
}
value: String
{
$$ = StringValue{$1}
}
| number