-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.cpp
495 lines (435 loc) · 12.9 KB
/
statement.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "statement.h"
#include "parser.h"
#include "mainwindow.h"
statement::statement(evalstate* st)
{
state = st;
}
bool statement::isLegalVarName(QString name)
{
if(name[0].isDigit())
return false;
QRegExp reg("^[a-zA-Z_][a-zA-Z0-9_]*$");
int index;
if((index=reg.indexIn(name))==0)
return true;
else return false;
}
stmtType statement::getType()
{
return type;
}
//-------------------------------------------REM-----------------------------------------------
remStmt::remStmt(QList<QString> tokens, evalstate* st):statement (st)
{
for (int i=1;i<tokens.size();++i) {
text += tokens[i];
}
type = REM;
}
int remStmt::excute()
{
return 0;
}
QString remStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("REM\n")+QString(" ")+text+QString("\n");
return tmpString;
}
bool remStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]=="REM")
return true;
return false;
}
//-------------------------------------------REM--------------------------------------------end
//-------------------------------------------LET-----------------------------------------------
letStmt::letStmt(QList<QString> tokens, evalstate* st):statement (st)
{
// if(tokens.length()!=4) {
// QString error("Illegal LET Statement! The Length Of Tokens Is Not 4.");
// throw error;
// }
// if(tokens[2]!="=") {
// QString error("Illegal LET Statement! Missing \"=\".");
// throw error;
// }
varible = tokens[1].remove(QRegExp("\\s")); //用正则表达式移除变量名内所有空格;
parser myParser(state);
if((tokens[3].front()=='\"'&&tokens[3].back()=='\"')||(tokens[3].front()=='\''&&tokens[3].back()=='\'')) { //如果是被单或双引号括起来的
sValue = tokens[3].mid(1, tokens[3].length()-2);
exp = nullptr;
}
else
exp = myParser.parseExp(tokens[3]);
type = LET;
}
letStmt::~letStmt()
{
if(exp)
delete exp;
}
int letStmt::excute()
{
if(exp)
state->setValue(varible, exp->eval(*state));
else state->setValue(varible, sValue);
return 0;
}
QString letStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("LET =\n")+QString(" ")+varible+QString("\n");
if(exp)
tmpString.append(exp->toSyntaxTreeString(2));
else tmpString+=" "+sValue;
return tmpString;
}
bool letStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="LET")
return false;
if(tokens.length()!=4) {
return false;
}
if(tokens[2]!="=") {
return false;
}
if(!isLegalVarName(tokens[1].remove(QRegExp("\\s")))) //判断是否为合法的变量名
return false;
if((tokens[3].front()=='\"'&&tokens[3].back()=='\"')||(tokens[3].front()=='\''&&tokens[3].back()=='\'')) { //如果是被单或双引号括起来的
tokens[3]=tokens[3].mid(1, tokens[3].length()-2); //去掉头尾引号
if(tokens[3].contains('\"')||tokens[3].contains('\'')) //如果里面包含双或单引号,不合法
return false;
}
return true;
}
//-------------------------------------------LET--------------------------------------------end
//-------------------------------------------PRINT-----------------------------------------------
printStmt::printStmt(QList<QString> tokens, evalstate *st):statement (st)
{
// if(tokens.length()!=2) {
// QString error("Illegal PRINT Statement! The Length Of Tokens Is Not 2.");
// throw error;
// }
parser myParser(state);
exp = myParser.parseExp(tokens[1]);
type = PRINT;
}
printStmt::~printStmt()
{
if(exp)
delete exp;
}
int printStmt::getValue()
{
return exp->eval(*state);
}
int printStmt::excute()
{
return -1;
}
QString printStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("PRINT\n");
tmpString.append(exp->toSyntaxTreeString(2));
return tmpString;
}
bool printStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="PRINT")
return false;
if(tokens.length()!=2) {
return false;
}
return true;
}
//-------------------------------------------PRINT--------------------------------------------end
//-------------------------------------------INPUT-----------------------------------------------
inputStmt::inputStmt(QList<QString> tokens, evalstate *st):statement (st)
{
// if(tokens.length()!=2) {
// QString error("Illegal GOTO Statement! The Length Of Tokens Is Not 2.");
// throw error;
// }
varible=tokens[1].remove(QRegExp("\\s")); //用正则表达式移除变量名内所有空格
type = INPUT;
}
int inputStmt::excute()
{
return -2;
}
QString inputStmt::getVarName()
{
return varible;
}
QString inputStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("INPUT\n")+QString(" ")+varible+QString("\n");
return tmpString;
}
bool inputStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="INPUT")
return false;
if(tokens.length()!=2) {
return false;
}
if(!isLegalVarName(tokens[1].remove(QRegExp("\\s")))) //判断是否为合法的变量名
return false;
return true;
}
//-------------------------------------------INPUT--------------------------------------------end
//-------------------------------------------GOTO-----------------------------------------------
gotoStmt::gotoStmt(QList<QString> tokens, evalstate *st):statement (st)
{
// if(tokens.length()!=2) {
// QString error("Illegal GOTO Statement! The Length Of Tokens Is Not 2.");
// throw error;
// }
tokens[1].remove(QRegExp("\\s")); //用正则表达式移除line number内所有空格
bool ok;
nextLineNum = tokens[1].toInt(&ok);
// if(!ok) {
// QString error("Illegal GOTO Statement! The Second Token Should Be A Number.");
// throw error;
// }
type = GOTO;
}
int gotoStmt::excute()
{
return nextLineNum;
}
QString gotoStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("INPUT\n")+QString(" ")+QString::number(nextLineNum)+QString("\n");
return tmpString;
}
bool gotoStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="GOTO")
return false;
if(tokens.length()!=2) {
return false;
}
tokens[1].remove(QRegExp("\\s")); //用正则表达式移除line number内所有空格
bool ok;
tokens[1].toInt(&ok);
if(!ok) {
return false;
}
return true;
}
//-------------------------------------------GOTO--------------------------------------------end
//-------------------------------------------IF THEN-----------------------------------------------
ifStmt::ifStmt(QList<QString> tokens, evalstate *st):statement (st)
{
if(tokens.length()!=6) {
QString error("Illegal IF Statement! The Length Of Tokens Is Not 6.\nThe Form Should Be \"IF exp1 op exp2 THEN n\"");
throw error;
}
bool ok;
tokens[5].remove(QRegExp("\\s")); //用正则表达式移除number内所有空格
thenNum = tokens[5].toInt(&ok);
if(!isOp(tokens[2])||tokens[4]!="THEN"||!ok) {
QString error("Illegal IF Statement!\nThe Form Should Be \"IF exp1 op exp2 THEN n\"");
throw error;
}
parser myParser(st);
exp1 = myParser.parseExp(tokens[1]);
exp2 = myParser.parseExp(tokens[3]);
op=tokens[2];
type = IF;
}
ifStmt::~ifStmt()
{
if(exp1)
delete exp1;
if(exp2)
delete exp2;
}
int ifStmt::excute()
{
if(condition())
return thenNum;
else return 0;
}
bool ifStmt::isOp(QString op)
{
if(op==">"||op=="<"||op=="=")
return true;
return false;
}
bool ifStmt::condition()
{
int value1=exp1->eval(*state);
int value2=exp2->eval(*state);
if(op==">") {
return value1>value2;
}
else if (op=="=") {
return value1==value2;
}
else if (op=="<") {
return value1<value2;
}
else return false;
}
QString ifStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("IF THEN\n");
tmpString.append(exp1->toSyntaxTreeString(2));
tmpString+=(QString(" ")+op+QString("\n"));
tmpString.append(exp2->toSyntaxTreeString(2));
tmpString+=(QString(" ")+QString::number(thenNum)+QString("\n"));
return tmpString;
}
bool ifStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="IF")
return false;
if(tokens.length()!=6) {
return false;
}
bool ok;
tokens[5].remove(QRegExp("\\s")); //用正则表达式移除number内所有空格
tokens[5].toInt(&ok);
if(!isOp(tokens[2])||tokens[4]!="THEN"||!ok) {
return false;
}
return true;
}
//-------------------------------------------IF THEN--------------------------------------------end
//-------------------------------------------END-----------------------------------------------
endStmt::endStmt(QList<QString> tokens, evalstate *st):statement (st)
{
type = END;
}
int endStmt::excute()
{
return -3;
}
QString endStmt::toSyntaxTree()
{
return QString("END\n");
}
bool endStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="END")
return false;
return true;
}
//-------------------------------------------END--------------------------------------------end
//-------------------------------------------INPUTS--------------------------------------------
inputsStmt::inputsStmt(QList<QString> tokens, evalstate *st):statement (st)
{
// if(tokens.length()!=2) {
// QString error("Illegal GOTO Statement! The Length Of Tokens Is Not 2.");
// throw error;
// }
varible=tokens[1].remove(QRegExp("\\s")); //用正则表达式移除变量名内所有空格
type = INPUTS;
}
int inputsStmt::excute()
{
return -2;
}
QString inputsStmt::getVarName()
{
return varible;
}
QString inputsStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("INPUTS\n")+QString(" ")+varible+QString("\n");
return tmpString;
}
bool inputsStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="INPUTS")
return false;
if(tokens.length()!=2) {
return false;
}
if(!isLegalVarName(tokens[1].remove(QRegExp("\\s")))) //判断是否为合法的变量名
return false;
return true;
}
//-------------------------------------------INPUTS-----------------------------------------end
//-------------------------------------------PRINTF--------------------------------------------
printfStmt::printfStmt(QList<QString> tokens, evalstate *st):statement (st)
{
tokenizer myTokenizer;
QString tmpString;
for (int i=1;i<tokens.size();++i) {
tmpString+=tokens[i];
}
bool flag=false;
argv = myTokenizer.toStringTokens(tmpString, flag);
type = PRINTF;
}
QString printfStmt::getText()
{
QString text = argv[0], tmpS;
for (int i=0, j=1, k=0;i<argv[0].size();++i, ++k) {
if(argv[0].mid(i, 2)=="{}") {
if((argv[j].front()=='\"'&&argv[j].back()=='\"')||(argv[j].front()=='\''&&argv[j].back()=='\'')) {
tmpS = argv[j].mid(1, argv[j].size()-2);
}
else {
bool ok;
int num = argv[j].toInt(&ok);
if(ok)
tmpS = QString::number(num);
else {
if(state->isDefinedInt(argv[j]))
tmpS = QString::number(state->getValue(argv[j]));
else tmpS = state->getString(argv[j]);
}
}
text.replace(k, 2, tmpS);
k+=tmpS.size()-1; //for循环里还要++k,所以这里少加一个
++j;
++i; //多加一个i,跳过右大括号
}
}
text = text.mid(1, text.size()-2);
return text;
}
int printfStmt::excute()
{
return -1;
}
QString printfStmt::toSyntaxTree()
{
QString tmpString;
tmpString=QString("PRINTF\n");
for (int i=0;i<argv.size();++i) {
tmpString.append(" "+argv[i]+"\n");
}
return tmpString;
}
bool printfStmt::isLegal(QList<QString> tokens)
{
if(tokens[0]!="PRINTF")
return false;
QString tmpString;
for (int i=1;i<tokens.size();++i) {
tmpString+=tokens[i];
}
bool flag=false;
tokenizer myTokenizer;
QList<QString> lists;
lists = myTokenizer.toStringTokens(tmpString, flag);
if(!flag)
return false;
if(lists[0].count("{}")>lists.size()) //"{}"的数量大于后续参数的数量?
return false;
return true;
}
//-------------------------------------------PRINTF-----------------------------------------end