-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolTable.c
158 lines (129 loc) · 3.05 KB
/
symbolTable.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
#include "symbolTable.h"
#include "auxFunctions.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
symbol * searchIdentifier(char * identifier) {
symbol * sp = NULL;
int i;
for (i = 0; i < size_of_table; i++) {
if (strcmp(symbol_table[i].name, identifier) == 0) {
sp = &symbol_table[i];
break;
}
}
return sp;
}
/* Find a symbol or create a new symbol in table */
symbol * searchSymbol(char * word) {
symbol * sp = NULL;
int i;
for (i = 0; i < size_of_table; i++) {
if (strcmp(symbol_table[i].name, word) == 0) {
return &symbol_table[i];
}
}
if (size_of_table < SIZE_TABLE) {
symbol_table[size_of_table].name = strdup(word);
symbol_table[size_of_table].reflist = NULL;
return &symbol_table[size_of_table++];
} else {
fputs("Symbol table overflow\n", stderr);
abort();
}
}
int addNewVariable(type_values * all, char * type, int line, char * filename) {
reference * r;
symbol * sp = searchSymbol(all->value);
int return_validate = 0;
/* Don't do dups of same line and file */
if (sp->reflist) {
return_validate = 0;
} else {
sp->type = convertTypeStringToTypeInt(type);
r = malloc(sizeof(reference));
if (!r) {
fputs("out of space\n", stderr);
abort();
} else {
r->value = NULL;
r->next = sp->reflist;
r->filename = filename;
r->line = line;
sp->reflist = r;
}
return_validate = 1;
}
return return_validate;
}
int addAttribuition(char * variable, type_values * new_value, int line, char * filename) {
reference * r;
symbol * sp = searchIdentifier(variable);
int return_validate = 0;
/* Don't do dups of same line and file */
if (sp->reflist == NULL) {
return_validate = 0;
} else {
r = malloc(sizeof(reference));
if (!r) {
fputs("out of space\n", stderr);
abort();
} else {
r->value = new_value;
r->next = sp->reflist;
r->filename = filename;
r->line = line;
sp->reflist = r;
}
return_validate = 1;
}
return return_validate;
}
void printSymbolTable() {
char * prevfn = NULL;
qsort(symbol_table, SIZE_TABLE, sizeof(symbol), symCompare);
for (int i = 0; i < size_of_table; i++) {
prevfn = NULL;
reference * ref = symbol_table[i].reflist;
/* Now print the word and its references */
printf("%20s:%-20s", symbol_table[i].name, convertTypeIntToTypeString(symbol_table[i].type));
while(ref != NULL) {
if (ref->filename == prevfn) {
printf(", %d", ref->line);
} else {
printf(" %s:%d", ref->filename, ref->line);
prevfn = ref->filename;
}
if (ref->value != NULL) {
printf(":");
printTypeValues1(ref->value);
}
ref = ref->next;
}
printf("\n");
}
}
/* Aux function for sorting */
static int symCompare(const void * xa, const void * xb) {
const symbol * a = xa;
const symbol * b = xb;
if (!a->name) {
if (!b->name) {
/* Both empty */
return 0;
} else {
/* Put the empties at the end */
return 1;
}
} else {
if (!b->name) {
return -1;
} else {
return strcmp(a->name, b->name);
}
}
}
int printDebugText(int debugValue) {
printf("Debug %d \n", debugValue);
return debugValue + 1;
}