-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-generator.h
193 lines (153 loc) · 4.09 KB
/
code-generator.h
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
#include <sstream>
#include "assembler-parser.tab.h"
using namespace std;
enum Opcode {
OP_ADD = 0x0100,
OP_AND = 0x0200,
OP_ASSIGN = 0x0300,
OP_CALL = 0x0400,
OP_CMP = 0x0500,
OP_CMPGT = 0x0600,
OP_CMPGTE = 0x0700,
OP_CMPLT = 0x0800,
OP_CMPLTE = 0x0900,
OP_CMPNE = 0x0A00,
OP_FUNC = 0x0B00,
OP_OR = 0x0C00,
OP_PUSH = 0x0D00,
OP_STR = 0x0001,
OP_INT = 0x0002,
OP_DBL = 0x0003,
OP_IDENT = 0x0004,
OP_NIL = 0x0005,
};
class Instruction {
public:
virtual ~Instruction() {}
virtual void text( ostream& oss ) const = 0;
virtual void bytecode( ostream& oss ) const = 0;
static Opcode TokenToOpcode( int token );
};
class ADD : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class AND : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class ASSIGN : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CALL : public Instruction {
public:
CALL( const string& ident_, long long argc_ )
: ident( ident_ ), argc( argc_ ) {}
private:
string ident;
long long argc;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMP : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMPGT : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMPGTE : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMPLT : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMPLTE : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class CMPNE : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class FUNCDEF : public Instruction {
public:
FUNCDEF( const string& ref_, int argc_ )
: ref( ref_ ), argc( argc_ ) {}
private:
string ref;
int argc;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class JZ : public Instruction {
public:
JZ( const string& ref_ )
: ref( ref_ ) {}
private:
string ref;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class OR : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class PUSH : public Instruction {
public:
PUSH( int type_, long long value_ )
: type( type_ ) {
ostringstream oss;
oss << value_;
value = oss.str();
}
PUSH( int type_, double value_ )
: type( type_ ) {
ostringstream oss;
oss << value_;
value = oss.str();
}
PUSH( int type_, string value_ )
: type( type_ ), value( value_ ) {}
private:
int type;
string value;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class POP : public Instruction {
public:
POP( const string& ident_ )
: ident( ident_ ) {}
private:
string ident;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class REFERENCEDEF : public Instruction {
public:
REFERENCEDEF( const string& ref_ )
: ref( ref_ ) {}
private:
string ref;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class RET : public Instruction {
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};
class STRINGDEF : public Instruction {
public:
STRINGDEF( const string& ref_, int len, const string& str_ )
: ref( ref_ ), str( str_.data(), len ) {}
private:
string ref;
string str;
virtual void text( ostream& oss ) const;
virtual void bytecode( ostream& oss ) const;
};