Mais um atributo redundante removido: is_parameter de Symbol.
[toypasc.git] / symbol_table.c
blob2c098b30ab887c342667bc2203a79c97184a377d
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->params = -1;
13 symbol->param_types = NULL;
14 symbol->decl_linenum = 0;
15 symbol->is_global = FALSE;
16 symbol->stack_index = -1;
18 symbol->next = NULL;
20 if (name != NULL)
21 symbol->name = strdup(name);
22 else
23 symbol->name = NULL;
25 return symbol;
28 /* Insere um simbolo na tabela indicada.
29 * Caso o simbolo ja exista, a memoria do
30 * simbolo do parametro sera liberada.
31 * Returns: O simbolo passado como parametro,
32 * caso este ainda nao esteja na tabela,
33 * ou um ponteiro para o simbolo encontrado.
35 Symbol *
36 symbol_insert(Symbol *symtab, Symbol *symbol)
38 Symbol *sym;
40 if (symbol == NULL)
41 return NULL;
43 sym = symbol_lookup(symtab, symbol->name);
45 if (sym != NULL) {
46 free(symbol->name);
47 free(symbol);
48 return sym;
51 symbol->next = symtab->next;
52 symtab->next = symbol;
54 return symbol;
57 Symbol *
58 symbol_lookup(Symbol *symtab, char const *name)
60 Symbol *temp;
62 if (symtab == NULL)
63 return NULL;
65 for (temp = symtab->next; temp != NULL; temp = temp->next) {
66 if (!strcmp (temp->name, name))
67 return temp;
70 return temp;
73 void
74 symbol_table_destroy(Symbol *symtab)
76 Symbol *first;
77 Symbol *to_kill;
78 first = symtab->next;
79 symtab->next = NULL;
81 while (first != NULL) {
82 to_kill = first;
83 first = first->next;
84 if (to_kill->name != NULL)
85 free(to_kill->name);
86 free(to_kill);
90 void
91 symbol_create_params(Symbol *symbol, int quantity)
93 symbol->params = quantity;
94 if (quantity > 0)
95 symbol->param_types = (int *) malloc (sizeof(int) * quantity);
98 bool
99 symbol_is_procfunc(Symbol *symbol)
101 return (symbol->params > -1);
104 void
105 symbol_print(Symbol *symbol)
107 if (symbol == NULL) {
108 printf("NULL\n\n");
109 return;
112 printf("Symbol: %x\n", symbol);
113 printf("name: %s\n", symbol->name);
114 printf("type: %d\n", symbol->type);
115 printf("value:");
116 value_print(stdout, &symbol->value, symbol->type);
117 printf("\ndeclaration line: %d\n", symbol->decl_linenum);
118 printf("next: %x\n\n", symbol->next);
121 void
122 symbol_table_dump(Symbol *symtab)
124 Symbol *temp = symtab;
126 for (temp = symtab->next; temp != NULL; temp = temp->next)
127 symbol_print(temp);