-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecgReturn.cpp
70 lines (68 loc) · 1.6 KB
/
recgReturn.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
#include "syntaxAnalys.h"
#include <iostream>
#include "getSym.h"
#include "error.h"
#include "symbolTable.h"
#include "midCode.h"
using namespace std;
bool hasRetrun = false;
int recgReturn(int level) {
int curState = 0;
streampos sp_tmp = sourceFile.tellg();
int n_tmp = curSym.len;
int type_tmp = curSym.getType();
try {
if (curSym.getType() != RETURN_SYM) throw E_NOT_THIS;
getSym();
struct tableNode node;
node.id.lev = 0;
strcpy(node.id.name, MidCode.func_name);
int i = Table.find(node.id, &node);
if (node.type == VOID_SYM) {
if(curSym.getType()!=SEMICOL_SYM) {
reportError(E_RET);
char until[2] = { ';','\n' };
skip(until, 2);
return 0;
}
else {
MidCode.add(RET_V);
cout << "At line " << lineNum << " This is a return statement!" << endl;
hasRetrun = true;
return 0;
}
}
else {
if (curSym.getType() != LPAREN_SYM) {
MISS_TOKEN;
return 0;
}
getSym();
struct tableNode * result = (struct tableNode *)malloc(sizeof(struct tableNode));
strcpy(result->id.name, TMP_VAR);
recgExpression(level, result);
if (curSym.getType() != RPAREN_SYM) {
MISS_TOKEN;
return 0;
}
if (node.type != result->type) {
reportError(E_RET);
curState = E_RET;
}
if (curState == 0) {
MidCode.add(node.type == INT_SYM ? RET_I : RET_C, result->id.name);
}
getSym();
cout << "At line " << lineNum << " This is a return statement!" << endl;
hasRetrun = true;
return 0;
}
}
catch (int) {
seekSp(sp_tmp);
backSym(n_tmp, type_tmp);
curChar = sourceFile.get();
getSym();
return -E_NOT_THIS;
}
}