forked from devaigergely81/flex-bison-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
implementation.hh
218 lines (189 loc) · 5.29 KB
/
implementation.hh
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
#ifndef IMPLEMENTATION_HH
#define IMPLEMENTATION_HH
#include <string>
#include <list>
#include <unordered_map>
enum mode {compiler, interpreter};
extern mode current_mode;
enum type {boolean, natural};
void error(int line, std::string text);
class expression {
public:
virtual type get_type() const = 0;
virtual ~expression();
virtual std::string get_code() const = 0;
virtual unsigned get_value() const = 0;
virtual bool is_const_expr() const = 0;
};
class number_expression : public expression {
public:
number_expression(std::string text);
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
unsigned value;
};
class boolean_expression : public expression {
public:
boolean_expression(bool _value);
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
bool value;
};
extern long id;
extern std::string next_label();
struct symbol {
symbol() {}
symbol(int _line, std::string _name, type _type);
void declare();
std::string get_code();
int get_size();
int line;
std::string name;
type symbol_type;
std::string label;
};
extern std::unordered_map<std::string, symbol> symbol_table;
extern std::unordered_map<std::string, unsigned> value_table;
class id_expression : public expression {
public:
id_expression(int line, std::string _name);
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
int line;
std::string name;
};
class binop_expression : public expression {
public:
binop_expression(int _line, std::string _op, expression* _left, expression* _right);
~binop_expression();
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
int line;
std::string op;
expression* left;
expression* right;
};
class trinaryop_expression : public expression {
public:
trinaryop_expression(int _line, expression *_cond, expression* _left, expression* _right);
~trinaryop_expression();
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
int line;
expression* cond;
expression* left;
expression* right;
};
class not_expression : public expression {
public:
not_expression(int _line, std::string _op, expression* _operand);
~not_expression();
type get_type() const;
std::string get_code() const;
unsigned get_value() const;
bool is_const_expr() const;
private:
int line;
std::string op;
expression* operand;
};
class instruction {
public:
instruction(int _line);
virtual ~instruction();
virtual void type_check() = 0;
virtual std::string get_code() = 0;
virtual void execute() = 0;
int get_line();
protected:
int line;
};
class assign_instructions : public instruction {
public:
assign_instructions(int _line, std::list<std::string>* _left, std::list<expression*>* right);
~assign_instructions();
void type_check();
std::string get_code();
void execute();
bool is_const_expr() const;
private:
std::list<std::string>* left;
std::list<expression*>* right;
};
class read_instruction : public instruction {
public:
read_instruction(int _line, std::string _id);
void type_check();
std::string get_code();
void execute();
private:
std::string id;
};
class write_instruction : public instruction {
public:
write_instruction(int _line, expression* _exp);
~write_instruction();
void type_check();
std::string get_code();
void execute();
private:
expression* exp;
type exp_type;
};
class if_instruction : public instruction {
public:
if_instruction(int _line, expression* _condition, std::list<instruction*>* _true_branch, std::list<instruction*>* _false_branch);
~if_instruction();
void type_check();
std::string get_code();
void execute();
private:
expression* condition;
std::list<instruction*>* true_branch;
std::list<instruction*>* false_branch;
};
class while_instruction : public instruction {
public:
while_instruction(int _line, expression* _condition, std::list<instruction*>* _body);
~while_instruction();
void type_check();
std::string get_code();
void execute();
private:
expression* condition;
std::list<instruction*>* body;
};
class for_instruction : public instruction {
public:
for_instruction(int _line, std::string _id, expression* _lowerlimit, expression* _upperlimit, std::list<instruction*>* _body);
~for_instruction();
void type_check();
std::string get_code();
void execute();
private:
std::string id;
expression* lowerlimit;
expression* upperlimit;
std::list<instruction*>* body;
};
void type_check_commands(std::list<instruction*>* commands);
void generate_code_of_commands(std::ostream& out, std::list<instruction*>* commands);
void execute_commands(std::list<instruction*>* commands);
void delete_commands(std::list<instruction*>* commands);
void generate_code(std::list<instruction*>* commands);
#endif // IMPLEMENTATION_HH