comparison lwcc/symbol.c @ 296:83fcc1ed6ad6 ccdev

Checkpoint lwcc development Initial untested version of the preprocessor with macro expansion but without file inclusion.
author William Astle <lost@l-w.ca>
date Sat, 14 Sep 2013 20:04:38 -0600
parents
children 856caf91ffaa
comparison
equal deleted inserted replaced
295:4b17780f2777 296:83fcc1ed6ad6
1 /*
2 lwcc/symbol.c
3
4 Copyright © 2013 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <string.h>
23
24 #include <lw_alloc.h>
25 #include <lw_string.h>
26
27 #include "cpp.h"
28 #include "symbol.h"
29 #include "token.h"
30
31 void symbol_free(struct symtab_e *s)
32 {
33 int i;
34 struct token *t;
35
36 lw_free(s -> name);
37
38 for (i = 0; i < s -> nargs; i++)
39 lw_free(s -> params[i]);
40 lw_free(s -> params);
41 while (s -> tl)
42 {
43 t = s -> tl;
44 s -> tl = t -> next;
45 token_free(t);
46 }
47 }
48
49 struct symtab_e *symtab_find(struct preproc_info *pp, char *name)
50 {
51 struct symtab_e *s;
52
53 for (s = pp -> sh; s; s = s -> next)
54 {
55 if (strcmp(s -> name, name) == 0)
56 {
57 return s;
58 }
59 }
60 return NULL;
61 }
62
63 void symtab_undef(struct preproc_info *pp, char *name)
64 {
65 struct symtab_e *s, **p;
66
67 p = &(pp -> sh);
68 for (s = pp -> sh; s; s = s -> next)
69 {
70 if (strcmp(s -> name, name) == 0)
71 {
72 (*p) -> next = s -> next;
73 symbol_free(s);
74 return;
75 }
76 p = &((*p) -> next);
77 }
78 }
79
80 void symtab_define(struct preproc_info *pp, char *name, struct token *def, int nargs, char **params, int vargs)
81 {
82 struct symtab_e *s;
83
84 s = lw_alloc(sizeof(struct symtab_e));
85 s -> name = lw_strdup(name);
86 s -> tl = def;
87 s -> nargs = nargs;
88 s -> params = params;
89 s -> vargs = vargs;
90 s -> next = pp -> sh;
91 pp -> sh = s;
92 }