-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.cpp
265 lines (213 loc) · 7.68 KB
/
Parser.cpp
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
#include "Parser.h"
#include <cassert>
Parser::Parser(std::string buf)
: lex(buf)
{
}
extern bool IsPunctuation(char c);
bool IsFunction(std::string typeName)
{
return (typeName == "function");
}
bool IsVar(std::string typeName)
{
return (typeName == "var");
}
void PrintFunction(FuncNode* node)
{
assert(node->type == FUNCTION);
printf("Function %s(", node->name.c_str());
for (int i = 0; i < node->args.size(); i++)
{
printf("%s", node->args[i].c_str());
if (i < (node->args.size()-1))
printf(", ");
}
printf(")\n");
}
void PrintVariable(VarDeclNode* node)
{
printf("Var \"%s\", initial value = \"%s\"\n", node->name.c_str(), node->initialVal.c_str());
}
void PrintFuncCall(FuncCallNode* node)
{
printf("Function call \"%s\" with args: ", node->name.c_str());
for (int i = 0; i < node->arg_vals.size(); i++)
{
printf("\"%s\"", node->arg_vals[i].val.c_str());
if (i < (node->arg_vals.size()-1))
printf(", ");
}
printf("\n");
}
void PrintReturn()
{
printf("Function return\n");
}
void RecursivelyPrintTree(Node* node, int level)
{
for (int i = 0; i < level; i++)
printf("\t");
switch (node->type)
{
case ROOT_NODE:
printf("(ROOT)\n");
break;
case FUNCTION:
PrintFunction((FuncNode*)node);
break;
case VARIABLE:
PrintVariable((VarDeclNode*)node);
break;
case FUNCTION_CALL:
PrintFuncCall((FuncCallNode*)node);
break;
case RETURN:
PrintReturn();
break;
default:
printf("Unknown AST tree node type %d\n", node->type);
break;
}
for (auto& node : node->children)
{
RecursivelyPrintTree(node, level+1);
}
}
Node* Parser::Parse(std::string outPutFile)
{
Token token;
Node* rootNode = new Node;
Node* current = nullptr;
std::vector<VarDeclNode*> vars;
int varIndex = 0;
VarDeclNode* prevVarNode = nullptr;
rootNode->type = ROOT_NODE;
FuncNode* mainNode = nullptr;
while (lex.GetNextToken(token))
{
if (IsFunction(token.val))
{
lex.GetNextToken(token);
if (token.type != IDENTIFIER)
ERROR("ERROR: Expected identifier, got \"%s\"\n", token.val.c_str());
FuncNode* func = new FuncNode;
func->name = token.val;
func->type = FUNCTION;
lex.GetNextToken(token);
if (token.type != PUNCTUATION || token.val != "(")
ERROR("Expected \"(\", got \"%s\" instead\n", token.val.c_str());
while (lex.GetNextToken(token) && token.val != ")")
{
// Arguments
func->args.push_back(token.val);
if (lex.GetNextToken(token) && token.val != ",")
break;
}
lex.GetNextToken(token);
if (token.type != PUNCTUATION || token.val != "{")
ERROR("Expected \"{\", got \"%s\"\n", token.val.c_str());
rootNode->children.push_back(func);
current = rootNode->children.back();
if (func->name == "main")
mainNode = func;
}
else if (token.type == PUNCTUATION && token.val == "}")
{
// Insert a return, if the previous node isn't a return
if (current->children.back()->type != RETURN)
current->children.push_back(new Node{.type = RETURN});
printf("End of function scope!\n");
current = nullptr;
}
else if (IsVar(token.val))
{
lex.GetNextToken(token);
VarDeclNode* var = new VarDeclNode;
var->type = VARIABLE;
var->name = token.val;
var->index = varIndex++;
vars.push_back(var);
if (current)
current->children.push_back(var);
else
rootNode->children.push_back(var);
Token peekToken;
if (lex.PeekNextToken(peekToken) && peekToken.type == OPERATION && peekToken.val == "=")
{
lex.GetNextToken(token);
lex.GetNextToken(token); // The value
if (token.type != IDENTIFIER && token.type != INTEGER && token.type != STRING)
ERROR("Error: expected identifier or constant after \"=\", got \"%s\" instead\n", token.val.c_str());
if (token.type == STRING)
var->varType = VarDeclNode::STRING;
else if (token.type == INTEGER)
var->varType = VarDeclNode::INT;
var->initialVal = token.val;
}
else
var->initialVal = "";
printf("New variable \"%s\", index %d, initial value \"%s\"\n", var->name.c_str(), var->index, var->initialVal.c_str());
if (!lex.GetNextToken(token) || token.val != ";")
printf("Expected semicolon at end of variable declaration\n");
}
else if (token.type == IDENTIFIER && token.val == "return")
{
Node* node = new Node;
node->type = RETURN;
if (!current)
ERROR("Unexpected \"return\" outside of function\n");
current->children.push_back(node);
Token peekToken;
if (lex.PeekNextToken(peekToken) && (token.type == INTEGER || token.type == STRING))
ERROR("TODO: Non-void return statements!\n");
if (!lex.GetNextToken(token) || token.type != PUNCTUATION || token.val != ";")
ERROR("Expected ';' at the end of return statement, got \"%s\"\n", token.val.c_str());
}
else
{
Token peekToken;
if (lex.PeekNextToken(peekToken) && peekToken.type == PUNCTUATION && peekToken.val == "(")
{
FuncCallNode* node = new FuncCallNode;
node->name = token.val;
node->type = FUNCTION_CALL;
printf("Function call: calling %s(", token.val.c_str());
lex.GetNextToken(token);
while (lex.GetNextToken(token) && token.val != ")")
{
// Arguments
ArgType type;
if (token.type == STRING)
type = ARG_STRING;
else if (token.type == IDENTIFIER)
type = ARG_VARIABLE;
else
ERROR("Unknown arg type %d\n", token.type);
node->arg_vals.push_back({.val = token.val, .type = type});
if (token.type == STRING)
printf("\"");
printf("%s", token.val.c_str());
if (token.type == STRING)
printf("\"");
if (lex.GetNextToken(token) && token.val != ",")
break;
printf(", ");
}
printf(")\n");
if (!lex.GetNextToken(token) || token.type != PUNCTUATION || token.val != ";")
ERROR("Expected ';' at the end of function call, got \"%s\"\n", token.val.c_str());
current->children.push_back(node);
}
else
{
ERROR("Unknown token \"%s\"\n", token.val.c_str());
}
}
}
RecursivelyPrintTree(rootNode, 0);
if (!mainNode)
ERROR("ERROR: No main entry point defined!\n");
// TODO: Dump AST to intermediate file if the user wants it
return rootNode;
}