annotate lwcc/parse.c @ 512:7e41d6123175

Make conditional suppression actually hide comments and blank lines
author William Astle <lost@l-w.ca>
date Sat, 04 Jul 2020 16:21:08 -0600
parents a3e277c58df9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
2 lwcc/parse.c
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
3
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2013 William Astle
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
5
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
7
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
12
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
17
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
21
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
22 #include <stdio.h>
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
23 #include <string.h>
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
24 #include <lw_alloc.h>
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
25 #include <lw_string.h>
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
26
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include "cpp.h"
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include "tree.h"
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
29 #include "parse.h"
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
30
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
31 #include "parse_c.h"
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
32
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
33
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
34 void *Parse(void *parser, int tokid, struct tokendata *tdata, struct parserinfo *pi);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
35 void *ParseAlloc(void *(*alloc)(size_t size));
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
36 void ParseFree(void *parser, void (*free)(void *ptr));
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
37
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
38 void tokendata_free(struct tokendata *td)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
39 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
40 if (td)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
41 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
42 if (td -> strval)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
43 lw_free(td -> strval);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
44 lw_free(td);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
45 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
46 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
47
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
48 extern char *ptoken_names[];
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
49 char *tokendata_name(struct tokendata *td)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
50 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
51 if (td -> tokid < 0)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
52 return "****UNKNOWN****";
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
53 return ptoken_names[td -> tokid];
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
54 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
55
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
56 void tokendata_print(FILE *fp, struct tokendata *td)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
57 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
58 fprintf(fp, "TOKEN: %s", tokendata_name(td));
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
59 if (td -> strval)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
60 fprintf(fp, " \"%s\"", td -> strval);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
61 fprintf(fp, "\n");
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
62 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
63
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
64 #define TOK_KW_IF -1
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
65 #define TOK_KW_ELSE -2
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
66 #define TOK_KW_WHILE -3
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
67 #define TOK_KW_DO -4
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
68 #define TOK_KW_FOR -5
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
69 #define TOK_KW_VOID -6
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
70 #define TOK_KW_INT -7
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
71 #define TOK_KW_CHAR -8
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
72 #define TOK_KW_SHORT -9
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
73 #define TOK_KW_LONG -10
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
74 #define TOK_KW_UNSIGNED -11
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
75 #define TOK_KW_SIGNED -12
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
76 #define TOK_KW_FLOAT -13
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
77 #define TOK_KW_DOUBLE -14
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
78 #define TOK_KW_STRUCT -15
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
79 #define TOK_KW_UNION -16
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
80 #define TOK_KW_TYPEDEF -17
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
81 #define TOK_KW_STATIC -18
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
82 #define TOK_KW_SWITCH -19
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
83 #define TOK_KW_CASE -20
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
84 #define TOK_KW_DEFAULT -21
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
85 #define TOK_KW_BREAK -22
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
86 #define TOK_KW_CONTINUE -23
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
87 #define TOK_KW_CONST -24
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
88 #define TOK_KW_AUTO -25
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
89 #define TOK_KW_ENUM -26
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
90 #define TOK_KW_REGISTER -27
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
91 #define TOK_KW_SIZEOF -28
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
92 #define TOK_KW_VOLATILE -29
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
93 #define TOK_KW_RETURN -30
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
94 #define TOK_KW_EXTERN -31
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
95 #define TOK_KW_GOTO -32
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
96 #define TOK_TYPENAME -100
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
97
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
98 static struct { int tok; char *word; } keyword_list[] = {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
99 { TOK_KW_IF, "if" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
100 { TOK_KW_ELSE, "else" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
101 { TOK_KW_WHILE, "while" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
102 { TOK_KW_DO, "do" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
103 { TOK_KW_FOR, "for" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
104 { TOK_KW_VOID, "void" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
105 { TOK_KW_INT, "int" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
106 { TOK_KW_CHAR, "char" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
107 { TOK_KW_SHORT, "short" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
108 { TOK_KW_LONG, "long" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
109 { TOK_KW_UNSIGNED, "unsigned" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
110 { TOK_KW_SIGNED, "signed" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
111 { TOK_KW_FLOAT, "float" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
112 { TOK_KW_DOUBLE, "double" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
113 { TOK_KW_STRUCT, "struct" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
114 { TOK_KW_UNION, "union" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
115 { TOK_KW_TYPEDEF, "typedef" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
116 { TOK_KW_STATIC, "static" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
117 { TOK_KW_SWITCH, "switch" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
118 { TOK_KW_CASE, "case" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
119 { TOK_KW_DEFAULT, "default" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
120 { TOK_KW_BREAK, "break" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
121 { TOK_KW_CONTINUE, "continue" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
122 { TOK_KW_CONST, "const" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
123 { TOK_KW_AUTO, "auto" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
124 { TOK_KW_ENUM, "enum" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
125 { TOK_KW_REGISTER, "register" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
126 { TOK_KW_SIZEOF, "sizeof" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
127 { TOK_KW_VOLATILE, "volatile" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
128 { TOK_KW_RETURN, "return" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
129 { TOK_KW_EXTERN, "extern" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
130 { TOK_KW_GOTO, "goto" },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
131 { 0, "" }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
132 };
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
133
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
134 struct token *parse_next(struct preproc_info *pp)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
135 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
136 struct token *tok;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
137 int i;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
138
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
139 for (;;)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
140 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
141 tok = preproc_next(pp);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
142 if (tok -> ttype == TOK_WSPACE)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
143 continue;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
144 if (tok -> ttype == TOK_EOL)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
145 continue;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
146 if (tok -> ttype == TOK_CHAR)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
147 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
148 // random character
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
149 fprintf(stderr, "Random character %02x\n", tok -> strval[0]);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
150 if (tok -> strval[0] < 32 || tok -> strval[0] > 126)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
151 continue;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
152 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
153 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
154 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
155 if (tok -> ttype == TOK_IDENT)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
156 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
157 /* convert identifier tokens to their respective meanings */
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
158 for (i = 0; keyword_list[i].tok != TOK_NONE; i++)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
159 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
160 if (strcmp(keyword_list[i].word, tok -> strval) == 0)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
161 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
162 tok -> ttype = keyword_list[i].tok;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
163 goto out;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
164 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
165 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
166 /* check for a registered type here */
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
167 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
168 out:
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
169 fprintf(stderr, "Lexed: ");
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
170 token_print(tok, stderr);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
171 fprintf(stderr, " (%d)\n", tok -> ttype);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
172 return tok;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
173 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
174
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
175 static struct {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
176 int tokid;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
177 int ttype;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
178 } toktable[] = {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
179 { PTOK_IDENTIFIER, TOK_IDENT },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
180 { PTOK_ENDS, TOK_EOS },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
181 { PTOK_KW_INT, TOK_KW_INT },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
182 { PTOK_KW_LONG, TOK_KW_LONG },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
183 { PTOK_KW_SHORT, TOK_KW_SHORT },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
184 { PTOK_KW_CHAR, TOK_KW_CHAR },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
185 { PTOK_KW_SIGNED, TOK_KW_SIGNED },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
186 { PTOK_KW_UNSIGNED, TOK_KW_UNSIGNED },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
187 { PTOK_STAR, TOK_STAR },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
188 { PTOK_KW_VOID, TOK_KW_VOID },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
189 { PTOK_KW_FLOAT, TOK_KW_FLOAT },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
190 { PTOK_KW_DOUBLE, TOK_KW_DOUBLE },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
191 { PTOK_OBRACE, TOK_OBRACE },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
192 { PTOK_CBRACE, TOK_CBRACE },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
193 { PTOK_OPAREN, TOK_OPAREN },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
194 { PTOK_CPAREN, TOK_CPAREN },
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
195 { 0, 0 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
196 };
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
197
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
198 static int lookup_ptok(int ttype)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
199 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
200 int i;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
201 for (i = 0; toktable[i].tokid != 0; i++)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
202 if (toktable[i].ttype == ttype)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
203 return toktable[i].tokid;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
204 return -1;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
205 }
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
206
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
207 node_t *parse_program(struct preproc_info *pp)
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
208 {
314
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
209 struct token *tok;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
210 struct tokendata *td;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
211 struct parserinfo pi = { NULL };
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
212 void *parser;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
213
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
214 /* the cast below shuts up a warning */
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
215 parser = ParseAlloc((void *)lw_alloc);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
216 for (;;)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
217 {
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
218 tok = parse_next(pp);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
219 if (tok -> ttype == TOK_EOF)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
220 break;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
221
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
222 td = lw_alloc(sizeof(struct tokendata));
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
223 td -> strval = NULL;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
224 td -> numval[0] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
225 td -> numval[1] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
226 td -> numval[2] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
227 td -> numval[3] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
228 td -> numval[4] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
229 td -> numval[5] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
230 td -> numval[6] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
231 td -> numval[7] = 0;
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
232 td -> tokid = lookup_ptok(tok -> ttype);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
233 if (tok -> strval)
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
234 td -> strval = lw_strdup(tok -> strval);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
235
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
236 tokendata_print(stderr, td);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
237
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
238 Parse(parser, td -> tokid, td, &pi);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
239 }
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
240 Parse(parser, 0, NULL, &pi);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
241 ParseFree(parser, lw_free);
a3e277c58df9 Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents: 312
diff changeset
242 return pi.parsetree;
312
41118fb0a8f2 Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff changeset
243 }