-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecgTerm.cpp
60 lines (57 loc) · 1.73 KB
/
recgTerm.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
#include "syntaxAnalys.h"
#include <iostream>
#include "getSym.h"
#include "error.h"
#include "symbolTable.h"
#include "midCode.h"
using namespace std;
int recgTerm(int level, struct tableNode * tmpnode) {
int curState = 0;
streampos sp_tmp = sourceFile.tellg();
int n_tmp = curSym.len;
int type_tmp = curSym.getType();
try {
struct tableNode * tmpnode1 = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(tmpnode1->id.name , TMP_VAR);
if (recgFactor(level, tmpnode1) == -E_NOT_THIS) throw(E_NOT_THIS);
MidCode.add(ADD, tmpnode->id.name, tmpnode1->id.name, ZERO);
tmpnode->type = tmpnode1->type;
tmpnode->kind = tmpnode1->kind;
tmpnode->val = tmpnode1->val;
streampos sp = sourceFile.tellg();
int n = curSym.len;
int type = curSym.getType();
bool found = false;
while (true) {
bool isStar = true;
if (curSym.getType() != STAR_SYM && curSym.getType() != SLASH_SYM)
break;
tmpnode->val = EXCEPTVALUE;
if (curSym.getType() != STAR_SYM) isStar = false;
found = true;
getSym();
struct tableNode * tmpnode2 = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(tmpnode2->id.name, TMP_VAR);
recgFactor(level, tmpnode2);
if(isStar) MidCode.add(MULT, tmpnode->id.name, tmpnode->id.name, tmpnode2->id.name);
else MidCode.add(DIV, tmpnode->id.name, tmpnode->id.name, tmpnode2->id.name);
sp = sourceFile.tellg();
n = curSym.len;
type = curSym.getType();
}
if (found) tmpnode->type = INT_SYM;
seekSp(sp);
backSym(n, type);
curChar = sourceFile.get();
cout << "At line " << lineNum << " This is a term!" << endl;
getSym();
return 0;
}
catch (int) {
seekSp(sp_tmp);
backSym(n_tmp, type_tmp);
curChar = sourceFile.get();
getSym();
return -E_NOT_THIS;
}
}