Grafo da AST bem melhor. Loopando infinitamente ao montar tabela de simbolos.
[toypasc.git] / symbol_table.c
blobc40d83a7d67ec3787ec574c1ef062175f127ec28
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "symbol_table.h"
6 Symbol *
7 symbol_new(char const * name)
9 Symbol * symbol = (Symbol *) malloc (sizeof(Symbol));
10 symbol->type = NONE_TYPE;
11 value_set(&symbol->value, symbol->type, NULL);
12 symbol->next = NULL;
14 if (name != NULL)
15 symbol->name = strdup(name);
16 else
17 symbol->name = NULL;
19 return symbol;
22 /* Insere um simbolo na tabela indicada.
23 * Caso o simbolo ja exista, a memoria do
24 * simbolo do parametro sera liberada.
25 * Returns: O simbolo passado como parametro,
26 * caso este ainda nao esteja na tabela,
27 * ou um ponteiro para o simbolo encontrado.
29 Symbol *
30 symbol_insert(Symbol *symtab, Symbol *symbol)
32 Symbol *sym;
34 if (symbol == NULL)
35 return NULL;
37 sym = symbol_lookup(symtab->next, symbol->name);
39 if (sym != NULL) {
40 free(symbol->name);
41 free(symbol);
42 return sym;
45 //printf("symbol %x (next %x)\nsym %x\n", symbol, symbol->next, sym);
47 symbol->next = symtab->next;
48 symtab->next = symbol;
50 symbol_table_dump(symtab);
51 //printf("symbol %x (next %x)\nsym %x\n", symbol, symbol->next, sym);
52 return symbol;
55 Symbol *
56 symbol_lookup(Symbol *symtab, char const *name)
58 Symbol *temp;
60 for (temp = symtab; temp != NULL; temp = temp->next) {
61 if (!strcmp (temp->name, name))
62 return temp;
65 return temp;
68 void
69 symbol_table_destroy(Symbol *symtab)
71 Symbol *first;
72 Symbol *to_kill;
73 first = symtab->next;
74 symtab->next = NULL;
76 while (first != NULL) {
77 to_kill = first;
78 first = first->next;
79 if (to_kill->name != NULL)
80 free(to_kill->name);
81 free(to_kill);
85 void
86 symbol_print(Symbol *symbol)
88 if (symbol == NULL) {
89 printf("NULL\n\n");
90 return;
93 printf("Symbol: %x\n", symbol);
94 printf("name: %s\n", symbol->name);
95 printf("type: %d\n", symbol->type);
96 printf("value:");
97 value_print(&symbol->value, symbol->type);
98 printf("\nnext: %x\n\n", symbol->next);
101 void
102 symbol_table_dump(Symbol *symtab)
104 Symbol *temp = symtab;
106 for (temp = symtab->next; temp != NULL; temp = temp->next)
107 symbol_print(temp);