-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.cpp
273 lines (239 loc) · 7.87 KB
/
runner.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
#include "runner.h"
runner::runner(QObject *parent) : QThread(parent)
{
line_numbers.clear();
line_numbers.resize(0);
split_expreesion_store.clear();
expressions_store.clear();
}
void runner::parse_line(int line_num){
Statement cur_state = store_statement_type[line_num];
std::vector<std::string> line_split = split_expreesion_store[line_num];
std::vector<std::vector<std::string>> expr_tokens = expressions_store[line_num];
class exp* node = exp_->parse_line(line_split, expr_tokens, cur_state);
store_tree[line_num] = node;
}
int runner::string_convert(std::string& input_line){
//判断语句是否有行号
bool have_line_num = tokenizer->is_have_line_num(input_line);
std::string input_without_line_num;
if(have_line_num){ //有行号的输入
int line_number = tokenizer->split_line_num(input_line);
//如果行号之前没出现过,插入行号存储vector
store_line_number(line_number);
//split input_line without line_number
int start_id = input_line.find_first_of(' ', 0) + 1;
int length = input_line.size() - start_id;
input_without_line_num = input_line.substr(start_id, length);
code_display_content[line_number] = input_without_line_num; //存储codedisplay要显示的内容
Statement state_op = tokenizer->state_confirm(input_without_line_num);
store_statement_type[line_number] = state_op;
//split input into tokens
split_expreesion_store[line_number] = tokenizer->split_expression(input_without_line_num);
store_expreesions(line_number);
return line_number;
}else{ //无行号的输入(let/print/input类型 or RUN/LOAD/LIST/CLEAR/HELP/QUIT or 不合法类型)
//TODO 判断是否是合法的无行号输入
Statement state_op = tokenizer->state_confirm(input_line);
if(state_op >= 8){ //RUN/LOAD/LIST/CLAER/HELP/QUIT
is_cmd = true;
return state_op;
}
return INT_MIN;
}
}
void runner::store_line_number(int line_num){
if(line_numbers.size() == 0){
line_numbers.push_back(line_num);
return;
}
int left = 0, right = line_numbers.size();
while(left < right){
int mid = (right - left) / 2 + left;
if(line_numbers[mid] < line_num){
left = mid + 1;
}else if(line_numbers[mid] > line_num){
right = mid;
}else if(line_numbers[mid] == line_num){
return;
}
}
line_numbers.insert(line_numbers.begin() + left, line_num);
}
QString runner::print_line(int line_num){
std::string line_content = "";
line_content += std::to_string(line_num);
line_content += " ";
line_content += code_display_content[line_num];
return QString::fromStdString(line_content);
}
void runner::store_expreesions(int line_num){
Statement state_op = store_statement_type[line_num];
switch (state_op) {
case Let:{
std::string expr = split_expreesion_store[line_num][2];
std::vector<std::string> tokens = tokenizer->split_expr_to_tokens(expr);
expressions_store[line_num].push_back(tokens);
break;
}
case Print:{
std::string expr = split_expreesion_store[line_num][1];
std::vector<std::string> tokens = tokenizer->split_expr_to_tokens(expr);
expressions_store[line_num].push_back(tokens);
break;
}
case If:{
std::string expr1 = split_expreesion_store[line_num][3], expr2 = split_expreesion_store[line_num][4];
std::vector<std::string> tokens1 = tokenizer->split_expr_to_tokens(expr1), tokens2 = tokenizer->split_expr_to_tokens(expr2);
expressions_store[line_num].push_back(tokens1);
expressions_store[line_num].push_back(tokens2);
break;
}
}
}
Statement runner::get_state_op(int &line_index){
return store_statement_type[line_numbers[line_index]];
}
void runner::run(){
int line_index = 0;
run_program(line_index);
}
void runner::run_program(int &line_index){
output.clear();
int n = line_numbers.size();
while(line_index < n){
pauseLock.lock();
if(is_input){
input_cond.wait(&pauseLock);
}
pauseLock.unlock();
Statement state_op = get_state_op(line_index);
switch (state_op) {
case Rem:{
line_index++;
continue;
break;
}
case Let:{
run_let(line_index);
break;
}
case Print:{
run_print(line_index);
//ui->textBrowser->append(QString::fromStdString(res));
break;
}
case Input:{
//TODO
is_input = true;
run_input(line_index);
line_index++;
break;
}
case Goto:{
run_goto(line_index);
break;
}
case If:{
run_if(line_index);
break;
}
case End:{
line_index = n;
return;
}
}
}
}
void runner::run_let(int &line_index){
int cur_line_num = line_numbers[line_index];
//TODO calculate
int res = exp_->eval_exp(store_tree[cur_line_num]->get_child_x(1));
std::string variable = split_expreesion_store[cur_line_num][1];
exp_->store_var(variable, res);
line_index++;
}
void runner::run_print(int &line_index){
int cur_line_num = line_numbers[line_index];
class exp* cur = store_tree[cur_line_num]->get_child_x(0);
int res = exp_->eval_exp(cur);
line_index++;
output.push_back(std::to_string(res));
emit toPrint();
}
void runner::run_goto(int &line_index){
int cur_line_num = line_numbers[line_index];
int line_togo = stoi(split_expreesion_store[cur_line_num][1]);
int line_togo_index = find_line_num_index(line_togo);
line_index = line_togo_index;
}
void runner::run_if(int &line_index){
int cur_line_num = line_numbers[line_index];
std::vector<std::string> expr1 = expressions_store[cur_line_num][0];
std::vector<std::string> expr2 = expressions_store[cur_line_num][1];
int res1 = exp_->eval_exp(store_tree[cur_line_num]->get_child_x(0));
int res2 = exp_->eval_exp(store_tree[cur_line_num]->get_child_x(2));
Cmp cmp = string_to_cmp(split_expreesion_store[cur_line_num][2]);
//verify the condition
if(is_true(res1, res2, cmp)){
int line_togo = stoi(split_expreesion_store[cur_line_num][1]);
int line_togo_index = find_line_num_index(line_togo);
line_index = line_togo_index;
}else{
line_index++;
}
}
void runner::run_input(int &line_index){
int cur_line_num = line_numbers[line_index];
std::string var = split_expreesion_store[cur_line_num][1];
//TODO print data update
output.push_back(" ? ");
input_var = var;
}
Cmp runner::string_to_cmp(std::string &str){
if(str == "="){
return Equal;
}else if(str == ">"){
return Greater;
}else if(str == "<"){
return Less;
}
}
bool runner::is_true(int num1, int num2, Cmp op){
switch (op) {
case Equal:{
return num1 == num2;
}
case Greater:{
return num1 > num2;
}
case Less:{
return num1 < num2;
}
}
}
int runner::find_line_num_index(int &line_num){
int left = 0, right = line_numbers.size();
while(left < right){
int mid = (right - left) / 2 + left;
if(line_num == line_numbers[mid]){
return mid;
}else if(line_numbers[mid] > line_num){
right = mid;
}else if(line_numbers[mid] < line_num){
left = mid + 1;
}
}
}
void runner::clear_all(){
is_input = false;
output.clear();
store_tree.clear();
line_numbers.clear();
split_expreesion_store.clear();
store_statement_type.clear();
expressions_store.clear();
code_display_content.clear();
exp_->variable_hash.clear();
tokenizer->variables.clear();
}