-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.hpp
195 lines (166 loc) · 3.99 KB
/
model.hpp
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
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
class Generator
{
public:
static int counter_label;
static int counter_var;
static void init()
{
counter_label = 0;
counter_var = 0;
}
static std::string make_label()
{
std::string temp = "__label__";
temp += std::to_string(counter_label++);
return temp;
}
static std::string make_var()
{
std::string temp = "__temp__";
temp += std::to_string(counter_var++);
return temp;
}
};
class ASTNode
{
public:
virtual std::string gencode() = 0;
};
class Expr : public ASTNode
{
public:
Expr(Expr *p1, std::string op, Expr *p2) : p1(p1), op(op), p2(p2) {}
virtual ~Expr()
{
delete p1;
delete p2;
}
virtual std::string gencode()
{
std::stringstream ss;
ss << p1->gencode() << p2->gencode();
std::string temp = Generator::make_var();
ss << op << ' ' << temp << ", " << p1->ret_var << ", " << p2->ret_var << '\n';
ret_var = temp;
return ss.str();
}
std::string ret_var;
protected:
Expr() {}
Expr *p1 = nullptr, *p2 = nullptr;
std::string op;
};
class ExprID : public Expr
{
public:
ExprID(std::string name) : name(name) {}
virtual std::string gencode()
{
ret_var = name;
return "";
}
protected:
std::string name;
};
class ExprNumber : public Expr
{
public:
ExprNumber(int number) : number(number) {}
virtual std::string gencode()
{
std::string temp = Generator::make_var();
ret_var = temp;
std::stringstream ss;
ss << "= " << temp << ", " << std::to_string(number) << '\n';
return ss.str();
}
protected:
int number;
};
class Statement : public ASTNode
{
};
class StatementList : public ASTNode
{
public:
StatementList() {}
virtual ~StatementList()
{
for (auto s : stat_vec)
{
delete s;
}
}
void append(Statement *s) { stat_vec.push_back(s); }
virtual std::string gencode() {
std::stringstream ss;
for (auto s : stat_vec) {
ss << s->gencode();
}
return ss.str();
}
protected:
std::vector<Statement *> stat_vec;
};
class IfElseStatement : public Statement
{
public:
IfElseStatement(Expr *bool_expr, StatementList *then_block, StatementList *else_block)
: bool_expr(bool_expr), then_block(then_block), else_block(else_block) {}
virtual std::string gencode() {
std::stringstream ss;
ss << bool_expr->gencode();
std::string l0, l1, l2;
l0 = Generator::make_label();
l1 = Generator::make_label();
l2 = Generator::make_label();
ss << "?:= " << l0 << ", " << bool_expr->ret_var << '\n';
ss << ":= " << l1 << '\n';
ss << ": " << l0 << '\n';
ss << then_block->gencode();
ss << ":= " << l2 << '\n';
ss << ": " << l1 << '\n';
ss << else_block->gencode();
ss << ": " << l2 << '\n';
return ss.str();
}
protected:
Expr *bool_expr;
StatementList *then_block;
StatementList *else_block;
};
class ExprStatement : public Statement
{
public:
ExprStatement(Expr *expr) : expr(expr) {}
virtual ~ExprStatement() { delete expr; }
virtual std::string gencode() {
return expr->gencode();
}
protected:
Expr *expr;
};
class DefineStatement : public Statement
{
public:
DefineStatement(std::string name) : name(name) {}
DefineStatement(std::string name, Expr *expr) : name(name), expr(expr) {}
virtual ~DefineStatement() { delete expr; }
virtual std::string gencode()
{
std::stringstream ss;
ss << ". " << name << '\n';
if (expr) {
ss << expr->gencode();
ss << "= " << name << ", " << expr->ret_var << '\n';
}
return ss.str();
}
protected:
std::string name;
Expr *expr = nullptr;
};