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
|
#pragma once
#include <cstring>
#include <vector>
#include <stack>
#include <iostream>
#include "Token.h"
namespace pim
{
namespace compiler
{
class VariableNotFoundException: public std::exception
{
char * error;
public:
VariableNotFoundException(std::string variable) {
error = strdup(std::string("Could not find the variable \""+variable+"\" in the current scope").c_str());
}
const char * what() const throw()
{
return error;
}
~VariableNotFoundException() throw() {};
};
class SymbolNotFoundException: public std::exception
{
char * error;
public:
SymbolNotFoundException(std::string variable) {
error = strdup(std::string("Could not find the symbol \""+variable+"\".").c_str());
}
const char * what() const throw()
{
return error;
}
~SymbolNotFoundException() throw() {};
};
class Type
{
enum { Integer = Token::IntegerSymbol, Decimal = Token::DecimalSymbol };
};
class Definition
{
public:
std::string Name;
int Type;
int StackPosition;
Definition(std::string name, int type, int position) :
Type(type),
Name(name),
StackPosition(position)
{
}
};
struct Label
{
std::string Name;
int Position;
};
class Scope
{
public:
std::vector<Definition> Definitions;
std::vector<Label> Labels;
int FrameSize;
int LocalFrameSize;
Scope():
FrameSize(0),
LocalFrameSize(0)
{
}
Definition GetDefinition(std::string name)
{
for(std::vector<Definition>::iterator iter = Definitions.begin(), end = Definitions.end(); iter != end; ++iter)
{
if((*iter).Name == name)
return *iter;
}
throw VariableNotFoundException(name);
}
};
class Generator
{
int variableType;
std::stack<Scope*> scopes;
Scope * currentScope;
std::ostream & output;
int labelCounter;
int programCounter;
typedef std::pair<int, std::string> Placeholder;
std::vector<Placeholder> placeholders;
typedef std::pair<int, int*> ValuePlaceholder;
std::vector<ValuePlaceholder> valuePlaceholders;
typedef std::pair<int, std::string> PropertyPlaceholder;
std::vector<PropertyPlaceholder> propertyPlaceholders;
typedef std::pair<int, std::string> MacroPlaceholder;
std::vector<MacroPlaceholder> macroPlaceholders;
std::vector<Label> labelPositions;
std::vector<unsigned char> program;
void defineLabel(std::string label);
void writeOpcode(int opcode);
void writeConstant(std::string constant);
void writeConstant(int constant);
void writeConstantPlaceholder(std::string label);
void writeConstantPlaceholder(int * value);
void writeConstantMacroPlaceholder(std::string macro);
void writeConstantPropertyPlaceholder(std::string property);
public:
Generator();
std::vector<unsigned char> Finish();
std::string UniqueLabel(std::string prefix);
void PushScope(std::string label);
void PushLocalScope(std::string label);
void LocalEnter();
void PopScope();
void ScopeLabel(std::string label);
void ScopeVariableType(int type);
void ScopeVariable(std::string label);
void PushVariableAddress(std::string label);
// void Store();
void LoadVariable(std::string label);
void StoreVariable(std::string label);
void Duplicate();
void Discard();
void RTConstant(std::string name);
void Constant(std::string constant);
void Increment(std::string constant);
void Add();
void Subtract();
void Multiply();
void Divide();
void Modulus();
void Negate();
void TransformParticle();
void CreateParticle();
void GetParticle();
void GetPosition();
void KillParticle();
void LoadProperty(std::string property);
void StoreProperty(std::string property);
void IntegerToDecimal();
void DecimalToInteger();
void JumpEqual(std::string label);
void JumpNotEqual(std::string label);
void JumpGreater(std::string label);
void JumpGreaterEqual(std::string label);
void JumpLess(std::string label);
void JumpLessEqual(std::string label);
void Jump(std::string label);
void Call(int arguments, std::string label);
void Return();
};
}
}
|