-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
364 lines (318 loc) · 18.4 KB
/
command.cpp
File metadata and controls
364 lines (318 loc) · 18.4 KB
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
//
// Created by erthax on 15.01.2021.
//
#include "instruction_generators.h"
//______________________________Command_________________________________
void Command::loadIdentifierAddress(Register *tAddressRegister, Identifier *tIdentifier, bool checkIsInitialized) {
if (tIdentifier->type == eIdentifier::VARIABLE_IDENTIFIER) {
if (checkIsInitialized) {
Command::checkIfInitialized(tIdentifier->variable);
}
if (tIdentifier->variable->variableType == eVariableType::ITERATOR) {
throw tIdentifier->variable->pid + " is an array iterator";
}
if (tIdentifier->variable->variableType == eVariableType::ARRAY) {
throw "wrong use of array variable " + tIdentifier->variable->pid;
}
code->loadNumberToRegister(tAddressRegister->getName(), tIdentifier->variable->address);
} else if (tIdentifier->type == eIdentifier::NUMBER_ARRAY_IDENTIFIER) {
uint cellAddress = tIdentifier->array->address;
if (tIdentifier->array->variableType == eVariableType::SINGLE) {
throw "wrong use of variable " + tIdentifier->array->pid;
}
if (tIdentifier->index < tIdentifier->array->start || tIdentifier->index > tIdentifier->array->end) {
throw tIdentifier->array->pid + " wrong array range";
}
cellAddress += tIdentifier->index - tIdentifier->array->start;
code->loadNumberToRegister(tAddressRegister->getName(), cellAddress);
} else {
if (tIdentifier->array->variableType == eVariableType::SINGLE) {
throw "wrong use of variable " + tIdentifier->variable->pid;
}
Command::checkIfInitialized(tIdentifier->variable);
Register *helperRegister = memory->getFreeRegister();
code->loadNumberToRegister(helperRegister->getName(),
tIdentifier->variable->address); //we load index variable address
code->load(helperRegister->getName(), helperRegister->getName()); // we load numValue of index variable
code->loadNumberToRegister(tAddressRegister->getName(),
tIdentifier->array->address);// load to register array address
code->add(tAddressRegister->getName(),
helperRegister->getName());// add to array address index variable numValue
code->loadNumberToRegister(helperRegister->getName(), tIdentifier->array->start);// load to register array start
code->sub(tAddressRegister->getName(), helperRegister->getName());// sub array start from addressRegister
helperRegister->setFull(false);
}
}
void Command::loadValueToRegister(Register *tValueRegister, Value *tValue) {
Register *addressRegister = memory->getFreeRegister();
if (tValue->type == eValue::IDENTIFIER_VALUE) { // numValue with some variable
if (tValue->identifier->type == eIdentifier::VARIABLE_IDENTIFIER &&
tValue->identifier->variable->variableType ==
eVariableType::ITERATOR) { // if its an iterator called from value we load it
// we now for sure that its a VARIABLE_IDENTIFIER
code->loadNumberToRegister(addressRegister->getName(), tValue->identifier->variable->address);
code->load(tValueRegister->getName(), addressRegister->getName());
} else {
Command::loadIdentifierAddress(addressRegister, tValue->identifier, true);
code->load(tValueRegister->getName(), addressRegister->getName());
}
} else { // numValue with a constant variable
if (tValue->identifier->variable->bInitialized) { //TODO check when its worth it to save constant instead of gen it again
Command::loadIdentifierAddress(addressRegister, tValue->identifier, true);
code->load(tValueRegister->getName(), addressRegister->getName());
} else { // if const was never created, create, save it in memory and load it for our use
code->loadNumberToRegister(tValueRegister->getName(),
tValue->numValue); // we load number value to our register
Command::loadIdentifierAddress(addressRegister, tValue->identifier,
false); // we load constant address to our register
code->store(tValueRegister->getName(),
addressRegister->getName()); // we store our value in address from addressRegister
tValue->identifier->variable->bInitialized = true; // constant is initialized
// our value is still in tValueRegister
}
}
addressRegister->setFull(false);
}
void Command::checkIfInitialized(Variable *tVaraiable) {
if (!(tVaraiable->bInitialized)) {
throw tVaraiable->pid + " was not initialized";
}
}
//____________________________ReadCommand_______________________________
void ReadCommand::generateInstructions() {
Register *addressRegister = memory->getFreeRegister();
Command::loadIdentifierAddress(addressRegister, identifier, false);
code->get(addressRegister->getName()); // we save input to address we got
if (identifier->type == eIdentifier::VARIABLE_IDENTIFIER) {
identifier->variable->bInitialized = true;
} // we set variable to initialized
memory->freeRegisters();
}
//____________________________WriteCommand_______________________________
void WriteCommand::generateInstructions() {
Register *addressRegister = memory->getFreeRegister();
if (value->type == eValue::IDENTIFIER_VALUE) { // numValue with some variable
if (value->identifier->type == eIdentifier::VARIABLE_IDENTIFIER &&
value->identifier->variable->variableType ==
eVariableType::ITERATOR) { // if its an iterator called from value we load it
// we now for sure that its a VARIABLE_IDENTIFIER
code->loadNumberToRegister(addressRegister->getName(), value->identifier->variable->address);
} else{
Command::loadIdentifierAddress(addressRegister, this->value->identifier, true);
}
} else { // numValue with a constant variable
if (value->identifier->variable->bInitialized) {
Command::loadIdentifierAddress(addressRegister,
this->value->identifier,
false); // if constant was created before, load its address
} else { // if const was never created, create and save it in memory
Register *helperRegister = memory->getFreeRegister();
code->loadNumberToRegister(helperRegister->getName(), value->numValue);
Command::loadIdentifierAddress(addressRegister, this->value->identifier, false);
code->store(helperRegister->getName(), addressRegister->getName());
value->identifier->variable->bInitialized = true; // constant is initialized
}
}
code->put(addressRegister->getName());
memory->freeRegisters();
}
//_____________________________IsCommand________________________________
void IsCommand::generateInstructions() {
// generate expression value into specified register
Register *resultRegister = memory->getFreeRegister();
expression->generateExpressionValue(resultRegister);
memory->freeRegisters();
resultRegister->setFull(true);
// save this value into address in our value in IsCommand
Register *addressRegister = memory->getFreeRegister();
Command::loadIdentifierAddress(addressRegister, identifier, false);
code->store(resultRegister->getName(), addressRegister->getName());
if (identifier->type == eIdentifier::VARIABLE_IDENTIFIER) {
identifier->variable->bInitialized = true;
} // we set variable to initialized
memory->freeRegisters();
}
//_____________________________IfCommand________________________________
void IfCommand::generateInstructions() {
Register *conditionRegister = memory->getFreeRegister();
condition->generateConditionValue(conditionRegister);
int instructionCountBeforeCommands = code->getInstructionCount();
memory->freeRegisters();
int size = this->commandList->size();
for (int i = 0; i < size; i++) {
(*commandList)[i]->generateInstructions();
}
int instructionCountAfterCommands = code->getInstructionCount();
code->putInstructionAtIndex(
new Instruction("JUMP", std::to_string(instructionCountAfterCommands - instructionCountBeforeCommands + 1)),
instructionCountBeforeCommands);
memory->freeRegisters();
}
//_____________________________IfElseCommand________________________________
void IfElseCommand::generateInstructions() {
Register *conditionRegister = memory->getFreeRegister();
condition->generateConditionValue(conditionRegister);
int instructionCountBeforeIfCommands = code->getInstructionCount();
memory->freeRegisters();
int sizeIf = this->commandListIf->size();
for (int i = 0; i < sizeIf; i++) {
(*commandListIf)[i]->generateInstructions();
}
int instructionCountBeforeElseCommands = code->getInstructionCount();
memory->freeRegisters();
int sizeElse = this->commandListElse->size();
for (int i = 0; i < sizeElse; i++) {
(*commandListElse)[i]->generateInstructions();
}
int instructionCountAfterCommands = code->getInstructionCount();
code->putInstructionAtIndex( // Jump that skips If commands
new Instruction("JUMP",
std::to_string(instructionCountBeforeElseCommands - instructionCountBeforeIfCommands + 2)),
instructionCountBeforeIfCommands);
memory->freeRegisters();
code->putInstructionAtIndex( // Jump that skips Else commands when if commands finished
new Instruction("JUMP",
std::to_string(instructionCountAfterCommands - instructionCountBeforeElseCommands + 1)),
instructionCountBeforeElseCommands + 1);
memory->freeRegisters();
}
//______________________________WhileCommand_________________________________
void WhileCommand::generateInstructions() { // very similar to if, just add jump that jumps before condition
int instructionCountBeforeCondition = code->getInstructionCount();
Register *conditionRegister = memory->getFreeRegister();
condition->generateConditionValue(conditionRegister);
int instructionCountBeforeCommands = code->getInstructionCount();
memory->freeRegisters();
int size = this->commandList->size();
for (int i = 0; i < size; i++) {
(*commandList)[i]->generateInstructions();
}
int instructionCountAfterCommands = code->getInstructionCount();
code->jump(
-(instructionCountAfterCommands - instructionCountBeforeCondition + 1)); // +1 bcs of put instruction below
code->putInstructionAtIndex(
new Instruction("JUMP", std::to_string(instructionCountAfterCommands - instructionCountBeforeCommands + 2)),
instructionCountBeforeCommands);
memory->freeRegisters();
}
//______________________________RepeatCommand_________________________________
void RepeatCommand::generateInstructions() { // very similar to if, just add jump that jumps before condition
int instructionCountBeforeCommands = code->getInstructionCount();
int size = this->commandList->size();
for (int i = 0; i < size; i++) {
(*commandList)[i]->generateInstructions();
}
Register *conditionRegister = memory->getFreeRegister();
condition->generateConditionValue(conditionRegister);
memory->freeRegisters();
int instructionCountAfterCommands = code->getInstructionCount();
code->jump(-(instructionCountAfterCommands - instructionCountBeforeCommands));
memory->freeRegisters();
}
//_______________________________ForCommand__________________________________
void ForCommand::generateInstructions() {
// create iterator variable and iterator identifier with this variable
memory->getIterator(pid)->isInsideLoop = true;
Value *iteratorValue = new Value(new Identifier(pid)); // we have value created
Register *valueRegister = memory->getFreeRegister();
Register *addressRegister = memory->getFreeRegister();
Command::loadValueToRegister(valueRegister, value1); // we load starting value to register
code->loadNumberToRegister(addressRegister->getName(),
iteratorValue->identifier->variable->address); //we load iterator address to register
code->store(valueRegister->getName(), addressRegister->getName()); // we store starting iterator val in iterator
iteratorValue->identifier->variable->bInitialized = true;
std::string iteratorEndPid = pid + std::to_string(memory->iteratorCount);
// if value2 is NUMBER_VALUE we dont have to do anything (our ending point is a constant and wont change inside a loop)
if (value2->type != eValue::NUMBER_VALUE) {// if value2 is VARIABLE_IDENTIFIER
Command::loadValueToRegister(valueRegister, value2);
memory->declareIterator(iteratorEndPid);
Value *iteratorEndValue = new Value(new Identifier(iteratorEndPid));
Command::loadValueToRegister(valueRegister, value2); // we load ending value to register
code->loadNumberToRegister(addressRegister->getName(),
iteratorEndValue->identifier->variable->address); //we load iterator address to register
code->store(valueRegister->getName(), addressRegister->getName()); // we store starting iterator val in iterator
this->value2 = iteratorEndValue;
}
memory->freeRegisters(); // we have both values prepared for for loop
int instructionCountBeforeCondition = code->getInstructionCount();
LessEqualCondition *endingCondition = new LessEqualCondition(iteratorValue, this->value2);
valueRegister = memory->getFreeRegister();
endingCondition->generateConditionValue(valueRegister);
memory->freeRegisters();
int instructionCountBeforeCommands = code->getInstructionCount();
int size = this->commandList->size();
for (int i = 0; i < size; i++) {
(*commandList)[i]->generateInstructions();
}
valueRegister = memory->getFreeRegister();
addressRegister = memory->getFreeRegister();
code->loadNumberToRegister(addressRegister->getName(),
iteratorValue->identifier->variable->address); // we load iterator address
code->load(valueRegister->getName(), addressRegister->getName()); // we load iterator value
code->inc(valueRegister->getName()); // we increase value
code->store(valueRegister->getName(), addressRegister->getName()); //we store the value
int instructionCountAfterCommands = code->getInstructionCount();
code->jump(
-(instructionCountAfterCommands - instructionCountBeforeCondition + 1)); // +1 bcs of put instruction below
code->putInstructionAtIndex(
new Instruction("JUMP", std::to_string(instructionCountAfterCommands - instructionCountBeforeCommands + 2)),
instructionCountBeforeCommands);
memory->freeRegisters();
memory->getIterator(pid)->isInsideLoop = false;
if (value2->type != eValue::NUMBER_VALUE)
memory->eraseIterator(iteratorEndPid);
}
//_____________________________ForDowntoCommand________________________________
void ForDowntoCommand::generateInstructions() {
// create iterator variable and iterator identifier with this variable
memory->getIterator(pid)->isInsideLoop = true;
Value *iteratorValue = new Value(new Identifier(pid)); // we have value created
Register *valueRegister = memory->getFreeRegister();
Register *addressRegister = memory->getFreeRegister();
Command::loadValueToRegister(valueRegister, value1); // we load starting value to register
code->loadNumberToRegister(addressRegister->getName(),
iteratorValue->identifier->variable->address); //we load iterator address to register
code->store(valueRegister->getName(), addressRegister->getName()); // we store starting iterator val in iterator
iteratorValue->identifier->variable->bInitialized = true;
std::string iteratorEndPid = pid + std::to_string(memory->iteratorCount);
// if value2 is NUMBER_VALUE we dont have to do anything (our ending point is a constant and wont change inside a loop)
if (value2->type != eValue::NUMBER_VALUE) {// if value2 is VARIABLE_IDENTIFIER
Command::loadValueToRegister(valueRegister, value2);
memory->declareIterator(iteratorEndPid);
Value *iteratorEndValue = new Value(new Identifier(iteratorEndPid));
Command::loadValueToRegister(valueRegister, value2); // we load ending value to register
code->loadNumberToRegister(addressRegister->getName(),
iteratorEndValue->identifier->variable->address); //we load iterator address to register
code->store(valueRegister->getName(), addressRegister->getName()); // we store starting iterator val in iterator
this->value2 = iteratorEndValue;
}
memory->freeRegisters(); // we have both values prepared for for loop
int instructionCountBeforeCondition = code->getInstructionCount();
GreaterEqualCondition *endingCondition = new GreaterEqualCondition(iteratorValue, this->value2);
valueRegister = memory->getFreeRegister();
endingCondition->generateConditionValue(valueRegister);
memory->freeRegisters();
int instructionCountBeforeCommands = code->getInstructionCount();
int size = this->commandList->size();
for (int i = 0; i < size; i++) {
(*commandList)[i]->generateInstructions();
}
valueRegister = memory->getFreeRegister();
addressRegister = memory->getFreeRegister();
code->loadNumberToRegister(addressRegister->getName(),
iteratorValue->identifier->variable->address); // we load iterator address
code->load(valueRegister->getName(), addressRegister->getName()); // we load iterator value
code->jzero(valueRegister->getName(), 4);
code->dec(valueRegister->getName()); // we increase value
code->store(valueRegister->getName(), addressRegister->getName()); //we store the value
int instructionCountAfterCommands = code->getInstructionCount();
code->jump(
-(instructionCountAfterCommands - instructionCountBeforeCondition + 1)); // +1 bcs of put instruction below
code->putInstructionAtIndex(
new Instruction("JUMP", std::to_string(instructionCountAfterCommands - instructionCountBeforeCommands + 2)),
instructionCountBeforeCommands);
memory->freeRegisters();
memory->getIterator(pid)->isInsideLoop = false;
if (value2->type != eValue::NUMBER_VALUE)
memory->eraseIterator(iteratorEndPid);
}