-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
221 lines (195 loc) · 4.64 KB
/
Parser.cpp
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
#include "Parser.hpp"
// Parser::Parser(void)
// {
// return;
// }
Parser::Parser(std::vector<std::vector<std::string>> & input) : _instructs(input)
{
initFunctSimpleArray();
return;
}
Parser::Parser(Parser const & src) : _instructs(src._instructs), _array(src._array)
{
*this = src;
for (int i = 0; i < 9; i++)
_functSimpleArray[i] = src._functSimpleArray[i];
return;
}
Parser::~Parser(void)
{
return;
}
Parser & Parser::operator=(Parser const & rhs)
{
_instructs = rhs._instructs;
_array = rhs._array;
for (int i = 0; i < 9; i++)
_functSimpleArray[i] = rhs._functSimpleArray[i];
return *this;
}
std::string const Parser::toString(void) const
{
return "Parser";
}
void Parser::push(eOperandType type, std::string const value)
{
_array.push_back(Factory::instance->createOperand(type, value));
}
void Parser::pop()
{
if (_array.size() > 0)
_array.pop_back();
else
throw AvmException("can't pop an empty stack !");
}
void Parser::dump()
{
for (std::vector<const IOperand *>::iterator it = _array.begin(); it != _array.end(); it++)
std::cout << (*it)->toString() << std::endl;
}
void Parser::assert(eOperandType type, std::string const value)
{
if ((*(_array.end() - 1))->getType() != type)
throw AvmException("assert error : different types");
else if (((*(_array.end() - 1))->toString()).compare(value) != 0)
throw AvmException("assert error : different values");
}
void Parser::add()
{
if (_array.size() >= 2)
{
IOperand const *a = *(_array.end() - 1);
pop();
IOperand const *b = *(_array.end() - 1);
pop();
_array.push_back(*b + *a);
}
else
throw AvmException("not enough elements to perform add()");
}
void Parser::sub()
{
if (_array.size() >= 2)
{
IOperand const *a = *(_array.end() - 1);
pop();
IOperand const *b = *(_array.end() - 1);
pop();
_array.push_back(*b - *a);
}
else
throw AvmException("not enough elements to perform sub()");
}
void Parser::mul()
{
if (_array.size() >= 2)
{
IOperand const *a = *(_array.end() - 1);
pop();
IOperand const *b = *(_array.end() - 1);
pop();
_array.push_back(*b * *a);
}
else
throw AvmException("not enough elements to perform mul()");
}
void Parser::div()
{
if (_array.size() >= 2)
{
IOperand const *a = *(_array.end() - 1);
pop();
IOperand const *b = *(_array.end() - 1);
pop();
_array.push_back(*b / *a);
}
else
throw AvmException("not enough elements to perform div()");
}
void Parser::mod()
{
if (_array.size() >= 2)
{
IOperand const *a = *(_array.end() - 1);
pop();
IOperand const *b = *(_array.end() - 1);
pop();
_array.push_back(*b % *a);
}
else
throw AvmException("not enough elements to perform mod()");
}
void Parser::print()
{
if ((*(_array.end() - 1))->getType() == eOperandType::Int8)
{
int tmp = stoi((*(_array.end() - 1))->toString());
std::cout << static_cast<char>(tmp);
}
else
throw AvmException("only int8 values can be print");
}
void Parser::exit()
{
std::exit(EXIT_SUCCESS);
}
void Parser::initFunctSimpleArray()
{
int i = 0;
this->_functSimpleArray[i++] = &Parser::pop;
this->_functSimpleArray[i++] = &Parser::dump;
this->_functSimpleArray[i++] = &Parser::add;
this->_functSimpleArray[i++] = &Parser::sub;
this->_functSimpleArray[i++] = &Parser::mul;
this->_functSimpleArray[i++] = &Parser::div;
this->_functSimpleArray[i++] = &Parser::mod;
this->_functSimpleArray[i++] = &Parser::print;
this->_functSimpleArray[i++] = &Parser::exit;
}
eOperandType Parser::strToType(const std::string str)
{
if (str.compare("int8") == 0)
return eOperandType::Int8;
else if (str.compare("int16") == 0)
return eOperandType::Int16;
else if (str.compare("int32") == 0)
return eOperandType::Int32;
else if (str.compare("float") == 0)
return eOperandType::Float;
else if (str.compare("double") == 0)
return eOperandType::Double;
return eOperandType::SizeMax; // Due to lexer, should never be used !!!
}
void Parser::run()
{
std::string str[] = {"pop", "dump", "add", "sub", "mul", "div", "mod", "print", "exit"};
for (size_t i = 0; i < _instructs.size() ; i++)
{
try
{
if (_instructs[i][0].compare("push") == 0)
push(strToType(_instructs[i][1]), _instructs[i][2]);
else if (_instructs[i][0].compare("assert") == 0)
assert(strToType(_instructs[i][1]), _instructs[i][2]);
else
{
for (int j = 0; j < 9; j++)
{
if (str[j].compare(_instructs[i][0]) == 0)
(this->*(_functSimpleArray[j]))();
}
}
}
catch(const AvmException& e)
{
std::cerr << "runtime error on line " << _instructs[i][_instructs[i].size() - 1] << ": ";
std::cerr << e.what() << '\n';
exit();
}
}
}
std::ostream & operator<< (std::ostream & o, Parser const & rhs)
{
o << rhs.toString();
return o;
}