Mercurial > hg-old > index.cgi
comparison src/lwval.h @ 15:1f598d89b9b0
Started creating expression parser
author | lost |
---|---|
date | Thu, 23 Oct 2008 03:37:48 +0000 |
parents | src/expr.h@b28d7cb60779 |
children | 4f14eae64d38 |
comparison
equal
deleted
inserted
replaced
14:b28d7cb60779 | 15:1f598d89b9b0 |
---|---|
1 /* | |
2 lwval.h | |
3 Copyright © 2008 William Astle | |
4 | |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 /* | |
22 This file contains definitions associated with the expression evaluator used | |
23 by LWASM. | |
24 | |
25 The core of the entire expression handler is the opaque type LWVAL, pointers | |
26 to which are passed around to keep track of values. A value may be a simple | |
27 integer or it could be a more complex expression linked by operators or it | |
28 could be a polynomial expression. Simple integers are merely a degenerate | |
29 case of polynomials. | |
30 | |
31 The package understands the following operations: | |
32 | |
33 addition | |
34 subtraction | |
35 multiplication | |
36 division | |
37 modulus | |
38 parentheses | |
39 unary negation | |
40 unary "positive" | |
41 bitwise and | |
42 bitwise or | |
43 bitwise not (1's complement) | |
44 bitwise exclusive or | |
45 | |
46 Infix operators can be expressed as LWVAL <op> LWVAL. Thus, the order of | |
47 operations is only relevant when initially parsing the expression. The order | |
48 of evaluation is determined by what appears on either side of the <op> as | |
49 an LWVAL may be an expression. | |
50 */ | |
51 | |
52 #ifndef __lwval_h_seen__ | |
53 #define __lwval_h_seen__ | |
54 | |
55 typedef struct lwval LWVAL; | |
56 | |
57 struct lwval_dt_expr | |
58 { | |
59 LWVAL *v1; // first value | |
60 LWVAL *v2; // second value | |
61 int op; // operator | |
62 }; | |
63 | |
64 union lwval_dt | |
65 { | |
66 int lwval_int; // integer type data | |
67 char *lwval_var; // pointer to variable name | |
68 struct lwval_dt_expr expr; // expression | |
69 }; | |
70 | |
71 enum | |
72 { | |
73 LWVAL_TYPE_UNDEF, // undefined | |
74 LWVAL_TYPE_NAN, // not a number | |
75 LWVAL_TYPE_INT, // integer | |
76 LWVAL_TYPE_VAR, // variable (symbol) | |
77 LWVAL_TYPE_EXPR, // expression | |
78 LWVAL_TYPE_ERR // error | |
79 }; | |
80 | |
81 struct lwval | |
82 { | |
83 int lwval_type; // data type | |
84 union lwval_dt dt; // type specific stuff | |
85 }; | |
86 | |
87 #ifndef __lwval_c_seen__ | |
88 #define EE extern | |
89 #else | |
90 #define EE | |
91 #endif | |
92 | |
93 EE LWVAL *lwval_construct_int(int value); | |
94 EE LWVAL *lwval_construct_err(int errno); | |
95 EE LWVAL *lwval_construct_nan(void); | |
96 EE LWVAL *lwval_construct_expr(LWVAL *v1, LWVAL *v2, int op); | |
97 EE LWVAL *lwval_construct_undef(void); | |
98 EE void lwval_clear(LWVAL *value); | |
99 EE void lwval_destroy(LWVAL *value); | |
100 EE void lwval_dup(LWVAL *v1, LWVAL *v2); | |
101 | |
102 // operators - operate on v1 and v2 in order, result goes into | |
103 // v1; return v1 | |
104 EE LWVAL *lwval_add(LWVAL *v1, LWVAL *v2); | |
105 EE LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2); | |
106 EE LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2); | |
107 EE LWVAL *lwval_div(LWVAL *v1, LWVAL *v2); | |
108 EE LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2); | |
109 EE LWVAL *lwval_neg(LWVAL *v1); | |
110 | |
111 #endif //__lwval_h_seen__ |