-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.hpp
More file actions
209 lines (166 loc) · 3.68 KB
/
Copy pathsyntax.hpp
File metadata and controls
209 lines (166 loc) · 3.68 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
#ifndef _SYNTAX_H
#define _SYNTAX_H
#include <exception>
#include <string>
#include <iostream>
#include <vector>
#include "pointer.hpp"
#include "sym.hpp"
using namespace std;
class syntax_node {
public:
virtual unsigned int eval() { throw exception(); }
virtual unsigned int *leval() { throw exception(); }
virtual void exec() { throw exception(); }
virtual ~syntax_node() {}
};
class var_node: public syntax_node {
public:
var_node(string name): _name(name) {}
unsigned int eval() {
unsigned int val;
get_var_value(_name, &val);
return val;
}
unsigned int *leval() {
return get_var_address(_name);
}
private:
string _name;
};
class imm_node: public syntax_node {
public:
imm_node(unsigned int value): _value(value) {}
unsigned int eval() {
return _value;
}
private:
unsigned int _value;
};
class arith_expr_node: public syntax_node {
public:
typedef enum { arith_add, arith_sub } arith_t;
public:
arith_expr_node(pointer<syntax_node> left, arith_t op, pointer<syntax_node> right):
_left(left), _op(op), _right(right) {}
unsigned int eval() {
unsigned int lval = _left->eval();
unsigned int rval = _right->eval();
switch(_op) {
case arith_add:
return lval + rval;
break;
case arith_sub:
return lval - rval;
}
throw exception();
}
private:
pointer<syntax_node> _left;
char _op;
pointer<syntax_node> _right;
};
class logic_expr_node: public syntax_node {
public:
typedef enum { logic_equal, logic_nequal } logic_t;
public:
logic_expr_node(pointer<syntax_node> left, logic_t op, pointer<syntax_node> right):
_left(left), _op(op), _right(right) {}
unsigned int eval() {
unsigned int lval = _left->eval();
unsigned int rval = _right->eval();
switch(_op) {
case logic_equal:
return lval == rval;
break;
case logic_nequal:
return lval != rval;
break;
}
throw exception();
}
private:
pointer<syntax_node> _left;
logic_t _op;
pointer<syntax_node> _right;
};
class assign_node: public syntax_node {
public:
assign_node(pointer<syntax_node> var, pointer<syntax_node> expr):
_var(var), _expr(expr) {}
void exec() {
*_var->leval() = _expr->eval();
}
private:
pointer<syntax_node> _var;
pointer<syntax_node> _expr;
};
class if_else_node: public syntax_node {
public:
if_else_node(pointer<syntax_node> cond, pointer<syntax_node> tstat,
pointer<syntax_node> fstat = pointer<syntax_node>(new dummy_node())):
_cond(cond), _tstat(tstat), _fstat(fstat) {}
void exec() {
if (_cond->eval()) {
_tstat->exec();
} else {
_fstat->exec();
}
}
private:
pointer<syntax_node> _cond;
pointer<syntax_node> _tstat;
pointer<syntax_node> _fstat;
private:
class dummy_node: public syntax_node {
void exec() {}
};
};
class print_node: public syntax_node {
public:
print_node(pointer<syntax_node> var): _var(var) {}
void exec() {
cout << _var->eval() << endl;
}
private:
pointer<syntax_node> _var;
};
class while_node: public syntax_node {
public:
while_node(pointer<syntax_node> cond, pointer<syntax_node> assign):
_cond(cond), _assign(assign) {}
void exec() {
while(_cond->eval()) {
_assign->exec();
}
}
private:
pointer<syntax_node> _cond;
pointer<syntax_node> _assign;
};
class block_stat_node: public syntax_node {
public:
void add_stat(pointer<syntax_node> stat) {
_stats.push_back(stat);
}
void exec() {
for(auto it = _stats.begin();
it != _stats.end();
++it) {
(*it)->exec();
}
}
private:
vector<pointer<syntax_node> > _stats;
};
class function_node: public syntax_node {
public:
function_node(vector<string> args, pointer<syntax_node> stats):
_args(args), _stats(stats) {}
void exec() {
}
private:
vector<string> _args;
pointer<syntax_node> _stats;
};
#endif