-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea.cpp
181 lines (159 loc) · 7.22 KB
/
tea.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
/*
*AUTHOR: Samed Kahyaoglu
*GITHUB: urtuba
*/
#define MAX_STATEMENTS 100
#include "runtime.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
AddressValue executeStatement( const Statement & statement,
const AddressValue currentAddress,
ScalarValue variables [5],
Stack<ScalarValue> & callStack,
const AddressValue addressOfCalleeIfCall,
bool & isDone )
{
(void) variables;
(void) callStack;
(void) isDone;
(void) addressOfCalleeIfCall;
//entry point
//cout << variables[0] << " " << variables[1] << " " << variables[2] << " " << variables[3] << " " << variables[4] << " "<< endl;
cout << "Executing " << statement.type << " at line " << currentAddress /*<< " degree " << callStack.degree*/ << endl;
if (statement.type.compare("call") == 0)
{
//this part finds the address of calling function by its name
AddressValue a = 0;
while(true)
{
if (((callStack.top + a) -> type.compare("function") == 0) && ((callStack.top + a) -> arg1.compare(statement.arg1) == 0)) break;
a++;
}
//then degree increases, it means a b c d e variables are re-arranged for this function
callStack.degree ++;
//one variable will be transferred to new variables due to arg2
if (statement.arg2.compare("a")==0) callStack.variables[callStack.degree][0] = variables[0];
else callStack.variables[callStack.degree][0] = 0;
if (statement.arg2.compare("b")==0) callStack.variables[callStack.degree][1] = variables[1];
else callStack.variables[callStack.degree][1] = 0;
if (statement.arg2.compare("c")==0) callStack.variables[callStack.degree][2] = variables[2];
else callStack.variables[callStack.degree][2] = 0;
if (statement.arg2.compare("d")==0) callStack.variables[callStack.degree][3] = variables[3];
else callStack.variables[callStack.degree][3] = 0;
if (statement.arg2.compare("e")==0) callStack.variables[callStack.degree][4] = variables[4];
else callStack.variables[callStack.degree][4] = 0;
//address before passing new function is recorded to array
callStack.degreeCalls[callStack.degree] = currentAddress;
return a + 1;
}
if ((addressOfCalleeIfCall != 0) && (statement.type.compare("return") == 0))
{
//if function is not main function, return statement goes next statement in parent function
//it saves one of its temporary variables into general variables before
if (statement.arg1.compare("a") == 0) callStack.variables[callStack.degree - 1][0] = variables[0];
if (statement.arg1.compare("b") == 0) callStack.variables[callStack.degree - 1][1] = variables[1];
if (statement.arg1.compare("c") == 0) callStack.variables[callStack.degree - 1][2] = variables[2];
if (statement.arg1.compare("d") == 0) callStack.variables[callStack.degree - 1][3] = variables[3];
if (statement.arg1.compare("e") == 0) callStack.variables[callStack.degree - 1][4] = variables[4];
callStack.degree --;
return addressOfCalleeIfCall + 1;
}
if ((addressOfCalleeIfCall == 0) && (statement.type.compare("return") == 0))
{
//final situation, if function is not called it is main(first condition) and if statement is return, program finishes its work.
if (statement.arg1.compare("a") == 0) cout << variables[0];
if (statement.arg1.compare("b") == 0) cout << variables[1];
if (statement.arg1.compare("c") == 0) cout << variables[2];
if (statement.arg1.compare("d") == 0) cout << variables[3];
if (statement.arg1.compare("e") == 0) cout << variables[4];
cout << endl;
callStack.isDone = true;
return 0;
}
if ((statement.type.size() == 3)) //mul,div,inc and dec are all consists of 3 chars.
{
// finding value of arg2, if it is value itself; typecasting to ScalarValue.
ScalarValue arg2;
if(statement.arg2.compare("a") == 0) arg2 = variables[0];
else if(statement.arg2.compare("b") == 0) arg2 = variables[1];
else if(statement.arg2.compare("c") == 0) arg2 = variables[2];
else if(statement.arg2.compare("d") == 0) arg2 = variables[3];
else if(statement.arg2.compare("e") == 0) arg2 = variables[4];
else arg2 = atoi(statement.arg2.c_str());
//operations with order: inc, dec, mul, div.
if (statement.type.compare("inc") == 0) {
if (statement.arg1.compare("a") == 0) variables[0] += arg2;
if (statement.arg1.compare("b") == 0) variables[1] += arg2;
if (statement.arg1.compare("c") == 0) variables[2] += arg2;
if (statement.arg1.compare("d") == 0) variables[3] += arg2;
if (statement.arg1.compare("e") == 0) variables[4] += arg2;
}
if (statement.type.compare("dec") == 0) {
if (statement.arg1.compare("a") == 0) variables[0] -= arg2;
if (statement.arg1.compare("b") == 0) variables[1] -= arg2;
if (statement.arg1.compare("c") == 0) variables[2] -= arg2;
if (statement.arg1.compare("d") == 0) variables[3] -= arg2;
if (statement.arg1.compare("e") == 0) variables[4] -= arg2;
}
if (statement.type.compare("mul") == 0) {
if (statement.arg1.compare("a") == 0) variables[0] *= arg2;
if (statement.arg1.compare("b") == 0) variables[1] *= arg2;
if (statement.arg1.compare("c") == 0) variables[2] *= arg2;
if (statement.arg1.compare("d") == 0) variables[3] *= arg2;
if (statement.arg1.compare("e") == 0) variables[4] *= arg2;
}
if (statement.type.compare("div") == 0) {
if (statement.arg1.compare("a") == 0) variables[0] /= arg2;
if (statement.arg1.compare("b") == 0) variables[1] /= arg2;
if (statement.arg1.compare("c") == 0) variables[2] /= arg2;
if (statement.arg1.compare("d") == 0) variables[3] /= arg2;
if (statement.arg1.compare("e") == 0) variables[4] /= arg2;
}
}
//except function calls and returns each statement returns next statement
return currentAddress + 1;
}
int main(int argc, char const *argv[]) {
(void) argc;
Statement statements[MAX_STATEMENTS];
Stack<ScalarValue> callStack =
{
{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},
0, statements, {0,0,0,0,0}, 0, false
};
ifstream myFile(argv[1]);
int x = 0;
string buffer;
while (myFile >> buffer) { //this while loop transfers data from file to statements array
statements[x].type = buffer;
if ((buffer.compare("function") == 0) || (buffer.compare("return") == 0))
{//this functions has 1 arg so I should skip arg2 for them.
myFile >> buffer;
statements[x].arg1 = buffer;
statements[x].arg2 = "";
} else {
myFile >> buffer;
statements[x].arg1 = buffer;
myFile >> buffer;
statements[x].arg2 = buffer;
}
x++;
}
myFile.close();
int k = 0; //k detects the address value of main function
while (true) {
k++;
if (statements[k-1].arg1.compare("main") == 0) break; //when i find main function, I can start with it's next statement. so k is 1 after main.
}
while (!callStack.isDone)
{
//I used callStack for most of data because I can reach and change call stack parts in every function (because it is passed by reference)
//callStack degreeCalls gives me chance to follow nested function connections and their return addressess
//cStack.variables[degree] gives each function its variables as a matrix row
k = executeStatement(statements[k], k, callStack.variables[callStack.degree], callStack, callStack.degreeCalls[callStack.degree], callStack.isDone);
}
return 0;
}