annotate lwbasic/symtab.c @ 30:bcd532a90e53

Renamed "compiler" to "parser" for more consistent terminology
author lost@l-w.ca
date Thu, 03 Feb 2011 21:19:11 -0700
parents bc96cd02fbf4
children 49d608aecc4d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
1 /*
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
2 symtab.c
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
3
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2011 William Astle
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
5
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
7
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
11 version.
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
12
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
16 more details.
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
17
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
20 */
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
21
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
22 /*
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
23 Symbol table handling
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
24 */
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
25
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
26 #include <stdlib.h>
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
27 #include <string.h>
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
28
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
29 #include <lw_alloc.h>
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
30 #include <lw_string.h>
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
31
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
32 #define __symtab_c_seen__
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
33 #include "symtab.h"
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
34
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
35 symtab_t *symtab_init(void)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
36 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
37 symtab_t *st;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
38
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
39 st = lw_alloc(sizeof(symtab_t));
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
40 st -> head = NULL;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
41 return st;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
42 }
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
43
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
44 void symtab_destroy(symtab_t *st)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
45 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
46 symtab_entry_t *se;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
47
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
48 while (st -> head)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
49 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
50 se = st -> head;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
51 st -> head = se -> next;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
52 lw_free(se -> name);
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
53 lw_free(se);
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
54 }
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
55 lw_free(st);
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
56 }
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
57
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
58 symtab_entry_t *symtab_find(symtab_t *st, char *name)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
59 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
60 symtab_entry_t *se;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
61
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
62 for (se = st -> head; se; se = se -> next)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
63 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
64 if (strcmp(se -> name, name) == 0)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
65 return se;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
66 }
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
67 return NULL;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
68 }
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
69
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
70 void symtab_register(symtab_t *st, char *name, int addr, int symtype)
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
71 {
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
72 symtab_entry_t *se;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
73
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
74 se = lw_alloc(sizeof(symtab_entry_t));
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
75 se -> name = lw_strdup(name);
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
76 se -> addr = addr;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
77 se -> symtype = symtype;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
78 se -> next = st -> head;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
79 st -> head = se;
bc96cd02fbf4 Added basic symbol table structure
lost@l-w.ca
parents:
diff changeset
80 }