-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.cpp
277 lines (238 loc) · 7.07 KB
/
calculator.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
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
#include "calculator.hpp"
#include <iostream>
#include <sstream>
#include <memory>
using std::unique_ptr;
/**
* For terminals that specify an associated class, these functions takes the
* matched string from the input and returns a value of that class.
*/
unique_ptr<Num>
scan_num(Table* table, const std::string& text) {
return std::make_unique<Num>(std::stoi(text));
}
unique_ptr<Num>
scan_hex(Table* table, const std::string& text)
{
std::stringstream stream;
stream << std::hex << text;
int num = 0;
stream >> num;
return std::make_unique<Num>(num);
}
unique_ptr<Ident>
scan_ident(Table* table, const std::string& text) {
return std::make_unique<Ident>(text);
}
/**
* Functions called when its associated rule is matched. Every function is
* passed arguments for each terminal in the rule that has an associated type.
* If any function is not implemented, including with the exact argument types,
* then link errors will occur when building the final program.
*/
unique_ptr<Num>
reduce_total(Table* table, unique_ptr<Num>& E1) {
return std::move(E1);
}
unique_ptr<Num>
reduce_line(Table* table, unique_ptr<Num>& E1) {
return std::move(E1);
}
unique_ptr<Num>
reduce_assign(Table* table,
unique_ptr<Ident>& E1,
unique_ptr<Num>& E2) {
table->vars[E1->name] = E2->value;
return std::move(E2);
}
unique_ptr<Num>
reduce_exit(Table* table) {
table->done = true;
return std::make_unique<Num>(0);
}
unique_ptr<Num>
reduce_add_mul(Table* table,
unique_ptr<Num>& E1,
unique_ptr<Num>& E2) {
unique_ptr<Num> result = std::move(E1);
result->value += E2->value;
return result;
}
unique_ptr<Num>
reduce_sub_mul(Table* table,
unique_ptr<Num>& E1,
unique_ptr<Num>& E2) {
unique_ptr<Num> result = std::move(E1);
result->value -= E2->value;
return result;
}
unique_ptr<Num>
reduce_mul_int(Table* table,
unique_ptr<Num>& E1,
unique_ptr<Num>& E2) {
unique_ptr<Num> result = std::move(E1);
result->value *= E2->value;
return result;
}
unique_ptr<Num>
reduce_div_int(Table* table,
unique_ptr<Num>& E1,
unique_ptr<Num>& E2) {
unique_ptr<Num> result = std::move(E1);
result->value /= E2->value;
return result;
}
unique_ptr<Num>
reduce_paren(Table* table, unique_ptr<Num>& E1) {
return std::move(E1);
}
unique_ptr<Num>
reduce_lookup(Table* table, unique_ptr<Ident>& E1) {
return std::make_unique<Num>(table->vars[E1->name]);
}
/**
* Functions provided by the lexer for identifing terminals given the input
* characters. If no node is returned, check the current node for a matching
* symbol and call scan to get its value.
*/
Node* node_next(Node* node, int c);
Symbol* node_accept(Node* node);
Value* node_scan(Node* node, Table*, const std::string&);
/**
* Functions provided by the parser. For each new symbol the parser determines
* if the symbol should be pushed onto the stack, or reduced by a rule of the
* grammar. If the top symbols are reduced by a rule, call the reduce function
* to determine the new value for the top of the stack. After reducing the
* rule, call goto to determine the next state of the parser.
*/
struct Rule;
State* find_shift(State* state, Symbol* sym);
Rule* find_reduce(State* state, Symbol* sym, bool* accept);
Symbol* rule_nonterm(Rule* rule, size_t* length);
Value* rule_reduce(Rule* rule, Table*, Value**);
State* find_goto(State* state, Symbol* sym);
/**
* At startup, the lexer for finding terminals is in its initial node. For the
* parser, the stack is cleared and the initial state is placed onto the top of
* the stack.
*/
void
Calculator::start()
{
states.clear();
symbols.clear();
values.clear();
node = &node0;
text.clear();
states.push_back(&state0);
};
/**
* The calculator calls scan with the next input character to match terminals in
* input. If a terminal is found, the calculator updates the stack based on
* the action in the parse table. Based on the action in the parse table, the
* new symbol is either shifted onto the stack, or the stack is reduced by a
* rule and the user defined action is called with the values on top of the
* stack as arguments.
*/
bool
Calculator::scan(Table* table, int c)
{
while (true)
{
if (node == &node0 && isspace(c)) {
return true;
}
else {
Node* next = node_next(node, c);
if (next) {
text.push_back(c);
node = next;
return true;
}
else {
if (!node_accept(node)) {
return false;
}
Value* value = node_scan(node, table, text);
if (!advance(table, node_accept(node), value)) {
return false;
}
node = &node0;
text.clear();
}
}
}
}
/**
* The scanning process is continued until the end of the input. At the end
* of the input the end mark symbol reduces the remaining symbols still on the
* stack into a single value.
*/
std::unique_ptr<Value>
Calculator::scan_end(Table* table)
{
Symbol* accept = node_accept(node);
if (accept) {
Value* value = node_scan(node, table, text);
if (!advance(table, accept, value)) {
return nullptr;
}
} else if (node != &node0) {
return nullptr;
}
if (!advance(table, &endmark, nullptr)) {
return nullptr;
}
return std::unique_ptr<Value>(values.front());
};
/**
* After a terminal is found in the input, it is either shifted onto the top of
* the stack or the stack is reduced by one of the predefined rules in the
* grammar. If the stack is reduced, the corresponding user defined function is
* called with the values on the top of the stack as arguments.
*/
bool
Calculator::advance(Table* table, Symbol* sym, Value* val)
{
while (true)
{
State* top = states.back();
State* next = find_shift(top, sym);
if (next) {
push(next, sym, val);
return true;
}
bool accept = false;
Rule* rule = find_reduce(top, sym, &accept);
if (rule) {
size_t length = 0;
Symbol* nonterm = rule_nonterm(rule, &length);
Value* result = rule_reduce(rule, table, values.data() + values.size());
pop(length);
top = states.back();
State* found = find_goto(top, nonterm);
push(found, nonterm, result);
} else {
return false;
}
if (accept) {
return true;
}
}
};
void
Calculator::push(State* state, Symbol* sym, Value* val)
{
states.push_back(state);
symbols.push_back(sym);
values.push_back(val);
}
void
Calculator::pop(size_t count)
{
for (size_t i = 0; i < count; i++) {
states.pop_back();
symbols.pop_back();
values.pop_back();
}
};