-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuter.h
More file actions
188 lines (162 loc) · 5.48 KB
/
Executer.h
File metadata and controls
188 lines (162 loc) · 5.48 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
#ifndef PASCAL_EXECUTER_H
#define PASCAL_EXECUTER_H
#include <iostream>
#include <string>
#include <cstdio>
#include <ctype.h>
#include <cstdlib>
#include <vector>
#include <stack>
#include <algorithm>
#include "Lex.h"
using namespace std;
/*==========================================================================
*-----------------------------Класс Executer-------------------------------
*==========================================================================
* Выполнение программы по внутреннему представлению (ПОЛИЗ)
*==========================================================================
*/
class Executer {
public:
void execute ( vector<Lex> & poliz );
};
/*==========================================================================
* Выполняет команды, полученные из вектора лексем
*==========================================================================
*/
void Executer::execute ( vector<Lex> & poliz ) {
Lex pc_el;
stack < int > args;
int i, j, index = 0, size = poliz.size();
while ( index < size ) {
pc_el = poliz [ index ];
switch ( pc_el.get_type () ) {
case LEX_TRUE: case LEX_FALSE: case LEX_NUM: case POLIZ_ADDRESS: case POLIZ_LABEL:
args.push ( pc_el.get_value () );
break;
case LEX_ID:
i = pc_el.get_value ();
if ( TID[i].get_assign () ) {
args.push ( TID[i].get_value () );
break;
}
else
throw "POLIZ: indefinite identifier";
case LEX_NOT:
from_st ( args, i );
args.push( !i );
break;
case LEX_OR:
from_st ( args, i );
from_st ( args, j );
args.push ( j || i );
break;
case LEX_AND:
from_st ( args, i );
from_st ( args, j );
args.push ( j && i );
break;
case POLIZ_GO:
from_st ( args, i );
index = i - 1;
break;
case POLIZ_FGO:
from_st ( args, i );
from_st ( args, j );
if ( !j ) index = i - 1;
break;
case LEX_WRITE:
from_st ( args, j );
cout << j << endl;
break;
case LEX_READ:
int k;
from_st ( args, i );
if ( TID[i].get_type () == LEX_INT ) {
cout << "Input int value for" << TID[i].get_name () << endl;
cin >> k;
}
else {
string j;
while (1) {
cout << "Input boolean value (true or false) for" << TID[i].get_name() << endl;
cin >> j;
if ( j != "true" && j != "false" ) {
cout << "Error in input:true/false" << endl;
continue;
}
k = ( j == "true" ) ? 1 : 0;
break;
}
}
TID[i].put_value (k);
TID[i].put_assign ();
break;
case LEX_PLUS:
from_st ( args, i );
from_st ( args, j );
args.push ( i + j );
break;
case LEX_TIMES:
from_st ( args, i );
from_st ( args, j );
args.push ( i * j );
break;
case LEX_MINUS:
from_st ( args, i );
from_st ( args, j );
args.push ( j - i );
break;
case LEX_SLASH:
from_st ( args, i );
from_st ( args, j );
if (!i) {
args.push ( j / i );
break;
}
else
throw "POLIZ:divide by zero";
case LEX_EQ:
from_st ( args, i );
from_st ( args, j );
args.push ( i == j );
break;
case LEX_LSS:
from_st ( args, i );
from_st ( args, j );
args.push ( j < i );
break;
case LEX_GTR:
from_st ( args, i );
from_st ( args, j );
args.push ( j > i );
break;
case LEX_LEQ:
from_st ( args, i );
from_st ( args, j );
args.push ( j <= i );
break;
case LEX_GEQ:
from_st ( args, i );
from_st ( args, j );
args.push ( j >= i );
break;
case LEX_NEQ:
from_st ( args, i );
from_st ( args, j );
args.push ( j != i );
break;
case LEX_ASSIGN:
from_st ( args, i );
from_st ( args, j );
TID[j].put_value (i);
TID[j].put_assign ();
break;
default:
throw "POLIZ: unexpected elem";
}//end of switch
++index;
};//end of while
cout << "Finish of executing!!!" << endl;
}
#endif //PASCAL_EXECUTER_H