-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cc
More file actions
148 lines (146 loc) · 3.41 KB
/
simulator.cc
File metadata and controls
148 lines (146 loc) · 3.41 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
#include "simulator.h"
#include <deque>
#include <ios>
#include <iostream>
#include <unordered_map>
void simulate(const Program &program) {
size_t IP = 0;
std::deque<int> stack{};
std::deque<size_t> call_stack{};
std::unordered_map<int, int> heap{};
while (true) {
if (IP >= program.instructions.size()) {
throw "Finished execution without calling end";
}
const Instruction &inst = program.instructions.at(IP);
switch (inst.type) {
case READC: {
char n;
std::cin >> std::noskipws >> n;
heap[stack.back()] = n;
stack.pop_back();
} break;
case READN: {
int n;
std::cin >> n;
heap[stack.back()] = n;
stack.pop_back();
} break;
case WRITEC: {
char c = stack.back();
stack.pop_back();
if (c == '\n')
std::cout << std::endl;
else
std::cout << c;
} break;
case WRITEN: {
int n = stack.back();
stack.pop_back();
std::cout << n;
} break;
case PUSH:
stack.push_back(inst.arg);
break;
case DUP:
stack.push_back(stack.back());
break;
case SWAP: {
int a = stack.back();
stack.pop_back();
int b = stack.back();
stack.pop_back();
stack.push_back(a);
stack.push_back(b);
} break;
case DISCARD:
stack.pop_back();
break;
case COPY:
stack.push_back(stack[stack.size() - inst.arg - 1]);
break;
case SLIDE:
// TODO: double check
stack.erase(stack.end() - inst.arg - 1, stack.end() - 1);
break;
case ADD: {
int b = stack.back();
stack.pop_back();
int a = stack.back();
stack.pop_back();
stack.push_back(a + b);
} break;
case SUB: {
int b = stack.back();
stack.pop_back();
int a = stack.back();
stack.pop_back();
stack.push_back(a - b);
} break;
case MUL: {
int b = stack.back();
stack.pop_back();
int a = stack.back();
stack.pop_back();
stack.push_back(a * b);
} break;
case DIV: {
int b = stack.back();
stack.pop_back();
int a = stack.back();
stack.pop_back();
stack.push_back(a / b);
} break;
case MOD: {
int b = stack.back();
stack.pop_back();
int a = stack.back();
stack.pop_back();
stack.push_back(a % b);
} break;
// note -1 to account for the IP++ at the end
case CALL:
call_stack.push_back(IP + 1);
IP = program.locations.at(inst.arg) - 1;
break;
case JUMP:
IP = program.locations.at(inst.arg) - 1;
break;
case JZ: {
int top = stack.back();
stack.pop_back();
if (top == 0) {
IP = program.locations.at(inst.arg) - 1;
}
} break;
case JN: {
int top = stack.back();
stack.pop_back();
if (top < 0) {
IP = program.locations.at(inst.arg) - 1;
}
} break;
case RET:
IP = call_stack.back() - 1;
call_stack.pop_back();
break;
case END:
return; // Important!
case STORE: {
int value = stack.back();
stack.pop_back();
int location = stack.back();
stack.pop_back();
heap[location] = value;
} break;
case RETRIEVE: {
int top = stack.back();
stack.pop_back();
stack.push_back(heap.at(top)); // at for bounds checking
} break;
default:
throw "Encountered invalid instruction";
}
IP++;
}
}