-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheval.h
More file actions
343 lines (309 loc) · 13 KB
/
eval.h
File metadata and controls
343 lines (309 loc) · 13 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
#ifndef TINYJS_EVAL
#define TINYJS_EVAL
#include <cmath>
#include <string>
#include "env.h"
#include "ast.h"
#include "log.h"
#include "built_in.h"
// #define elog
namespace Eval
{
using namespace AST;
using namespace Env;
using namespace BuiltIn;
class EvalImpl : public BuiltInImpl
{
using IntType = long long;
using T = std::vector<std::shared_ptr<ExprAST>>;
using EnvImpl = EnvImpl<T::value_type>;
private:
std::shared_ptr<EnvImpl> Scope;
std::shared_ptr<EnvImpl> CurScope;
const std::string TopScope = "__top_expression";
T Expression; // T := vector<unique_ptr<ExprAST>>
unsigned long long EvalLineNumber;
std::string ERR_INFO;
public:
EvalImpl() = delete;
EvalImpl(T Expression) : Expression(std::move(Expression))
{
BuiltInImpl();
if (!Scope)
{
CurScope.reset(new EnvImpl(TopScope));
Scope = CurScope;
EvalLineNumber = 1;
ERR_INFO = "";
}
}
~EvalImpl() = default;
EvalImpl(const EvalImpl&) = delete;
const EvalImpl& operator =(const EvalImpl&) = delete;
EvalImpl(EvalImpl&&) = delete;
const EvalImpl& operator =(EvalImpl&&) = delete;
/* Attention !!! Wait for rewrite !!! */
std::shared_ptr<ExprAST> exec_built_in(std::shared_ptr<ExprAST> Func)
{
auto F = ptr_to<CallExprAST>(Func);
auto Name = F->Callee;
if (Name == "print")
{
for (int i = 0; i < F->Args.size(); i++)
{
auto arg = F->Args[i];
print_value(eval_expression(arg));
}
}
return F;
}
/* -- Scope -- */
std::shared_ptr<EnvImpl> get_top_scope()
{
auto _CurScope = CurScope;
while (_CurScope->Parent)
_CurScope = _CurScope->Parent;
return _CurScope;
}
void enter_new_env()
{ CurScope.reset(new EnvImpl(CurScope)); }
void enter_new_env(const std::string& Name)
{ CurScope.reset(new EnvImpl(Name, CurScope)); }
void recover_prev_env()
{
if (CurScope->Parent)
CurScope = CurScope->Parent;
}
std::shared_ptr<EnvImpl> find_name_belong_scope(const std::string& Name)
{
auto val = CurScope->get(Name);
auto _CurScope = CurScope;
// Search for parent scope
while (!val && _CurScope->Parent)
{
_CurScope = _CurScope->Parent;
val = _CurScope->get(Name);
}
if (!val) return CurScope;
return _CurScope;
}
bool is_top_scope()
{ return CurScope->Parent ? false : true; }
/* ++ Scope ++ */
bool is_interrupt_control_flow(std::shared_ptr<ExprAST> E)
{
switch (E->SubType)
{
case Type::break_expr: case Type::continue_expr: case Type::return_expr:
if (is_top_scope())
eval_err("Uncaught SyntaxError: Illegal " + E->get_ast_name() + " statement");
return true;
default:
return false;
}
}
/* -- Name -- */
// Find exist name
// if (!find_name()) => Check name is exist?
std::shared_ptr<ExprAST> find_name(const std::string& Name)
{ return find_name_belong_scope(Name)->get(Name); }
// Set a variable or function
void set_name(const std::string& Name, std::shared_ptr<ExprAST> Value)
{ find_name_belong_scope(Name)->set(Name, Value); }
// get name
std::string get_name(std::shared_ptr<ExprAST> V)
{
switch (V->SubType)
{
case Type::variable_expr:
return ptr_to<VariableExprAST>(V)->Name;
case Type::binary_op_expr:
return get_name(ptr_to<BinaryOpExprAST>(V)->LHS);
case Type::call_expr:
return ptr_to<CallExprAST>(V)->Callee;
case Type::function_expr:
return ptr_to<FunctionAST>(V)->Proto->Name;
default:
return "";
}
}
/* ++ Name ++ */
// Type conversion: integer float string => bool
bool value_to_bool(std::shared_ptr<ExprAST> V)
{
#ifdef elog
log("in value_to_bool");
#endif
switch (V->SubType)
{
case Type::integer_expr:
if (ptr_to<IntegerValueExprAST>(V)->Val)
return true;
return false;
case Type::float_expr:
if (ptr_to<FloatValueExprAST>(V)->Val)
return true;
return false;
case Type::string_expr:
if (ptr_to<StringValueExprAST>(V)->Val.length())
return true;
return false;
case Type::variable_expr:
{
auto v = ptr_to<VariableExprAST>(V);
auto _v = find_name(v->Name);
if (!_v)
{
ERR_INFO = "[value_to_bool] ReferenceError: " + v->Name + " is not defined.";
eval_err(ERR_INFO);
}
return value_to_bool(_v);
}
default:
return false;
}
return false;
}
// Point transform
template <typename T>
inline std::shared_ptr<T> ptr_to(std::shared_ptr<ExprAST> P)
{ return std::static_pointer_cast<T>(P); }
/* -- Value -- */
void print_value(std::shared_ptr<ExprAST> V)
{
switch (V->SubType)
{
case Type::integer_expr:
std::cout << get_value<IntegerValueExprAST>(V) << std::endl;
break;
case Type::float_expr:
std::cout << get_value<FloatValueExprAST>(V) << std::endl;
break;
case Type::string_expr:
std::cout << get_value<StringValueExprAST>(V) << std::endl;
break;
case Type::variable_expr:
{
auto _v = ptr_to<VariableExprAST>(V);
auto _var = get_variable_value(_v);
if (!_var)
{
std::cout << "[warnning] Variable '"<< _v->Name << "' = undefined." << std::endl;
return;
}
std::cout << "Variable '" << _v->Name << "' = ";
print_value(_var);
break;
}
default:
std::cout << "[print_value] ExprAST Undefined." << std::endl;
V->print_ast();
break;
}
}
// get Integer or Float or String
template <typename T>
typename T::value_type get_value(std::shared_ptr<ExprAST> V)
{
return ptr_to<T>(V)->Val;
}
std::shared_ptr<ExprAST> get_variable_value(std::shared_ptr<ExprAST> V)
{
auto v = find_name(ptr_to<VariableExprAST>(V)->Name);
if (!v) return nullptr;
return v;
}
/* ++ Value ++ */
std::shared_ptr<ExprAST> eval_function_expr(std::shared_ptr<FunctionAST> F);
std::shared_ptr<ExprAST> eval_return(std::shared_ptr<ReturnExprAST> R);
std::shared_ptr<ExprAST> eval_if_else(std::shared_ptr<IfExprAST> If);
std::shared_ptr<ExprAST> eval_for(std::shared_ptr<ForExprAST> For);
std::shared_ptr<ExprAST> eval_while(std::shared_ptr<WhileExprAST> While);
std::shared_ptr<ExprAST> eval_do_while(std::shared_ptr<DoWhileExprAST> DoWhile);
std::shared_ptr<ExprAST> eval_call_expr(std::shared_ptr<CallExprAST> Caller);
std::shared_ptr<ExprAST> eval_unary_op_expr(std::shared_ptr<UnaryOpExprAST> expr);
/* Binary op expr */
std::shared_ptr<ExprAST> eval_binary_op_expr(std::shared_ptr<BinaryOpExprAST> expr);
std::shared_ptr<ExprAST> eval_one_bin_op_expr(std::shared_ptr<ExprAST> E);
std::shared_ptr<ExprAST> eval_bin_op_expr_helper(const std::string& Op, std::shared_ptr<ExprAST> LHS, std::shared_ptr<ExprAST> RHS);
/* Block */
std::shared_ptr<ExprAST> eval_block(std::vector<std::shared_ptr<ExprAST>>& Statement);
void eval()
{
for (auto& i : Expression)
{
EvalLineNumber = i->LineNumber;
eval_one(i);
}
}
// API (Interpreter)
std::shared_ptr<ExprAST> eval_one(std::shared_ptr<ExprAST> expr)
{
switch (expr->SubType)
{
case Type::function_expr:
return eval_function_expr(ptr_to<FunctionAST>(expr));
default:
return eval_expression(expr);
}
return nullptr;
}
std::shared_ptr<ExprAST> eval_expression(std::shared_ptr<ExprAST> E)
{
switch (E->SubType)
{
case Type::return_expr: case Type::break_expr: case Type::continue_expr:
case Type::integer_expr: case Type::float_expr: case Type::string_expr:
case Type::variable_expr:
return E;
case Type::if_else_expr:
return eval_if_else(ptr_to<IfExprAST>(E));
case Type::for_expr:
return eval_for(ptr_to<ForExprAST>(E));
case Type::while_expr:
return eval_while(ptr_to<WhileExprAST>(E));
case Type::do_while_expr:
return eval_do_while(ptr_to<DoWhileExprAST>(E));
case Type::unary_op_expr:
return eval_unary_op_expr(ptr_to<UnaryOpExprAST>(E));
case Type::binary_op_expr:
return eval_binary_op_expr(ptr_to<BinaryOpExprAST>(E));
case Type::call_expr:
return eval_call_expr(ptr_to<CallExprAST>(E));
default:
E->print_ast();
eval_err("Illegal statement");
}
return nullptr;
}
void eval_err(const std::string& loginfo)
{
std::cerr << "[Eval Error] in line: " << EvalLineNumber << std::endl;
std::cerr << loginfo << std::endl;
exit(1);
}
// If need check type to assign
std::shared_ptr<ExprAST> assign(const std::shared_ptr<VariableExprAST> LHS, const std::shared_ptr<ExprAST> RHS)
{ return nullptr; }
std::shared_ptr<ExprAST> _add(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _sub(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _mul(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _div(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _mod(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _not(const std::shared_ptr<ExprAST> RHS); /* '!' */
std::shared_ptr<ExprAST> _greater(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _less(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _not_more(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _not_less(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _equal(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _and(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _or(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_rshift(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_lshift(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_and(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_or(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_xor(const std::shared_ptr<ExprAST> LHS, const std::shared_ptr<ExprAST> RHS);
std::shared_ptr<ExprAST> _bit_not(const std::shared_ptr<ExprAST> RHS); /* '~' */
};
}
#endif