-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
249 lines (219 loc) · 6.42 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory>
#include <stdbool.h>
#include "../tree.hpp"
void yyerror(const char *s);
int yylex(void);
StatementNode* root;
%}
%debug
%union {
double float_val;
char* str;
char operation;
LiteralNumberNode* number;
StatementNode* stmt;
ExpressionNode* expr;
BinOpNode* binop;
ArrayElements* expressions;
ArrayNode* arr;
VariableNode* var;
ModificationAssign* mod;
FunctionCallData* fcd;
}
%token <float_val> NUMBER
%token <str> IDENT
%token END_OF_FILE ASSIGN LBRACKET RBRACKET COMMA LPAREN RPAREN LESS_THAN GREATER_THAN LCURLY RCURLY EQUALITY INEQUALITY
%token SLASH PLUS MINUS STAR HASH MOD
%type <number> number_literal
%type <stmt> statement for_each statements
%type <expr> expression array_access expression_function
%type <binop> binary_operation
%type <expressions> elements
%type <arr> array;
%type <var> identifier
%type <mod> modification_assignment
%type <fcd> function_call
%token LEN DIM
%start outer_statements
%%
outer_statements:
statements END_OF_FILE {
root = $1;
YYACCEPT;
}
statements:
statement {
$$ = $1;
} |
statements ';' statement {
$$ = $1->append($3);
}
;
modification_assignment:
PLUS ASSIGN expression {
$$ = new ModificationAssign(ExpressionOperation::ADD, $3);
} |
MINUS ASSIGN expression {
$$ = new ModificationAssign(ExpressionOperation::SUBTRACT, $3);
} |
SLASH ASSIGN expression {
$$ = new ModificationAssign(ExpressionOperation::DIVIDE, $3);
} |
STAR ASSIGN expression {
$$ = new ModificationAssign(ExpressionOperation::MULTIPLY, $3);
} |
MOD ASSIGN expression {
$$ = new ModificationAssign(ExpressionOperation::MOD, $3);
}
;
statement:
IDENT ASSIGN expression {
$$ = new AssignmentNode($1, $3);
} |
expression LBRACKET expression RBRACKET ASSIGN expression {
$$ = new ElementAssignmentNode($1, $6, $3);
} |
expression LBRACKET expression RBRACKET modification_assignment {
$$ = new ElementAssignmentNode($1, new BinOpNode(new BinOpNode($1, $3, ExpressionOperation::ACCESS), $5->value, $5->operation), $3);
} |
IDENT modification_assignment {
$$ = new AssignmentNode($1, new BinOpNode(new VariableNode($1), $2->value, $2->operation));
delete $2;
} |
for_each |
function_call {
$$ = $1->createNodeStatement();
delete $1;
} |
%empty {
$$ = nullptr;
}
;
identifier:
IDENT {
$$ = new VariableNode($1);
}
;
expression:
array |
number_literal |
binary_operation |
identifier |
expression_function |
property_access |
function_call {
$$ = $1->createNodeExpression();
delete $1;
} |
array_access
;
array_access:
expression LBRACKET expression RBRACKET {
$$ = new BinOpNode($1, $3, ExpressionOperation::ACCESS);
}
;
expression_function:
LPAREN expression RPAREN HASH identifier LCURLY expression RCURLY {
$$ = new ExpressionFunctionNode(ExpressionFunctionType::MAP, $2, $5, nullptr, $7);
} | // (x)#a{ a + 5 };
LPAREN expression RPAREN HASH identifier COMMA identifier LCURLY expression RCURLY {
$$ = new ExpressionFunctionNode(ExpressionFunctionType::MAP, $2, $5, $7, $9);
} | // (x)#a, i{ a + 5 };
LPAREN expression RPAREN SLASH identifier LCURLY expression RCURLY {
$$ = new ExpressionFunctionNode(ExpressionFunctionType::FILTER, $2, $5, nullptr, $7);
} | // (x)/a{ a + 5 };
LPAREN expression RPAREN SLASH identifier COMMA identifier LCURLY expression RCURLY {
$$ = new ExpressionFunctionNode(ExpressionFunctionType::FILTER, $2, $5, $7, $9);
} | // (x)/a{ a + 5 };
LPAREN expression RPAREN LESS_THAN identifier COMMA identifier LCURLY expression RCURLY {
$$ = new ExpressionFunctionNode(ExpressionFunctionType::REDUCE, $2, $5, $7, $9);
} // (x)<acc, v { acc + v };
;
for_each:
LPAREN expression RPAREN STAR identifier COMMA identifier LCURLY statements RCURLY {
$$ = new StatementFunctionNode($2, $5, $7, $9);
} |
LPAREN expression RPAREN STAR identifier LCURLY statements RCURLY {
$$ = new StatementFunctionNode($2, $5, nullptr, $7);
}
;
property_access:
IDENT '.' LEN { printf("length access of %s\n", $1); } |
IDENT '.' DIM { printf("dim access of %s\n", $1); }
;
function_call:
IDENT LPAREN elements RPAREN {
$$ = new FunctionCallData(std::string($1), $3);
} |
IDENT LPAREN RPAREN {
$$ = new FunctionCallData(std::string($1), nullptr);
}
;
binary_operation:
expression SLASH expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::DIVIDE);
} |
expression PLUS expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::ADD);
} |
expression MINUS expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::SUBTRACT);
} |
expression STAR expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::MULTIPLY);
} |
expression MOD expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::MULTIPLY);
} |
expression LESS_THAN expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::LT);
} |
expression GREATER_THAN expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::GT);
} |
expression GREATER_THAN ASSIGN expression {
$$ = new BinOpNode($1, $4, ExpressionOperation::GTEQ);
} |
expression LESS_THAN ASSIGN expression {
$$ = new BinOpNode($1, $4, ExpressionOperation::LTEQ);
} |
expression EQUALITY expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::EQ);
} |
expression INEQUALITY expression {
$$ = new BinOpNode($1, $3, ExpressionOperation::INEQ);
}
;
number_literal:
NUMBER {
$$ = new LiteralNumberNode($1);
}
array:
LBRACKET elements RBRACKET {
$$ = new ArrayNode($2);
} |
LBRACKET RBRACKET {
$$ = new ArrayNode(nullptr);
}
;
elements:
expression {
$$ = new ArrayElements();
$$->expressions.push_back($1);
} |
elements COMMA expression {
$1->expressions.push_back($3);
$$ = $1;
}
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}
StatementNode* getRoot() {
return root;
}