-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
148 lines (127 loc) · 2.64 KB
/
table.c
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
#ifndef TBL
#define TBL
#include "table.h"
#endif
#include <string.h>
#include "getSource.h"
#define MAXLEVEL 5
#define MAXNAME 31
#define MAXTABLE 100
typedef struct tableE {
KindT kind;
char name[MAXNAME];
union {
int value;
struct {
RelativeAddr relativeAddr;
int paramCount;
} f;
RelativeAddr relativeAddr;
} u;
} TableE;
static TableE nameTable[MAXTABLE];
static int level = -1;
static int localAddr;
static int tableIndex;
static int lastIndex[MAXLEVEL];
static int addr[MAXLEVEL];
static int tableFunctionIndex;
void blockBegin(int firstAddr)
{
if (level == -1) {
localAddr = firstAddr;
tableIndex = 0;
level++;
return;
}
lastIndex[level] = tableIndex;
addr[level] = localAddr;
localAddr = firstAddr;
level++;
return;
}
void blockEnd()
{
level--;
tableIndex = lastIndex[level];
localAddr = addr[level];
}
int currentBlockLevel()
{
return(level);
}
int functionParamCount()
{
return(nameTable[lastIndex[level-1]].u.f.paramCount);
}
void enterT(char *id)
{
if (tableIndex++ < MAXTABLE) strcpy(nameTable[tableIndex].name, id);
}
int enterTfunc(char *id, int v)
{
enterT(id);
nameTable[tableIndex].kind = funcId;
nameTable[tableIndex].u.f.relativeAddr.level = level;
nameTable[tableIndex].u.f.relativeAddr.addr = v;
nameTable[tableIndex].u.f.paramCount = 0;
tableFunctionIndex = tableIndex;
return tableIndex;
}
int enterTpar(char *id)
{
enterT(id);
nameTable[tableIndex].kind = parId;
nameTable[tableIndex].u.relativeAddr.level = level;
nameTable[tableFunctionIndex].u.f.paramCount++;
return tableIndex;
}
int enterTvar(char *id)
{
enterT(id);
nameTable[tableIndex].kind = varId;
nameTable[tableIndex].u.relativeAddr.level = level;
nameTable[tableIndex].u.relativeAddr.addr = localAddr++;
return(tableIndex);
}
int enterTconst(char *id, int v)
{
enterT(id);
nameTable[tableIndex].kind = constId;
nameTable[tableIndex].u.value = v;
return(tableIndex);
}
void endFunctionParam()
{
int i;
int paramCount = nameTable[tableFunctionIndex].u.f.paramCount;
if (paramCount == 0) return;
for (i=1; i<=paramCount; i++) nameTable[tableFunctionIndex+i].u.relativeAddr.addr = i-1-paramCount;
}
void changeV(int tableIndex, int newVal)
{
nameTable[tableIndex].u.f.relativeAddr.addr = newVal;
}
int searchT(char *id)
{
int i;
i = tableIndex;
while( strcmp(id, nameTable[i].name) ) i--;
return(i);
}
KindT kindT(int i)
{
return(nameTable[i].kind);
}
RelativeAddr relAddr(int tableIndex)
{
return(nameTable[tableIndex].u.relativeAddr);
}
int val(int tableIndex)
{
return(nameTable[tableIndex].u.value);
}
int LocalAddr()
{
return(localAddr);
}