-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table.c
180 lines (167 loc) · 4.6 KB
/
symbol_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
//
// symbol_table.c
// Oberon
//
// Created by Alvaro Costa Neto on 10/17/13.
// Copyright (c) 2013 Alvaro Costa Neto. All rights reserved.
//
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "backend.h"
#include "errors.h"
#include "scanner.h"
#include "symbol_table.h"
entry_t *symbol_table;
address_t current_address;
entry_t *integer_type;
entry_t *boolean_type;
bool initialize_table(address_t base_address, entry_t **ref)
{
current_address = base_address;
clear_table(ref);
// Os tipos elementares (“integer” e “boolean”) são as primeiras entradas da tabela de símbolos
// Todos os tipos elementares da linguagem devem ser criados e adicionados à tabela nesta função
type_t *base_type = create_type(form_atomic, 0, sizeof(value_t), NULL, NULL);
if (!base_type) {
mark_at(error_fatal, position_zero, "Not enough memory. By the way, who are you and what the hell is 42?");
return false;
}
entry_t *type = create_entry("integer", position_zero, class_type);
if (!type) {
mark_at(error_fatal, position_zero, "Not enough memory. By the way, who are you and what the hell is 42?");
free(base_type);
return false;
}
type->type = base_type;
integer_type = type;
add_entry(type, ref);
base_type = create_type(form_atomic, 0, sizeof(value_t), NULL, NULL);
if (!base_type) {
mark_at(error_fatal, position_zero, "Not enough memory. By the way, who are you and what the hell is 42?");
return false;
}
type = create_entry("boolean", position_zero, class_type);
if (!type) {
mark_at(error_fatal, position_zero, "Not enough memory. By the way, who are you and what the hell is 42?");
free(base_type);
return false;
}
type->type = base_type;
boolean_type = type;
add_entry(type, ref);
return true;
}
type_t *create_type(form_t form, value_t length, unsigned int size, entry_t *fields, type_t *base)
{
type_t *type = (type_t *)malloc(sizeof(type_t));
if (!type) {
mark(error_fatal, "Not enough memory. By the way, who are you and what the hell is 42?");
return NULL;
}
type->form = form;
type->length = length;
type->size = size;
type->fields = fields;
type->base = base;
return type;
}
link_t *create_link(fpos_t position)
{
link_t *link = (link_t *)malloc(sizeof(link_t));
if (!link) {
mark(error_fatal, "Not enough memory. By the way, who are you and what the hell is 42?");
return NULL;
}
link->position = position;
link->next = NULL;
return link;
}
entry_t *create_entry(identifier_t id, position_t position, class_t class)
{
entry_t *new_entry = (entry_t *)malloc(sizeof(entry_t));
if (!new_entry) {
mark_at(error_fatal, position, "Not enough memory. By the way, who are you and what the hell is 42?");
return NULL;
}
strcpy(new_entry->id, id);
new_entry->position = position;
new_entry->address = 0;
new_entry->class = class;
new_entry->type = NULL;
new_entry->value = 0;
new_entry->next = NULL;
return new_entry;
}
void clear_links(link_t **ref)
{
link_t *links = *ref;
while (links) {
link_t *current = links;
links = current->next;
free(current);
}
*ref = NULL;
}
void clear_table(entry_t **ref)
{
// TODO: Verificar quais tipos de entradas na tabela devem ser removidos primeiro
// Pode haver um problema ao liberar a memória para um “type” e este ainda ter referências para si
// em outras entradas. Contagem de referências seria uma solução…
entry_t *table = *ref;
while (table) {
entry_t *current = table;
table = current->next;
if (current->type && current->type->fields)
clear_table(¤t->type->fields);
free(current);
}
*ref = NULL;
}
void log_table(entry_t *table)
{
while (table) {
mark_at(error_log, position_zero, "Entry \"%s\" found.", table->id);
table = table->next;
}
}
entry_t *find_entry(identifier_t id, entry_t *table)
{
entry_t *current = table;
while (current) {
if (strcmp(current->id, id) == 0)
break;
current = current->next;
}
return current;
}
bool add_link(link_t *link, link_t **ref)
{
if (!ref || !link)
return false;
link_t *links = *ref;
if (links)
link->next = links;
*ref = link;
return true;
}
bool add_entry(entry_t *entry, entry_t **ref)
{
if (!ref || !entry)
return false;
entry_t *table = *ref;
entry_t *e = entry;
while (e) {
if (find_entry(e->id, table))
mark_at(error_parser, e->position, "The identifier \"%s\" has already been declared.", e->id);
e = e->next;
}
if (!table)
*ref = entry;
else {
while (table->next)
table = table->next;
table->next = entry;
}
return true;
}