diff options
Diffstat (limited to 'lib/forlan.h')
-rw-r--r-- | lib/forlan.h | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/forlan.h b/lib/forlan.h new file mode 100644 index 0000000..4dadc70 --- /dev/null +++ b/lib/forlan.h | |||
@@ -0,0 +1,123 @@ | |||
1 | /* This file is part of Eclat. | ||
2 | Copyright (C) 2012 Sergey Poznyakoff. | ||
3 | |||
4 | Eclat is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | Eclat is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #define FORLAN_DBG_LEX 3 | ||
18 | #define FORLAN_DBG_GRAM 2 | ||
19 | #define FORLAN_DBG_EVAL 1 | ||
20 | |||
21 | extern int forlan_dbg; | ||
22 | |||
23 | void forlan_init(); | ||
24 | void forlan_lex_begin(const char *input, size_t length, | ||
25 | struct grecs_locus_point *pt); | ||
26 | void forlan_lex_end(void); | ||
27 | int forlan_parse(const char *input, size_t length, | ||
28 | struct grecs_locus_point *pt); | ||
29 | |||
30 | union forlan_node; /* Declared below */ | ||
31 | |||
32 | enum forlan_type { | ||
33 | forlan_type_null, /* Unknown/unset type */ | ||
34 | forlan_type_comp, /* A path component */ | ||
35 | forlan_type_test, /* Value test (f[X]) */ | ||
36 | forlan_type_func, /* Function call */ | ||
37 | forlan_type_cond, /* Conditional */ | ||
38 | forlan_type_stmt, /* Statement */ | ||
39 | forlan_type_lit, /* Literal */ | ||
40 | forlan_type_expr, /* Boolean expression */ | ||
41 | forlan_type_last /* Return last evaluated grecs_node */ | ||
42 | }; | ||
43 | |||
44 | /* A path component */ | ||
45 | struct forlan_node_comp { | ||
46 | enum forlan_type type; | ||
47 | int abs; | ||
48 | union forlan_node *node; | ||
49 | }; | ||
50 | |||
51 | /* Path test: .path.comp[value] */ | ||
52 | struct forlan_node_test { | ||
53 | enum forlan_type type; | ||
54 | char *comp; | ||
55 | char *value; | ||
56 | }; | ||
57 | |||
58 | /* Function call */ | ||
59 | struct forlan_node_func { | ||
60 | enum forlan_type type; | ||
61 | void *fp; /* FIXME: replace with typedef */ | ||
62 | struct grecs_list *args; /* Arguments are struct forlan_node * */ | ||
63 | }; | ||
64 | |||
65 | /* Conditional */ | ||
66 | struct forlan_node_cond { | ||
67 | enum forlan_type type; | ||
68 | union forlan_node *expr; /* Controlling expression */ | ||
69 | union forlan_node *iftrue; /* Run this if expr yields true */ | ||
70 | union forlan_node *iffalse; /* Run this if expr yields false */ | ||
71 | }; | ||
72 | |||
73 | /* Statement or statement list */ | ||
74 | struct forlan_node_stmt { | ||
75 | enum forlan_type type; | ||
76 | union forlan_node *stmt; | ||
77 | union forlan_node *next; | ||
78 | }; | ||
79 | |||
80 | /* Literal string */ | ||
81 | struct forlan_node_lit { | ||
82 | enum forlan_type type; | ||
83 | char *string; | ||
84 | }; | ||
85 | |||
86 | /* Boolean opcodes */ | ||
87 | enum forlan_opcode { | ||
88 | forlan_opcode_node, /* Evaluate node, set 'last' */ | ||
89 | forlan_opcode_and, /* Boolean AND */ | ||
90 | forlan_opcode_or, /* Boolean OR */ | ||
91 | forlan_opcode_not /* Boolean NOT */ | ||
92 | }; | ||
93 | |||
94 | /* Boolean expression */ | ||
95 | struct forlan_node_expr { | ||
96 | enum forlan_type type; | ||
97 | enum forlan_opcode opcode; | ||
98 | union forlan_node *arg[2]; | ||
99 | }; | ||
100 | |||
101 | /* Now get all this together */ | ||
102 | union forlan_node { | ||
103 | enum forlan_type type; | ||
104 | struct forlan_node_comp comp; /* A path component */ | ||
105 | struct forlan_node_test test; /* Value test (f[X]) */ | ||
106 | struct forlan_node_func func; /* Function call */ | ||
107 | struct forlan_node_cond cond; /* Conditional */ | ||
108 | struct forlan_node_stmt stmt; /* Statement */ | ||
109 | struct forlan_node_lit lit; /* Literal */ | ||
110 | struct forlan_node_expr expr; /* Boolean expression */ | ||
111 | /* forlan_type_last needs no additional data */ | ||
112 | }; | ||
113 | |||
114 | union forlan_node *forlan_node_create(enum forlan_type type); | ||
115 | void forlan_node_free(union forlan_node *); | ||
116 | struct grecs_list *forlan_stmt_list(void); | ||
117 | struct grecs_list *forlan_complist(void); | ||
118 | union forlan_node *forlan_stmt_from_list(struct grecs_list *list); | ||
119 | |||
120 | extern union forlan_node *forlan_parse_tree; | ||
121 | |||
122 | void forlan_dump_node(FILE *fp, union forlan_node *p, int *num, int lev); | ||
123 | void forlan_dump_tree(FILE *fp, union forlan_node *node); | ||