Introduce old redir program
[lcapit-junk-code.git] / pet-projects / DLW-1e / das / symbol_tbl.c
blobce90a7695df25d7aa2f63e28a145fca119e94a40
1 #include <stdio.h>
2 #include <string.h>
4 #include "symbol_tbl.h"
5 #include "error.h"
7 struct symbol {
8 char name[LABEL_LEN];
9 int value;
12 #define SYM_TBL_MAX_SIZE 16
14 static size_t symbol_tbl_size;
15 static struct symbol symbol_tbl[SYM_TBL_MAX_SIZE];
17 static struct symbol *table_lookup(const char *name)
19 size_t i;
21 for (i = 0; i < symbol_tbl_size; i++)
22 if (!strcmp(name, symbol_tbl[i].name))
23 return &symbol_tbl[i];
25 return NULL;
28 int symbol_tbl_lookup(const char *name)
30 struct symbol *symbol;
32 symbol = table_lookup(name);
33 if (!symbol)
34 return -1;
36 return symbol->value;
39 static void fill_symbol(struct symbol *new, const char *name, int value)
41 memset(new, 0, sizeof(struct symbol));
42 strncpy(new->name, name, LABEL_LEN - 1);
43 new->value = value;
46 int symbol_tbl_insert(const char *name, int value, int line_nr)
48 struct symbol *sym;
50 sym = table_lookup(name);
51 if (sym) {
52 error(line_nr, "symbol %s redefined", name);
53 return -1;
56 if (symbol_tbl_size == SYM_TBL_MAX_SIZE)
57 fatal("symbol table is full");
59 fill_symbol(&symbol_tbl[symbol_tbl_size], name, value);
60 symbol_tbl_size++;
62 return 0;
65 static void dump_symbol(const struct symbol *sym)
67 printf("%s %d\n", sym->name, sym->value);
70 void symbol_tbl_dump(void)
72 size_t i;
74 for (i = 0; i < symbol_tbl_size; i++)
75 dump_symbol(&symbol_tbl[i]);