-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.c
187 lines (140 loc) · 2.45 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
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
#include <string.h>
#include "getSource.h"
#define MAXTABLE 100
#define MAXLEVEL 5
typedef struct tableE {
KindT kind;
char name[MAXNAME];
union {
int value;
struct {
RelAddr raddr;
int pars;
}f;
RelAddr raddr;
}u;
} TabelE;
static TabelE nameTable[MAXTABLE];
static int tIndex = 0;
static int level = -1;
static int indexL[MAXLEVEL];
static int addr[MAXLEVEL];
static int localAddr;
static int tfIndex;
void blockBegin(int firstAddr)
{
if(level == -1){
localAddr = firstAddr;
tIndex = 0;
level++;
return;
}
if(level == MAXLEVEL - 1) errorF("too many nested blocks");
indexL[level] = tIndex;
addr[level] = localAddr;
localAddr = firstAddr;
level++;
return;
}
void blockEnd()
{
level --;
tIndex = indexL[level];
localAddr = addr[level];
}
int bLevel()
{
return level;
}
int fPars()
{
return nameTable[indexL[level-1]].u.f.pars;
}
void enterT(char *id)
{
if(++tIndex < MAXTABLE){
strcpy(nameTable[tIndex].name, id);
}else{
errorF("too many names");
}
}
int enterTfunc(char *id, int v)
{
enterT(id);
nameTable[tIndex].kind = funcId;
nameTable[tIndex].u.f.raddr.level = level;
nameTable[tIndex].u.f.raddr.addr = v;
nameTable[tIndex].u.f.pars = 0;
tfIndex = tIndex;
return tIndex;
}
int enterTpar(char *id)
{
enterT(id);
nameTable[tIndex].kind = parId;
nameTable[tIndex].u.raddr.level = level;
nameTable[tfIndex].u.f.pars++;
return tIndex;
}
int enterTvar(char *id)
{
enterT(id);
nameTable[tIndex].kind = varId;
nameTable[tIndex].u.raddr.level = level;
nameTable[tIndex].u.raddr.addr = localAddr++;
return tIndex;
}
int enterTconst(char *id , int v)
{
enterT(id);
nameTable[tIndex].kind = constId;
nameTable[tIndex].u.value = v;
return tIndex;
}
void endpar()
{
int i;
int pars = nameTable[tfIndex].u.f.pars;
if (pars == 0) return;
for (i=1; i<=pars; i++)
nameTable[tfIndex+i].u.raddr.addr = i-1-pars;
}
void changeV(int ti, int newVal)
{
nameTable[ti].u.f.raddr.addr = newVal;
}
int searchT(char *id, KindT k)
{
int i;
i = tIndex;
strcpy(nameTable[0].name, id);
while( strcmp(id, nameTable[i].name) )
i--;
if ( i )
return i;
else {
errorType("undef");
if (k==varId) return enterTvar(id);
return 0;
}
}
KindT kindT(int i)
{
return nameTable[i].kind;
}
RelAddr relAddr(int ti)
{
return nameTable[ti].u.raddr;
}
int val(int ti)
{
return nameTable[ti].u.value;
}
int pars(int ti)
{
return nameTable[ti].u.f.pars;
}
int frameL()
{
return localAddr;
}