4 #include "symbol_tbl.h"
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
)
21 for (i
= 0; i
< symbol_tbl_size
; i
++)
22 if (!strcmp(name
, symbol_tbl
[i
].name
))
23 return &symbol_tbl
[i
];
28 int symbol_tbl_lookup(const char *name
)
30 struct symbol
*symbol
;
32 symbol
= table_lookup(name
);
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);
46 int symbol_tbl_insert(const char *name
, int value
, int line_nr
)
50 sym
= table_lookup(name
);
52 error(line_nr
, "symbol %s redefined", name
);
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
);
65 static void dump_symbol(const struct symbol
*sym
)
67 printf("%s %d\n", sym
->name
, sym
->value
);
70 void symbol_tbl_dump(void)
74 for (i
= 0; i
< symbol_tbl_size
; i
++)
75 dump_symbol(&symbol_tbl
[i
]);