-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluator.cpp
253 lines (229 loc) · 9.86 KB
/
Evaluator.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
#include "Evaluator.h"
#include <cmath>
#include <iostream>
#include "Utils/AssertError.h"
#include "Utils/Debug.h"
#include "Utils/StringUtils.h"
#include "Utils/FloatUtils.h"
Evaluator::Evaluator() {}
Evaluator::~Evaluator() {}
DataType Evaluator::eval(SyntaxTreeNode *root) {
// Traverse and eval all children.
db("eval:" << root->token);
db("Child nodes count:" << root->childNodes.size());
DataType ret;
switch (root->keywordType) {
case (KEYWORD_CONDITIONAL): {
ret = evalConditional(root);
break;
}
case (KEYWORD_VARIABLE_DEF): {
evalVarDef(root);
break;
}
case (KEYWORD_LAMBDA_DEF): {
evalLambdaDef(root);
break;
}
case (KEYWORD_CONSTANT): {
if (root->evaluated) {
ret = root->value;
} else {
bool tmp = StringUtils::stringToDouble(root->token, ret._doubledata);
ret.dataType = DataType::DATA_TYPE_DOUBLE;
root->evaluated = true;
root->value = ret;
eassert(tmp == true, "Error. Invalid number:" + root->token);
}
break;
}
case (KEYWORD_STRING): {
ret = root->token;
root->evaluated = true;
root->value = root->token;
break;
}
// For tokens with potentially unlimited children, evaluate everything first.
default: {
for (std::size_t i = 0; i < root->childNodes.size(); ++i) {
ret = eval(root->childNodes[i]);
}
break;
}
}
switch (root->keywordType) {
// If token is an operator, calculate result from its children and return evaluated value.
case (KEYWORD_OPERATOR): {
ret = evalOp(root);
break;
}
// If token is a symbol, map to its definition.
case (KEYWORD_SYMBOL): {
db("Symbol:" << root->token);
ret = evalSymbol(root);
break;
}
default: { // Empty token.
break;
}
}
// In case of something like ((2)) or ((1) (2)), i.e empty tokens, this happens.
root->evaluated = true;
root->value = ret;
return ret;
}
DataType Evaluator::evalOp(SyntaxTreeNode *node) {
std::vector<DataType> results; // Evaluted results of all children.
for (std::size_t i = 0; i < node->childNodes.size(); ++i) {
eassert(node->childNodes[i]->evaluated == true, "Error. Failed to evaluate operator, children not evaluted.");
results.push_back(node->childNodes[i]->value);
}
return mapOp(node->token, results);
}
DataType Evaluator::mapOp(const std::string &op, std::vector<DataType> vOperands) {
DataType ret;
if (op == "+") {
ret = vOperands.at(0);
for (std::size_t i = 1; i < vOperands.size(); ++i) {
ret = ret + vOperands.at(i);
}
} else if (op == "-") {
eassert(vOperands.size() == 2, "Error. Substraction takes 2 operands.");
ret = vOperands.at(0) - vOperands.at(1);
} else if (op == "*") {
ret = vOperands.at(0);
for (std::size_t i = 1; i < vOperands.size(); ++i) {
ret = ret * vOperands.at(i);
}
} else if (op == "/") {
eassert(vOperands.size() == 2, "Error. Division takes 2 operands:" + op);
ret = vOperands.at(0) / vOperands.at(1);
} else if (op == "<") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) < vOperands.at(1));
} else if (op == ">") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) > vOperands.at(1));
} else if (op == "==") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) == vOperands.at(1));
} else if (op == "&&") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) && vOperands.at(1));
} else if (op == "||") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) || vOperands.at(1));
} else if (op == "<=") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) <= vOperands.at(1));
} else if (op == ">=") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) >= vOperands.at(1));
} else if (op == "!=") {
eassert(vOperands.size() == 2, "Error. Comparison takes 2 operands:" + op);
ret = (vOperands.at(0) != vOperands.at(1));
} else if (op == "%") {
eassert(vOperands.size() == 2, "Error. Modulo takes 2 operands:" + op);
long mod = ((long)vOperands.at(0)._doubledata % (long)vOperands.at(1)._doubledata);
ret = static_cast<double>(mod);
}
return ret;
}
DataType Evaluator::evalConditional(SyntaxTreeNode *node) {
eassert((node->childNodes.size() == 2 || node->childNodes.size() == 3), "Error. Conditional takes 2-3 arguments.");
DataType test = eval(node->childNodes[0]);
DataType ret;
if (!(test == 0.0f)) {
ret = eval(node->childNodes[1]);
} else if (node->childNodes.size() > 2) {
ret = eval(node->childNodes[2]);
}
return ret;
}
// Eval "define" token. When a variable is defined, it must be evaluable.
void Evaluator::evalVarDef(SyntaxTreeNode *node) {
eassert(node->childNodes.size() == 2, "Error. Variable def only takes 2 arguments." + node->token);
DataType val = eval(node->childNodes[1]);
std::string id = node->childNodes[0]->token;
SyntaxTreeNode::Symbol sym;
sym.type = SyntaxTreeNode::Symbol::SYMBOL_TYPE_VAR;
sym.value = val;
db("Adding symbol " << id << "=" << val);
node->parent->propagateSymbol({id, sym});
}
// (lambda (symbol) (var1) (definition))
void Evaluator::evalLambdaDef(SyntaxTreeNode *node) {
eassert(node->childNodes.size() >= 3, "Error. Syntax error in lambda definition: " + node->childNodes[0]->token);
db("Lambda def: " << node->childNodes[0]->token);
SyntaxTreeNode::Symbol sym;
sym.type = SyntaxTreeNode::Symbol::SYMBOL_TYPE_LAMBDA;
sym.funcDef = node;
std::string id = node->childNodes[0]->token;
node->parent->propagateSymbol({id, sym});
}
DataType Evaluator::evalSymbol(SyntaxTreeNode *node) {
// Look up node's symbolTable, get value
db("NodeID " << node->nodeid);
db("symbolTable size:" << node->symbolTable.size());
auto symbolTableEntry = node->symbolTable.find(node->token);
eassert(symbolTableEntry != node->symbolTable.end(), "Undefined symbol " + node->token);
if (symbolTableEntry->second.type == SyntaxTreeNode::Symbol::SYMBOL_TYPE_VAR) {
db("Variable " << symbolTableEntry->second.value);
return symbolTableEntry->second.value;
} else { // SYMBOL_TYPE_FUNCTION
// eassert(node->childNodes.size() <= 1, "Lambdas only takes 0-1 argument: " + node->token);
std::size_t lambdaDefIndex = symbolTableEntry->second.funcDef->childNodes.size() - 1;
SyntaxTreeNode *lambdaDef =
symbolTableEntry->second.funcDef->childNodes[lambdaDefIndex]; // Lambda def's syntax node
// Try to process arguments accordingly to definition.
std::vector<DataType> argValue;
std::vector<std::string> argSymbol;
if (node->childNodes.size() > 0) { // Lambda takes argument.
for (std::size_t i = 0; i < node->childNodes.size(); ++i) {
eassert(node->childNodes[i]->evaluated == true, "Child node has not been evaluated.");
argValue.push_back(node->childNodes[i]->value); // Real value
argSymbol.push_back(symbolTableEntry->second.funcDef->childNodes[1 + i]->token); // In def
}
// Ignore "lambda" keyword and lambda definition. So we subtract 2.
eassert(symbolTableEntry->second.funcDef->childNodes.size() - 2 == argSymbol.size(),
"Error. Argument count mismatch in function: " + node->token);
}
// Copy lambda def into node. Lambda def is just a blueprint. This changes node's content.
node->constructLambdaNode(lambdaDef);
// Recursively replace argSymbol with real argValue for all children. This changes node's content.
if (node->childNodes.size() > 0) { // Lambda takes argument.
expandVar(node, argSymbol, argValue);
}
// Evaluate self
DataType ret = eval(node);
// Clean up
node->cleanSyntaxTree();
node->childNodes.clear();
db("Return from func:" << ret);
return ret;
}
}
void Evaluator::expandVar(SyntaxTreeNode *functionNode, const std::vector<std::string> &argSymbols,
const std::vector<DataType> &vals) {
eassert(argSymbols.size() == vals.size(), "Error. Argument number mismatch.");
db("arg count:" << argSymbols.size());
db("children count:" << functionNode->childNodes.size());
db("parent:" << functionNode->parent);
db("parent's token:" << functionNode->parent->token);
db("self's token:" << functionNode->token);
db("self's keywordType:[SYMBOL=4]" << functionNode->keywordType);
//__asm__("int $3");
for (std::size_t i = 0; i < argSymbols.size(); ++i) {
std::string argSymbol = argSymbols[i];
DataType val = vals[i];
if (functionNode->keywordType == KEYWORD_SYMBOL && functionNode->token == argSymbol) {
db("Found symbol " + argSymbol);
functionNode->keywordType = KEYWORD_CONSTANT;
functionNode->evaluated = true;
functionNode->value = val;
}
}
for (std::size_t i = 0; i < functionNode->childNodes.size(); ++i) {
expandVar(functionNode->childNodes[i], argSymbols, vals);
}
}