Mercurial > hg-old > index.cgi
comparison src/expr.c @ 15:1f598d89b9b0
Started creating expression parser
author | lost |
---|---|
date | Thu, 23 Oct 2008 03:37:48 +0000 |
parents | b28d7cb60779 |
children | df0c4a46af8f |
comparison
equal
deleted
inserted
replaced
14:b28d7cb60779 | 15:1f598d89b9b0 |
---|---|
17 You should have received a copy of the GNU General Public License along with | 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/>. | 18 this program. If not, see <http://www.gnu.org/licenses/>. |
19 */ | 19 */ |
20 | 20 |
21 /* | 21 /* |
22 This file contains implementations associated with the expression evaluator | 22 This file contains the actual expression evaluator which uses the LWVAL |
23 used by LWASM. | 23 mechanism to store results. |
24 */ | |
25 | |
26 #define __expr_c_seen__ | |
27 | |
28 #include <ctype.h> | |
29 #include <stdlib.h> | |
30 | |
31 #include "expr.h" | |
32 #include "lwval.h" | |
33 | |
34 // parse a single term out of the expression; return NULL if | |
35 // end of expression; return LWVAL_TYPE_ERR if error | |
36 /* | |
37 The following is handled by lwasm_parse_term: | |
38 | |
39 - constants | |
40 - parsing a symbol | |
41 - unary - | |
42 - unary + | |
43 - () | |
24 | 44 |
25 */ | 45 */ |
46 LWVAL *lwasm_parse_term(char **ptr) | |
47 { | |
48 int sign = 1; | |
49 int s = 0; | |
50 LWVAL *rval; | |
51 | |
52 start_term: | |
53 if (!**ptr || isspace(**ptr) || **ptr == ')') | |
54 return s ? lwval_construct_err(1) : NULL; | |
26 | 55 |
27 #include <malloc.h> | 56 s = 1; |
57 // unary + - NOOP | |
58 if (**ptr == '+') | |
59 { | |
60 (*ptr)++; | |
61 goto start_term; | |
62 } | |
63 | |
64 // unary - - applied once the rest of the term is worked out | |
65 if (**ptr == '-') | |
66 { | |
67 (*ptr)++; | |
68 sign = -sign; | |
69 goto start_term; | |
70 } | |
71 | |
72 // parens | |
73 if (**ptr == '(') | |
74 { | |
75 LWVAL *v; | |
76 (*ptr)++; | |
77 rval = lwasm_parse_expr(ptr); | |
78 if (**ptr != ')') | |
79 { | |
80 lwval_destroy(rval); | |
81 return lwval_construct_err(1); | |
82 } | |
83 (*ptr)++; | |
84 goto ret; | |
85 } | |
28 | 86 |
29 #define __expr_c_seen__ | 87 // parse an actual term here; no more futzing with expressions |
30 #include "expr.h" | |
31 | 88 |
32 LWVAL *lwval_construct_int(int value) | 89 ret: |
33 { | 90 // apply negation if appropriate |
34 LWVAL *v; | 91 if (sign < 0) |
35 | 92 lwval_neg(rval); |
36 v = malloc(sizeof(LWVAL)); | 93 return rval; |
37 if (!v) | |
38 return NULL; | |
39 | |
40 v -> lwval_type = LWVAL_TYPE_INT; | |
41 v -> dt.lwval_int = value; | |
42 | |
43 return v; | |
44 } | 94 } |
45 | 95 |
46 void lwval_destroy(LWVAL *value) | 96 // parse an expression |
47 { | 97 LWVAL *lwasm_parse_expr(char **ptr) |
48 if (value) | |
49 free(value); | |
50 } | |
51 | |
52 // v1 + v2 -> v3, return v3 | |
53 LWVAL *lwval_add(LWVAL *v1, LWVAL *v2, LWVAL *v3) | |
54 { | 98 { |
55 } | 99 } |
56 | 100 |
57 // v1 - v2 -> v3, return v3 | 101 // attempt to evaluate/simplify expression |
58 LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2, LWVAL *v3) | 102 int lwasm_eval_expr(LWVAL *expr) |
59 { | 103 { |
60 } | 104 } |
61 | 105 |
62 // v1 * v2 -> v3, return v3 | |
63 LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2, LWVAL *v3) | |
64 { | |
65 } | |
66 | 106 |
67 // v1 / v2 -> v3, return v3 | |
68 LWVAL *lwval_div(LWVAL *v1, LWVAL *v2, LWVAL *v3) | |
69 { | |
70 } | |
71 | |
72 // v1 % v2 -> v3, return v3 | |
73 LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2, LWVAL *v3) | |
74 { | |
75 } |