-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.c
209 lines (171 loc) · 4.25 KB
/
tables.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tables.h"
// Literals Table
// ----------------------------------------------------------------------------
#define LITERAL_MAX_SIZE 128
#define LITERALS_TABLE_MAX_SIZE 100
struct lit_table {
char t[LITERALS_TABLE_MAX_SIZE][LITERAL_MAX_SIZE];
int size;
};
LitTable* create_lit_table() {
LitTable *lt = malloc(sizeof * lt);
lt->size = 0;
return lt;
}
int lookup_lit(LitTable* lt, char* s) {
int i;
for (i = 0; i < lt->size; i++) {
if (strcpy(lt->t[lt->size], s) == 0) {
return i;
}
}
return -1;
}
int add_literal(LitTable* lt, char* s) {
int i;
for (i = 0; i < lt->size; i++) {
if (strcmp(lt->t[i], s) == 0) {
return i;
}
}
strcpy(lt->t[lt->size], s);
int idx_added = lt->size;
lt->size++;
return idx_added;
}
char* get_literal(LitTable* lt, int i) {
return lt->t[i];
}
void print_lit_table(LitTable* lt) {
printf("Literals table:\n");
int i;
for (i = 0; i < lt->size; i++) {
printf("Entry %d -- %s\n", i, get_literal(lt, i));
}
}
void free_lit_table(LitTable* lt) {
free(lt);
}
// Symbols Table
// ----------------------------------------------------------------------------
#define SYMBOL_MAX_SIZE 128
#define SYMBOL_TABLE_MAX_SIZE 100
typedef struct {
char name[SYMBOL_MAX_SIZE];
int line;
int escopo;
int tamanho;
int offset;
} Entry;
struct sym_table {
Entry t[SYMBOL_TABLE_MAX_SIZE];
int size;
};
SymTable* create_sym_table() {
SymTable *st = malloc(sizeof * st);
st->size = 0;
return st;
}
int lookup_var(SymTable* st, char* s,int scope) {
int i;
for (i = 0; i < st->size; i++) {
if (strcmp(st->t[i].name, s) == 0 && st->t[i].escopo == scope) {
return i;
}
}
return -1;
}
int add_var(SymTable* st, char* s, int line, int escopo, int tamanho) {
strcpy(st->t[st->size].name, s);
st->t[st->size].line = line;
st->t[st->size].escopo = escopo;
st->t[st->size].tamanho = tamanho;
int idx_added = st->size;
st->size++;
return idx_added;
}
void add_offset(SymTable* st, int offset, int i){
st->t[i].offset = offset;
}
char* get_name(SymTable* st, int i) {
return st->t[i].name;
}
int get_line(SymTable* st, int i) {
return st->t[i].line;
}
int get_escopo(SymTable* st, int i) {
return st->t[i].escopo;
}
int get_tamanho(SymTable* st, int i) {
return st->t[i].tamanho;
}
int get_offset(SymTable* st, int i){
return st->t[i].offset;
}
void print_sym_table(SymTable* st) {
printf("Variables table:\n");
int i;
for (i = 0; i < st->size; i++) {
printf("Entry %d -- name: %s, line: %d, scope: %d, size: %d\n", i, get_name(st, i), get_line(st, i), get_escopo(st, i), get_tamanho(st, i));
}
}
void free_sym_table(SymTable* st) {
free(st);
}
// Function Table
// ----------------------------------------------------------------------------
#define FUNCTION_MAX_SIZE 128
#define FUNCTION_TABLE_MAX_SIZE 100
typedef struct {
char name[FUNCTION_MAX_SIZE];
int line;
int aridade;
} Func;
struct fun_table {
Func t[FUNCTION_TABLE_MAX_SIZE];
int size;
};
FunTable* create_fun_table() {
FunTable *fun = malloc(sizeof * fun);
fun->size = 0;
return fun;
}
int lookup_fun(FunTable* fun, char* s) {
int i;
for (i = 0; i < fun->size; i++) {
if (strcmp(fun->t[i].name, s) == 0) {
return i;
}
}
return -1;
}
int add_fun(FunTable* fun, char* s, int line, int aridade) {
strcpy(fun->t[fun->size].name, s);
fun->t[fun->size].line = line;
fun->t[fun->size].aridade = aridade;
int idx_added = fun->size;
fun->size++;
return idx_added;
}
char* get_name_fun(FunTable* fun, int i) {
return fun->t[i].name;
}
int get_line_fun(FunTable* fun, int i) {
return fun->t[i].line;
}
int get_aridade_fun(FunTable* fun, int i) {
return fun->t[i].aridade;
}
void print_fun_table(FunTable* fun) {
printf("Functions table:\n");
int i;
for (i = 0; i < fun->size; i++) {
printf("Entry %d -- name: %s, line: %d, arity: %d\n", i, get_name_fun(fun, i), get_line_fun(fun, i), get_aridade_fun(fun, i));
}
}
void free_fun_table(FunTable* fun) {
free(fun);
}