ACT Library
Loading...
Searching...
No Matches
expr.h
Go to the documentation of this file.
1/*************************************************************************
2 *
3 * Standard expression parsing library
4 *
5 * Copyright (c) 1999-2010, 2019 Rajit Manohar
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 **************************************************************************
23 */
24#ifndef __EXPR_H__
25#define __EXPR_H__
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <common/file.h>
32#include <common/pp.h>
33#include <act/act_parse_id.h>
34
35#define E_AND 0
36#define E_OR 1
37#define E_NOT 2
38#define E_PLUS 3
39#define E_MINUS 4
40#define E_MULT 5
41#define E_DIV 6
42#define E_MOD 7
43#define E_LSL 8
44#define E_LSR 9
45#define E_ASR 10
46#define E_UMINUS 11
47#define E_INT 12 /* not a real token */
48#define E_VAR 13 /* not a real token */
49#define E_QUERY 14
50#define E_LPAR 15
51#define E_RPAR 16
52#define E_XOR 17
53#define E_LT 18
54#define E_GT 19
55#define E_LE 20
56#define E_GE 21
57#define E_EQ 22
58#define E_NE 23
59#define E_TRUE 24 /* not a real token */
60#define E_FALSE 25 /* not a real token */
61#define E_COLON 26
62#define E_PROBE 27
63#define E_COMMA 28
64#define E_CONCAT 29
65#define E_BITFIELD 30
66#define E_COMPLEMENT 31 /* bitwise complement */
67#define E_REAL 32 /* not a real token */
68
69#define E_RAWFREE 33
70
71#define E_END 34
72
73#define E_NUMBER 35 /* # of E_xxx things */
74
75#define E_FUNCTION 100
76
77#pragma GCC diagnostic push
78#pragma GCC diagnostic ignored "-Wpedantic"
79typedef struct expr {
80 int type;
81 union {
82 struct {
83 struct expr *l, *r;
84 } e;
85 struct {
86 char *s;
87 struct expr *r;
88 } fn;
89 struct {
90 unsigned int val;
91 unsigned long extra;
92 } x;
93 double f;
94 struct {
95 unsigned long v;
96 void *v_extra;
98 } u;
100#pragma GCC diagnostic pop
101
102/* Function calls are represented as:
103
104 type = E_FUNCTION
105
106 l = function name
107
108 E_FUNCTION
109 / |
110 name E_LT
111 / \
112 arg1 E_LT
113 / \
114 arg2 ...
115
116
117 A ? B : C is represented as:
118
119 E_QUERY
120 / \
121 A E_COLON
122 / \
123 B C
124
125
126 type = E_CONCAT
127
128 E_CONCAT
129 / |
130 expr E_CONCAT
131 / |
132 expr ...
133
134
135 type = E_BITFIELD
136
137 E_BITFIELD
138 / |
139 var E_BITFIELD
140 / |
141 lo hi
142*/
143
144
145void expr_settoken (int name, int value);
146 /*
147 Set the lexical id for token name (from the E_xxx list above) to
148 value. "value" is the integer that lex_addtoken() returns.
149 To delete expression operators, simply set the value of a token
150 to -1.
151 */
152
153
154int expr_parse_isany (LFILE *l);
155 /* return 0 if this is definitely not an expression */
156
158 /*
159 Parse any expression
160 */
161
163 /*
164 Parse an integer expression and returns its parse tree.
165 */
166
168 /*
169 Parse a Boolean expression and return its parse tree.
170 */
171
173 /*
174 Parse a real expression and return its parse tree.
175 */
176
177void expr_free (Expr *e);
178 /*
179 Free expression tree
180 */
181
182int expr_init (LFILE *l);
183 /*
184 Initialize all tokens with standard symbols, and add tokens to the
185 lexer. Returns the number of tokens added. This number might be
186 less than the # of tokens used by the expression evaluation package
187 because the lexer may already know about some of the tokens.
188 */
189
190void expr_clear (void);
191 /*
192 Clears expressions from expression parser
193 */
194
195void expr_endgtmode (int m);
196
197extern pId *(*expr_parse_id)(LFILE *l);
198 /* The function must do the following:
199 - if there is a valid identifier, then create a data structure
200 for it and return a pointer to it.
201 - if there isn't, don't do anything, return a NULL pointer. The
202 lexer state must be unchanged in this case.
203 */
204extern void (*expr_free_id) (void *);
205 /* Free an allocated id structure */
206
207extern void (*expr_print_id) (pp_t *, void *);
208 /* Print an allocated id structure */
209
210extern void (*expr_print_probe) (pp_t *, void *);
211 /* Print #chan */
212
213
214extern Expr *(*expr_parse_basecase_num)(LFILE *l);
215 /* if you want to support your own basecase for numerical expr */
216
217extern Expr *(*expr_parse_basecase_bool)(LFILE *l);
218 /* if you want to support your own basecase for bool expr */
219
220extern int (*expr_parse_newtokens)(LFILE *l);
221
222 /* define to free special basecase; return 1 if it was used, 0 otherwise */
223extern int (*expr_free_special_default)(Expr *);
224
225extern void expr_inc_parens (void);
226extern void expr_dec_parens (void);
227
228
229extern int expr_gettoken (int t);
230 /* needed for external parsing support */
231
232extern void expr_print (pp_t *, Expr *);
233/* Print expression */
234
235extern const char *expr_operator_name (int type);
236/* return string corresponding to the operator token */
237
238#ifdef __cplusplus
239}
240#endif
241
242#endif /* __EXPR_H__ */
int(* expr_parse_newtokens)(LFILE *l)
void expr_free(Expr *e)
int expr_gettoken(int t)
void expr_dec_parens(void)
void expr_print(pp_t *, Expr *)
void(* expr_free_id)(void *)
void expr_inc_parens(void)
int expr_init(LFILE *l)
void expr_endgtmode(int m)
Expr * expr_parse_real(LFILE *l)
Expr * expr_parse_any(LFILE *l)
void(* expr_print_id)(pp_t *, void *)
void(* expr_print_probe)(pp_t *, void *)
struct expr Expr
const char * expr_operator_name(int type)
void expr_clear(void)
void expr_settoken(int name, int value)
Expr * expr_parse_int(LFILE *l)
Expr * expr_parse_bool(LFILE *l)
int expr_parse_isany(LFILE *l)
int(* expr_free_special_default)(Expr *)
Definition: expr.h:79
union expr::@12 u
struct expr::@12::@14 fn
struct expr * r
Definition: expr.h:83
struct expr * l
Definition: expr.h:83
unsigned int val
Definition: expr.h:90
unsigned long v
Definition: expr.h:95
struct expr::@12::@13 e
struct expr::@12::@15 x
double f
Definition: expr.h:93
unsigned long extra
Definition: expr.h:91
void * v_extra
Definition: expr.h:96
int type
Definition: expr.h:80
struct expr::@12::@16 ival
char * s
Definition: expr.h:86