Iniciado trabalho com llvm_codegen_visitor.
[toypasc.git] / symbol_table.c
blob6bc1bf9d15b520e4775a915d2a4c607c53b5dfb0
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 = VOID;
11 value_set(&symbol->value, symbol->type, NULL);
12 symbol->decl_linenum = 0;
13 symbol->params = 0;
14 symbol->param_types = (int *) malloc (sizeof(int) * 10);
16 symbol->next = NULL;
18 if (name != NULL)
19 symbol->name = strdup(name);
20 else
21 symbol->name = NULL;
23 return symbol;
26 /* Insere um simbolo na tabela indicada.
27 * Caso o simbolo ja exista, a memoria do
28 * simbolo do parametro sera liberada.
29 * Returns: O simbolo passado como parametro,
30 * caso este ainda nao esteja na tabela,
31 * ou um ponteiro para o simbolo encontrado.
33 Symbol *
34 symbol_insert(Symbol *symtab, Symbol *symbol)
36 Symbol *sym;
38 if (symbol == NULL)
39 return NULL;
41 sym = symbol_lookup(symtab, symbol->name);
43 if (sym != NULL) {
44 free(symbol->name);
45 free(symbol);
46 return sym;
49 symbol->next = symtab->next;
50 symtab->next = symbol;
52 return symbol;
55 Symbol *
56 symbol_lookup(Symbol *symtab, char const *name)
58 Symbol *temp;
60 if (symtab == NULL)
61 return NULL;
63 for (temp = symtab->next; temp != NULL; temp = temp->next) {
64 if (!strcmp (temp->name, name))
65 return temp;
68 return temp;
71 void
72 symbol_table_destroy(Symbol *symtab)
74 Symbol *first;
75 Symbol *to_kill;
76 first = symtab->next;
77 symtab->next = NULL;
79 while (first != NULL) {
80 to_kill = first;
81 first = first->next;
82 if (to_kill->name != NULL)
83 free(to_kill->name);
84 free(to_kill);
88 void
89 symbol_print(Symbol *symbol)
91 if (symbol == NULL) {
92 printf("NULL\n\n");
93 return;
96 printf("Symbol: %x\n", symbol);
97 printf("name: %s\n", symbol->name);
98 printf("type: %d\n", symbol->type);
99 printf("value:");
100 value_print(stdout, &symbol->value, symbol->type);
101 printf("\ndeclaration line: %d\n", symbol->decl_linenum);
102 printf("next: %x\n\n", symbol->next);
105 void
106 symbol_table_dump(Symbol *symtab)
108 Symbol *temp = symtab;
110 for (temp = symtab->next; temp != NULL; temp = temp->next)
111 symbol_print(temp);