-
Notifications
You must be signed in to change notification settings - Fork 0
/
compilation_engine.h
387 lines (333 loc) · 9.66 KB
/
compilation_engine.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifndef _COMP_ENGINE_H
#define _COMP_ENGINE_H
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <stdexcept>
#include <boost/filesystem.hpp>
#include "jack_tokenizer.h"
#include "vm_writer.h"
#include "symbol_table.h"
using std::string;
using std::ofstream;
using std::vector;
using std::ostringstream;
using boost::filesystem::path;
class CompilationEngine {
public:
CompilationEngine(JackTokenizer &jtok)
:m_jtok(jtok) {}
virtual ~CompilationEngine() {}
virtual void compileClass() =0;
virtual void compileClassVarDec() =0;
virtual void compileSubroutine() =0;
virtual void compileParameterList() =0;
virtual void compileVarDec() =0;
virtual void compileStatements() =0;
virtual void compileDo() =0;
virtual void compileLet() =0;
virtual void compileWhile() =0;
virtual void compileReturn() =0;
virtual void compileIf() =0;
virtual void compileExpression() =0;
virtual void compileTerm() =0;
virtual void compileExpressionList() =0;
protected:
JackTokenizer &m_jtok;
};
class XMLCompilationEngine : public CompilationEngine {
public:
XMLCompilationEngine(JackTokenizer &jtok, path p);
virtual void compileClass();
virtual void compileClassVarDec();
virtual void compileSubroutine();
virtual void compileParameterList();
virtual void compileVarDec();
virtual void compileStatements();
virtual void compileDo();
virtual void compileLet();
virtual void compileWhile();
virtual void compileReturn();
virtual void compileIf();
virtual void compileExpression();
virtual void compileTerm();
virtual void compileExpressionList();
private:
string m_indent;
vector<char> m_op, m_unaryOp;
vector<TYPE_KEYWORD> m_kwConstant, m_classVarDec, m_type, m_subroutineDec, m_statement;
ofstream m_out;
string m_fileName;
// increment/decrement tabulation needed for XML reading comfort
void Inc_tab();
void Dec_tab();
// outputting functions
void outputIdentifier(string type);
void outputClassName();
void outputVarName();
void outputSubroutineName();
void outputKeyword( TYPE_KEYWORD tk );
void outputKeyword( vector<TYPE_KEYWORD> tk );
void outputSymbol( char symbol );
void outputSymbol( vector<char> symbols );
void outputIntegerConstant();
void outputStringConstant();
void outputKeywordConstant();
void outputOp();
void outputUnaryOp();
void outputType();
void outputSubroutineCall();
};
class JackCompilationEngine : public CompilationEngine {
public:
JackCompilationEngine( JackTokenizer &jtok, path p, map<string,SubroutineInfo> ref_methods = map<string,SubroutineInfo>() );
virtual void compileClass();
virtual void compileClassVarDec();
virtual void compileSubroutine();
virtual void compileParameterList();
virtual void compileVarDec();
virtual void compileStatements();
virtual void compileDo();
virtual void compileLet();
virtual void compileWhile();
virtual void compileReturn();
virtual void compileIf();
virtual void compileExpression();
virtual void compileTerm();
virtual void compileExpressionList();
void compileSubroutineCall();
void pushIdentifier(KIND kind, int index);
void popIdentifier(KIND kind, int index);
// map< method name, number of LocalVar >
map<string, SubroutineInfo> getMethodList();
private:
// vmwriter
VMWriter m_VMOutput;
// symbol table
SymbolTable m_symTab;
// used to format VM functions
string m_fileName, m_className;
// used to know each subroutine context (construct, method, ...)
string m_currentSubroutine_name, m_currentSubroutine_kind, m_currentSubroutine_type;
/* we need to determine every subroutine (specially methods) of the class BEFORE
* outputting VM inst. so we'll make 2 pass.
* The 1st one is to fill this member class
*/
map<string, SubroutineInfo> m_classSubroutine_params;
// count how many arguments used for other subroutines called
int m_externSubroutine_params;
// 'if' and 'while' counters
int m_ifCounter, m_whileCounter;
// keywords and symbols constants
vector<char> m_op, m_unaryOp;
vector<TYPE_KEYWORD> m_kwConstant, m_classVarDec, m_type, m_subroutineDec, m_statement;
// inspecting functions
void inspectIdentifier();
void inspectClassName();
void inspectVarName();
void inspectSubroutineName();
void inspectKeyword( TYPE_KEYWORD tk );
void inspectKeyword( vector<TYPE_KEYWORD> tk );
void inspectSymbol( char symbol );
void inspectSymbol( vector<char> symbols );
void inspectIntegerConstant();
void inspectStringConstant();
void inspectKeywordConstant();
void inspectOp();
void inspectUnaryOp();
void inspectType();
};
/** Handled exception */
class JackError : public std::exception {
public:
JackError( string message, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << message;
this->msg = oss.str();
}
virtual ~JackError() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class IdentifierNotFound : public std::exception {
public:
IdentifierNotFound( string type, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "\"" << type << "\" not found";
this->msg = oss.str();
}
virtual ~IdentifierNotFound() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class KeywordNotFound : public std::exception {
public:
KeywordNotFound( TYPE_KEYWORD keyword, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "\"" << keyword_to_string( keyword ) << "\" not found";
this->msg = oss.str();
}
KeywordNotFound( vector<TYPE_KEYWORD> keywords, string filename, int line, int column )
{
ostringstream oss;
string sep = ", ";
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
for ( vector<TYPE_KEYWORD>::iterator it = keywords.begin(), it_end = keywords.end();
it != it_end;
++it)
{
oss << "'" << keyword_to_string( *it ) << "'" << sep;
}
// remove the last separator
long pos = oss.tellp();
oss.seekp( pos - sep.size() );
oss << " not found";
this->msg = oss.str();
}
virtual ~KeywordNotFound() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class SymbolNotFound : public std::exception {
public:
SymbolNotFound( char symbol, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "\"" << symbol << "\" not found";
this->msg = oss.str();
}
SymbolNotFound( vector<char> symbols, string filename, int line, int column )
{
ostringstream oss;
string sep = ", ";
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
for ( vector<char>::iterator it = symbols.begin(), it_end = symbols.end();
it != it_end;
++it)
{
oss << "'" << *it << "'" << sep;
}
// remove the last separator
long pos = oss.tellp();
oss.seekp( pos - sep.size() );
oss << " not found";
this->msg = oss.str();
}
virtual ~SymbolNotFound() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class WrongType : public std::exception {
public:
WrongType( TYPE_TOKEN tt, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "\"" << token_to_string(tt) << "\" is not a valid type";
this->msg = oss.str();
}
virtual ~WrongType() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class InvalidType : public std::exception {
public:
InvalidType( string type, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "\"" << type << "\" is illegal here";
this->msg = oss.str();
}
virtual ~InvalidType() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class WrongToken : public std::exception {
public:
WrongToken( TYPE_TOKEN expected, TYPE_TOKEN found, string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "Expected a \"" << token_to_string(expected) << "\" token";
oss << ", found a \"" << token_to_string(found) << "\" one";
this->msg = oss.str();
}
virtual ~WrongToken() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
class MissingStringConstant : public std::exception {
public:
MissingStringConstant( string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "Missing string constant";
this->msg = oss.str();
}
virtual ~MissingStringConstant() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
/* May not be actually thrown because some programs
* exploit the binary representation of -1
*/
class IntegerOutOfRange : public std::exception {
public:
IntegerOutOfRange( string filename, int line, int column )
{
ostringstream oss;
oss << "Error in \"" << filename << "\" Ln " << line << ", Col " << column << " : ";
oss << "Integer value is out of range [0..32767]";
this->msg = oss.str();
}
virtual ~IntegerOutOfRange() throw() {}
virtual const char* what() const throw()
{
return this->msg.c_str();
}
private:
string msg;
};
#endif