-
Notifications
You must be signed in to change notification settings - Fork 0
/
generationZero.cpp
287 lines (252 loc) · 20 KB
/
generationZero.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
278
279
280
281
282
283
284
285
286
287
/*
Created by
Ethan Roberts
on 08/23/2019
This program is an interpreter. This program will read from a text file, interpret each line,
and then execute according to the interpretation.
example of file-format that will be read-in: +0001000002
+0000000003
+0256000004
+0250000005
-7123451234
-7111111111
+9999999999
+8000000777
+8000000778
-8777000000
-8003000000
+9999999999
*/
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void looper();
struct interpreter {
string programAry[999];
long long int dataAry[999];
bool endOfInitialData = false;
string *IP = nullptr;
string *restore;
string instruction;
int firstOct = 0;
int secOct = 0;
int thirdOct = 0;
int foundNine = 0;
bool looperRunning = false;
int sevenCounter = 0;
};
interpreter interp;
// This function converts ascii to int
int convertToInt(string myString, int startSub, int endSub) {
int size = 0;
int temp = 0;
int number = 0;
temp = myString[startSub] - '0'; //convert to int
number = temp;
for (int i = startSub + 1; i <= endSub; ++i) {
number = number * 10;
temp = myString[i] - '0';
number = number + temp;
}
return number;
}
void checkOpcode(string myString) {
char opCode = NULL;
opCode = myString[1]; //get opcode
int arraySize = 0;
int data = 0;
int index = 0;
string *restore;
if (myString[0] == '+') {
switch (opCode) {
case '0': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
data = interp.firstOct; // value that will be initialized to element
interp.dataAry[interp.thirdOct] = data;
break;
case '1': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.firstOct] + interp.dataAry[interp.secOct];
break;
case '2': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.firstOct] * interp.dataAry[interp.secOct];
break;
case '3': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.firstOct] * interp.dataAry[interp.firstOct];
break;
case '4': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
if (interp.dataAry[interp.firstOct] == interp.dataAry[interp.secOct]) {
interp.IP = &interp.programAry[interp.thirdOct]; // loop back to location
}
break;
case '5': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
if (interp.dataAry[interp.firstOct] >= interp.dataAry[interp.secOct]) {
interp.IP = &interp.programAry[interp.thirdOct]; // loop back to location
}
break;
case '6': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7); // array index
interp.thirdOct = convertToInt(myString, 8, 10);
index = interp.dataAry[interp.secOct];
interp.dataAry[interp.thirdOct] = interp.dataAry[index];
break;
case '7': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
++interp.sevenCounter;
if (interp.dataAry[interp.firstOct] < interp.dataAry[4]) { //element 4 is array size
++interp.dataAry[interp.firstOct]; //increment i
interp.IP = &interp.programAry[interp.thirdOct]; // loop back to location
if (interp.looperRunning == false) {
interp.looperRunning = true;
looper();
}
}
break;
case '8': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7); // array index
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.thirdOct]; // move data into temp location
break;
default: interp.endOfInitialData = true; // +9 was encountered
break;
}
}
else if (myString[0] == '-') {
switch (opCode) {
break;
case '1': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.firstOct] - interp.dataAry[interp.secOct];
break;
case '2': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.firstOct] / interp.dataAry[interp.secOct];
break;
case '3': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = sqrt(interp.dataAry[interp.firstOct]);
break;
case '4': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
if (interp.dataAry[interp.firstOct] != interp.dataAry[interp.secOct]) {
interp.IP = &interp.programAry[interp.thirdOct]; // loop back to location
}
break;
case '5': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7);
interp.thirdOct = convertToInt(myString, 8, 10);
if (interp.dataAry[interp.firstOct] < interp.dataAry[interp.secOct]) {
interp.IP = &interp.programAry[interp.thirdOct]; // loop back to location
}
break;
case '6': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7); // array index
interp.thirdOct = convertToInt(myString, 8, 10);
index = interp.dataAry[interp.thirdOct]; //getting next available array element
interp.dataAry[index] = interp.dataAry[interp.firstOct]; // moving temp into array
break;
case '7': interp.firstOct = convertToInt(myString, 2, 10); // special case for converting a 9-digit number
if (interp.dataAry[777] == 0) {
interp.dataAry[777] = interp.firstOct;
}
else{
interp.dataAry[778] = interp.firstOct;
}
break;
case '8': interp.firstOct = convertToInt(myString, 2, 4);
interp.secOct = convertToInt(myString, 5, 7); // array index
interp.thirdOct = convertToInt(myString, 8, 10);
interp.dataAry[interp.thirdOct] = interp.dataAry[interp.thirdOct]; // move data into temp location
cout << interp.dataAry[interp.firstOct] << endl;
break;
default: interp.endOfInitialData = true; // +9 was encountered
break;
}
}
}
void clearArrays() {
for (int i = 0; i < 999; ++i) {
interp.dataAry[i] = 0;
interp.programAry[i] = "";
}
}
void looper() {
int start = interp.dataAry[interp.firstOct];
int end = interp.dataAry[4];
for (int i = start; i <= end; ++i) {
while (interp.sevenCounter == 0) {
interp.instruction = *interp.IP; // read instruction
++interp.IP;
checkOpcode(interp.instruction); // select which opcode instruction to use
}
interp.sevenCounter = 0;
}
interp.IP = interp.restore; // leaving +7 loop, restore to location where we left off
interp.looperRunning = false;
interp.sevenCounter = 0;
}
int main()
{
ifstream myFile;
string line = "";
int executeLines = 0;
int initCounter = 0; //counts how many initialization variables there are
int lineCounter = 0;
clearArrays(); //initializing data array before interpreter starts
myFile.open("data.txt");
if (myFile.is_open()) {
while (getline(myFile, line)) {
if (line[1] == '9') { // Flag that marks the end of a section of code
interp.endOfInitialData = true;
++interp.foundNine;
}
if (interp.endOfInitialData == true && interp.foundNine == 1 && line[1] != '9') { // begin counting actual execution code
interp.programAry[executeLines] = line;
++executeLines;
}
if (interp.endOfInitialData == true && interp.foundNine == 2) { // checking for flag marking beginning of Input Data
if (line[1] != '9') {
checkOpcode(line);
}
}
if (interp.endOfInitialData == false && interp.foundNine == 0) { // counts number of initial variables
++initCounter;
checkOpcode(line);
}
line = ""; // reset
++lineCounter;
}
}
// The machine instructions have been read-in. Now start interpreting
interp.IP = interp.programAry;
for (int i = 0; i < executeLines; ++i) {
interp.instruction = *interp.IP; // read instruction
if (interp.instruction == "") { // end of program has been reached
break;
}
if (interp.instruction.find('+7') != std::string::npos) { // loop is about to start, save location to come back to after looping
interp.restore = interp.IP + 1; // cite for 'find': http://www.cplusplus.com/reference/string/string/find/
}
++interp.IP;
checkOpcode(interp.instruction); // select which opcode instruction to use
}
myFile.close();
return 0;
}