Mudancas no parser.y
[toypasc.git] / symbol_table.c
blob876b288dac5b93e35ff5e123d95fd2d54ecebac2
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;
23 Symbol *
24 symbol_insert(Symbol *table, char const *name)
26 Symbol *symbol = symbol_lookup(table, name);
28 if (symbol == NULL) {
29 symbol = symbol_new(name);
30 symbol->next = table;
31 return symbol;
34 return table;
37 Symbol *
38 symbol_lookup(Symbol *table, char const *name)
40 Symbol *temp;
41 temp = table;
43 while (temp != NULL) {
44 if (!strcmp (temp->name, name))
45 return temp;
47 temp = temp->next;
50 return temp;
53 void
54 symbol_table_destroy(Symbol **table)
56 Symbol *first;
57 Symbol *to_kill;
58 first = *table;
59 *table = NULL;
61 while (first != NULL) {
62 to_kill = first;
63 first = first->next;
64 free(to_kill);
67 free(first);
70 void
71 symbol_print(Symbol *symbol)
73 if (symbol == NULL) {
74 printf("NULL\n\n");
75 return;
78 printf("Symbol: %x\n", symbol);
79 printf("name: %s\n", symbol->name);
80 printf("type: %d\n", symbol->type);
81 printf("value:");
82 value_print(&symbol->value, symbol->type);
83 printf("\nnext: %x\n\n", symbol->next);
86 void
87 symbol_table_dump(Symbol *table)
89 Symbol *temp = table;
91 while(temp != NULL) {
92 symbol_print(temp);
93 temp = temp->next;
96 free(temp);