-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathantlr.cpp
More file actions
384 lines (326 loc) · 11.9 KB
/
Copy pathantlr.cpp
File metadata and controls
384 lines (326 loc) · 11.9 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#ifndef ANTLR_CUSTOM_CPP
#define ANTLR_CUSTOM_CPP
#include <string>
#include <iostream>
#include "antlr4-runtime/SinkedSeaLexer.h"
#include "antlr4-runtime/SinkedSeaParser.h"
#include "antlr4-runtime/SinkedSeaBaseVisitor.h"
#include "ast.hpp"
using namespace std;
using namespace antlr4;
namespace SinkedSea
{
// Visit the nodes of the SinkedSea Parse Tree and generate the AST.
// Each rule in the SinkedSea.g4 grammar file has an associated function here to generate the AST
class Visitor : public SinkedSeaVisitor
{
// Visit the parent node of any SinkedSea file
antlrcpp::Any visitFile(SinkedSeaParser::FileContext *context)
{
MultiTree *mt = new MultiTree;
for (auto command : context->command())
{
mt->commands.push_back(visit(command));
}
return shared_ptr<CommandTree>(mt);
}
antlrcpp::Any visitArguments(SinkedSeaParser::ArgumentsContext *context)
{
std::vector<shared_ptr<Tree>> exps;
for (auto exp : context->expression())
{
exps.push_back(visit(exp));
}
return exps;
}
antlrcpp::Any visitNewChan(SinkedSeaParser::NewChanContext *context)
{
ChanTree *cTree = new ChanTree;
cTree->ch = shared_ptr<Channel>(new Channel);
return shared_ptr<Tree>(cTree);
}
antlrcpp::Any visitChanRead(SinkedSeaParser::ChanReadContext *context)
{
ReadChanTree *crTree = new ReadChanTree;
crTree->child = visit(context->expression());
return shared_ptr<Tree>(crTree);
}
antlrcpp::Any visitExpression(SinkedSeaParser::ExpressionContext *context)
{
if (context->NAME() != NULL && context->arguments() == NULL)
{
VarTree *vTree = new VarTree;
vTree->name = context->NAME()->getText();
return shared_ptr<Tree>(vTree);
}
if (context->INT() != NULL)
{
IntTree *iTree = new IntTree;
iTree->val = stoi(context->INT()->getText());
return shared_ptr<Tree>(iTree);
}
if (context->BOOL() != NULL)
{
BoolTree *bTree = new BoolTree;
if (context->BOOL()->getText() == "true")
{
bTree->val = true;
}
else
{
bTree->val = false;
}
return shared_ptr<Tree>(bTree);
}
if (context->STR() != NULL) {
string s = context->STR()->getText();
s = s.substr(1, s.length() - 2); // Remove quotes around string
StringTree *sTree = new StringTree;
sTree->val = s;
return shared_ptr<Tree>(sTree);
}
if (context->op != NULL || context->MULT() != NULL || context->LT() != NULL || context->EQ() != NULL || context->AND() != NULL || context->OR() != NULL)
{
auto exp1 = context->expression()[0];
auto exp2 = context->expression()[1];
BinaryOp bop;
if (context->op)
{
bop = bops[context->op->getText()];
}
else if (context->MULT())
{
bop = bops[context->MULT()->getText()];
}
else if (context->LT())
{
bop = bops[context->LT()->getText()];
}
else if (context->EQ())
{
bop = bops[context->EQ()->getText()];
}
else if (context->AND())
{
bop = bops[context->AND()->getText()];
}
else if (context->OR())
{
bop = bops[context->OR()->getText()];
}
switch (bop)
{
case ADD:
case SUB:
case MULT:
{
IntBOPTree *iTree = new IntBOPTree;
iTree->bop = bop;
iTree->LTree = visit(exp1);
iTree->RTree = visit(exp2);
return shared_ptr<Tree>(iTree);
break;
}
case AND:
case OR:
case EQ:
case LT:
{
BoolBOPTree *bTree = new BoolBOPTree;
bTree->bop = bop;
bTree->LTree = visit(exp1);
bTree->RTree = visit(exp2);
return shared_ptr<Tree>(bTree);
break;
}
}
}
if (context->arguments() != NULL)
{
FunctionTree *fTree = new FunctionTree;
fTree->name = context->NAME()->getText();
fTree->args = visit(context->arguments()).as<vector<shared_ptr<Tree>>>();
return shared_ptr<Tree>(fTree);
}
if (context->newChan() != NULL)
{
return visit(context->newChan());
}
if (context->chanRead() != NULL)
{
return visit(context->chanRead());
}
throw runtime_error("Unimplemented expression rule");
}
antlrcpp::Any visitGlobalAssignment(SinkedSeaParser::GlobalAssignmentContext *context)
{
AssignTree *asgTree = new AssignTree;
asgTree->name = context->NAME()->getText();
asgTree->exp = visit(context->expression());
asgTree->global = true;
return shared_ptr<CommandTree>(asgTree);
}
antlrcpp::Any visitLocalAssignment(SinkedSeaParser::LocalAssignmentContext *context)
{
AssignTree *asgTree = new AssignTree;
asgTree->name = context->NAME()->getText();
asgTree->exp = visit(context->expression());
asgTree->local = true;
return shared_ptr<CommandTree>(asgTree);
}
antlrcpp::Any visitAssignment(SinkedSeaParser::AssignmentContext *context)
{
AssignTree *asgTree = new AssignTree;
asgTree->name = context->NAME()->getText();
asgTree->exp = visit(context->expression());
return shared_ptr<CommandTree>(asgTree);
}
antlrcpp::Any visitParameters(SinkedSeaParser::ParametersContext *context)
{
vector<string> names;
for (auto name : context->NAME())
{
names.push_back(name->getText());
}
return names;
}
antlrcpp::Any visitDeclaration(SinkedSeaParser::DeclarationContext *context)
{
DeclarationTree *decTree = new DeclarationTree;
decTree->name = context->NAME()->getText();
decTree->parameters = visit(context->parameters()).as<std::vector<std::string>>();
int i = 0;
for (auto command : context->command())
{
decTree->commands.push_back(visit(command));
}
return shared_ptr<CommandTree>(decTree);
}
antlrcpp::Any visitCom1(SinkedSeaParser::Com1Context *context)
{
MultiTree *mt = new MultiTree;
int i = 0;
for (auto com : context->command())
{
mt->commands.push_back(visit(com));
}
return shared_ptr<CommandTree>(mt);
}
antlrcpp::Any visitCom2(SinkedSeaParser::Com2Context *context)
{
MultiTree *mt = new MultiTree;
for (auto com : context->command())
{
mt->commands.push_back(visit(com));
}
return shared_ptr<CommandTree>(mt);
}
antlrcpp::Any visitIf2(SinkedSeaParser::If2Context *context)
{
IF2Tree *ifTree = new IF2Tree;
ifTree->guard = visit(context->expression());
ifTree->com1 = visit(context->com1());
ifTree->com2 = visit(context->com2());
return shared_ptr<CommandTree>(ifTree);
}
antlrcpp::Any visitIf1(SinkedSeaParser::If1Context *context)
{
IF1Tree *ifTree = new IF1Tree;
ifTree->guard = visit(context->expression());
ifTree->com = visit(context->com1());
return shared_ptr<CommandTree>(ifTree);
}
antlrcpp::Any visitReturnE(SinkedSeaParser::ReturnEContext *context)
{
ReturnTree *rt = new ReturnTree;
rt->exp = visit(context->expression());
return shared_ptr<CommandTree>(rt);
}
antlrcpp::Any visitPrint(SinkedSeaParser::PrintContext *context)
{
PrintTree *pt = new PrintTree;
pt->exp = visit(context->expression());
return shared_ptr<CommandTree>(pt);
}
antlrcpp::Any visitWhileLoop(SinkedSeaParser::WhileLoopContext *context)
{
WhileTree *wt = new WhileTree;
wt->lcondition = visit(context->expression());
MultiTree *mt = new MultiTree;
for (auto command : context->command())
{
mt->commands.push_back(visit(command));
}
wt->commands = shared_ptr<MultiTree>(mt);
return shared_ptr<CommandTree>(wt);
}
antlrcpp::Any visitThread(SinkedSeaParser::ThreadContext *context)
{
ThreadTree *tt = new ThreadTree;
tt->child = visit(context->expression());
return shared_ptr<CommandTree>(tt);
}
antlrcpp::Any visitChanAssignment(SinkedSeaParser::ChanAssignmentContext *context)
{
ChanAssignTree *caTree = new ChanAssignTree;
caTree->LTree = visit(context->expression()[0]);
caTree->RTree = visit(context->expression()[1]);
return shared_ptr<CommandTree>(caTree);
}
antlrcpp::Any visitCommand(SinkedSeaParser::CommandContext *context)
{
if (context->globalAssignment() != NULL)
{
return visit(context->globalAssignment());
}
if (context->localAssignment() != NULL)
{
return visit(context->localAssignment());
}
if (context->assignment() != NULL)
{
return visit(context->assignment());
}
if (context->declaration() != NULL)
{
return visit(context->declaration());
}
if (context->if2() != NULL)
{
return visit(context->if2());
}
if (context->if1() != NULL)
{
return visit(context->if1());
}
if (context->returnE() != NULL)
{
return visit(context->returnE());
}
if (context->print() != NULL)
{
return visit(context->print());
}
if (context->whileLoop() != NULL)
{
return visit(context->whileLoop());
}
if (context->thread() != NULL)
{
return visit(context->thread());
}
if (context->chanAssignment() != NULL)
{
return visit(context->chanAssignment());
}
if (context->expression() != NULL)
{
ExpressionComTree *ecTree = new ExpressionComTree;
ecTree->exp = visit(context->expression());
return shared_ptr<CommandTree>(ecTree);
}
throw std::runtime_error("Unimplemented rule for command");
}
};
}; // namespace SinkedSea
#endif