-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecgCondition.cpp
90 lines (89 loc) · 3.04 KB
/
recgCondition.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
#include "syntaxAnalys.h"
#include <iostream>
#include "getSym.h"
#include "error.h"
#include "symbolTable.h"
#include "midCode.h"
using namespace std;
int recgCondition(int level, char * Label, bool satisfy) {
int curState = 0;
streampos sp_tmp = sourceFile.tellg();
int n_tmp = curSym.len;
int type_tmp = curSym.getType();
try {
struct tableNode * left = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(left->id.name, TMP_VAR);
if(recgExpression(level, left)==-E_NOT_THIS) throw E_NOT_THIS;
if (left->type != INT_SYM) reportError(E_UN_MATCH);
switch (curSym.getType())
{
case EQUAL_SYM : {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
MidCode.add(satisfy?BEQ :BNE, Label, left->id.name, right->id.name);
break;
}
case NOTEQ_SYM: {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
MidCode.add(satisfy?BNE:BEQ, Label, left->id.name, right->id.name);
break;
}
case GREAT_SYM: {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
satisfy? MidCode.add(BGT, Label, left->id.name, right->id.name) : MidCode.add(BGE, Label, right->id.name, left->id.name);
break;
}
case GREATEQ_SYM: {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
satisfy ? MidCode.add(BGE, Label, left->id.name, right->id.name) : MidCode.add(BGT, Label, right->id.name, left->id.name);
break;
}
case LESS_SYM: {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
satisfy ? MidCode.add(BGT, Label, right->id.name, left->id.name) : MidCode.add(BGE, Label, left->id.name, right->id.name);
break;
}
case LESSEQ_SYM: {
getSym();
struct tableNode * right = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(right->id.name, TMP_VAR);
recgExpression(level, right);
if (right->type != INT_SYM) reportError(E_UN_MATCH);
satisfy ? MidCode.add(BGE, Label, right->id.name, left->id.name) : MidCode.add(BGT, Label, left->id.name, right->id.name);
break;
}
default: {
MidCode.add(satisfy ? BNE : BEQ, Label, left->id.name, ZERO);
break;
}
}
cout << "At line " << lineNum << " This is a condition statement!" << endl;
return 0;
}
catch (int) {
seekSp(sp_tmp);
backSym(n_tmp, type_tmp);
curChar = sourceFile.get();
getSym();
return -E_NOT_THIS;
}
}