-
Notifications
You must be signed in to change notification settings - Fork 0
/
symrec.c
108 lines (86 loc) · 1.98 KB
/
symrec.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symrec.h"
typedef struct {
int base;
symrec * syms;
} Cabec;
/* symrec *SymStack[SYM_STACK_SIZE] = {(symrec* )0,}; */
Cabec SymStack[SYM_STACK_SIZE];
int topss = 0; /* top = first free */
/* Inclui um símbolo na tabela corrente */
symrec *putsym (char *sym_name)
{
symrec *ptr;
symrec *table = SymStack[topss].syms;
ptr = (symrec *) malloc (sizeof (symrec));
ptr->name = (char *) malloc (strlen (sym_name) + 1);
strcpy (ptr->name,sym_name);
ptr->val = SymStack[topss].base++;
ptr->next = table;
SymStack[topss].syms = ptr;
return ptr;
}
/* Busca na tabela indicada */
static symrec *getsym_i(int t, char *sym_name) {
symrec *ptr;
symrec *table = SymStack[t].syms;
for (ptr = table; ptr != (symrec *) 0;
ptr = (symrec *)ptr->next)
if (strcmp (ptr->name,sym_name) == 0)
return ptr;
return 0;
}
/* Busca na tabela corrente */
symrec *getsymcur(char *sym_name) {
return getsym_i(topss, sym_name);
}
/* Busca em todas as tabelas */
symrec *getsym(char *sym_name)
{
symrec *ptr;
for (int t = topss; t >= 0; t--)
if ((ptr = getsym_i(t, sym_name)) != NULL)
return ptr;
return 0;
}
int isglobal() { return !topss;}
symrec *newtab(int b) {
if (topss < SYM_STACK_SIZE-1) {
SymStack[++topss] = (Cabec) {b,0};
return SymStack[topss].syms;
}
else
fprintf(stderr,
"Programa muito complexo:"
"número de contextos encaixados muito grande\n");
return NULL;
}
static void delsymtab(Cabec c) {
symrec *ptr, *prox;
symrec *s = c.syms;
if (!s)
return; /* já está vazia */
prox = s->next;
for (ptr = s; ptr != NULL; ptr = prox) {
prox = ptr->next;
free(ptr->name);
free(ptr);
}
}
symrec *deltab() {
if (topss ==0) return SymStack[0].syms;
delsymtab(SymStack[topss]);
return SymStack[--topss].syms;
}
void cleartab() {
while (topss) {
deltab();
}
delsymtab(SymStack[0]);
SymStack[0].syms = NULL;
}
int lastval() {
return SymStack[topss].base;
}