Mercurial > hg > index.cgi
annotate lwcc/tree.h @ 498:1bd2d590d734
Rejig parser to eliminate lemon
No longer use lemon for building the parser. It adds too much complexity,
really, and a hand written recursive descent parser is far more flexible.
The current iteration can handle exactly one statement: "return <int>".
author | William Astle <lost@l-w.ca> |
---|---|
date | Thu, 08 Aug 2019 23:32:23 -0600 |
parents | a3e277c58df9 |
children | f3e9732973f1 |
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/tree.h |
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 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
22 #ifndef tree_h_seen___ |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
23 #define tree_h_seen___ |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
24 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
25 #include <stdio.h> |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
26 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
27 /* the various node types */ |
314
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
28 #define NODE_NONE 0 // a node with no type |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
29 #define NODE_PROGRAM 1 // the whole program |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
30 #define NODE_DECL 2 // a declaration |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
31 #define NODE_TYPE_CHAR 3 // a character type |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
32 #define NODE_TYPE_SHORT 4 // short int |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
33 #define NODE_TYPE_INT 5 // integer |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
34 #define NODE_TYPE_LONG 6 // long int |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
35 #define NODE_TYPE_LONGLONG 7 // long long |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
36 #define NODE_IDENT 8 // an identifier of some kind |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
37 #define NODE_TYPE_PTR 9 // a pointer |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
38 #define NODE_TYPE_SCHAR 10 // signed char |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
39 #define NODE_TYPE_UCHAR 11 // unsigned char |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
40 #define NODE_TYPE_USHORT 12 // unsigned short |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
41 #define NODE_TYPE_UINT 13 // unsigned int |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
42 #define NODE_TYPE_ULONG 14 // unsigned long |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
43 #define NODE_TYPE_ULONGLONG 15 // unsigned long long |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
44 #define NODE_TYPE_VOID 16 // void |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
45 #define NODE_TYPE_FLOAT 17 // float |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
46 #define NODE_TYPE_DOUBLE 18 // double |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
47 #define NODE_TYPE_LDOUBLE 19 // long double |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
48 #define NODE_FUNDEF 20 // function definition |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
49 #define NODE_FUNDECL 21 // function declaration |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
50 #define NODE_FUNARGS 22 // list of function args |
a3e277c58df9
Checkpoint parser development for lwcc
William Astle <lost@l-w.ca>
parents:
312
diff
changeset
|
51 #define NODE_BLOCK 23 // statement block |
498
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
52 #define NODE_STMT_RETURN 24 // return statement |
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
53 #define NODE_CONST_INT 25 // constant integer |
1bd2d590d734
Rejig parser to eliminate lemon
William Astle <lost@l-w.ca>
parents:
314
diff
changeset
|
54 #define NODE_NUMTYPES 26 // the number of node types |
312
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
55 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
56 typedef struct node_s node_t; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
57 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
58 struct node_s |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
59 { |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
60 int type; // node type |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
61 char *strval; // any string value associated with the node |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
62 unsigned char ival[8]; // any 64 bit integer value associated with the node, signed or unsigned |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
63 node_t *children; // pointer to list of child nodes |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
64 node_t *next_child; // pointer to next child in the list |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
65 node_t *parent; // pointer to parent node |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
66 }; |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
67 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
68 extern node_t *node_create(int, ...); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
69 extern void node_destroy(node_t *); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
70 extern void node_addchild(node_t *, node_t *); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
71 extern void node_removechild(node_t *, node_t *); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
72 extern void node_display(node_t *, FILE *); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
73 extern void node_removechild_destroy(node_t *, node_t *); |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
74 |
41118fb0a8f2
Add no-op parser and parse tree infrastructure
William Astle <lost@l-w.ca>
parents:
diff
changeset
|
75 #endif // tree_h_seen___ |