-
Notifications
You must be signed in to change notification settings - Fork 0
/
may_ao.h
194 lines (185 loc) · 3.66 KB
/
may_ao.h
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
#include<vector>
#include<iostream>
using namespace std;
int b = 0;
int base(int L) {
int c = b;
while (L > 0) {
c = Stack[c + 3];
L --;
}
return c;
}
void intepreter(vector<Instruction> Code){
int Exit = 0;
int pc = 0;
int t = 0;
do {
// cout<<"lenh "<< pc <<endl;
switch (Code[pc].Op) {
case OP_LA: t ++;
Stack[t] = base(Code[pc].p) + Code[pc].q;
break;
case OP_LV: t ++;
Stack[t] = Stack[base(Code[pc].p) + Code[pc].q];
break;
case OP_LC: t ++;
Stack[t] = Code[pc].q;
break;
case OP_LI:
Stack[t] = Stack[Stack[t]];
break;
case OP_INT:
t += Code[pc].q;
break;
case OP_DCT:
t -= Code[pc].q;
break;
case OP_J:
pc = Code[pc].q - 1;
break;
case OP_FJ:
if(Stack[t]==0){
pc = Code[pc].q - 1;
}
t -= 1;
break;
case OP_HLT:
break;
case OP_ST:
Stack[Stack[t-1]] = Stack[t];
t -= 2;
break;
case OP_CALL:
Stack[t+2] = b; // Dynamic Link
Stack[t+3] = pc; // Return Address
Stack[t+4] = base(Code[pc].p); // Static Link
b = t + 1; // Base & Result
pc = Code[pc].q - 1;
break;
case OP_EP:
t = b-1;
pc = Stack[b+2];
b = Stack[b+1];
break;
case OP_EF:
t = b;
pc = Stack[b+2];
b = Stack[b+1];
break;
case OP_RC:
char temp;
cin >>temp;
Stack[Stack[t]] = temp;
t -=1;
break;
case OP_RI:
int temp2;
cin>>temp2;
Stack[Stack[t]] = temp2;
t -=1;
break;
case OP_WRC:
char temp3;
temp3 = Stack[t];
cout<<temp3;
t -=1;
break;
case OP_WRI:
cout<<Stack[t]<<" ";
t -=1;
break;
case OP_WLN:
cout<<endl;
break;
case OP_ADD:
t -= 1;
Stack[t] = Stack[t] + Stack[t+1];
break;
case OP_SUB:
t -= 1;
Stack[t] = Stack[t] - Stack[t+1];
break;
case OP_MUL:
t -= 1;
Stack[t] = Stack[t] * Stack[t+1];
break;
case OP_DIV:
t -= 1;
Stack[t] = Stack[t] / Stack[t+1];
break;
case OP_NEG:
Stack[t] = -Stack[t];
break;
case OP_CV:
Stack[t+1] = Stack[t];
t += 1;
break;
case OP_EQ:
t -= 1;
if (Stack[t] == Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_NE:
t -= 1;
if (Stack[t] != Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_GT:
t -= 1;
if (Stack[t] > Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_LT:
t -= 1;
if (Stack[t] < Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_GE:
t -= 1;
if (Stack[t] >= Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_LE:
t -= 1;
if (Stack[t] <= Stack[t+1]){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_ODD:
if(Stack[t]%2==0){
Stack[t] = 1;
}
else{
Stack[t] = 0;
}
break;
case OP_BP:
Exit = 1;
} //switch
pc++;
}while(Exit == 0);
}//intepreter