comparison src/expr.c @ 39:efa19ec69df9

tweaked debugging system for expression handler
author lost
date Sat, 03 Jan 2009 19:20:44 +0000
parents 538e15927776
children d2cee0c335e7
comparison
equal deleted inserted replaced
38:9bd584bb6296 39:efa19ec69df9
23 */ 23 */
24 24
25 #define __expr_c_seen__ 25 #define __expr_c_seen__
26 26
27 #include <ctype.h> 27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h> 28 #include <stdlib.h>
30 #include <string.h> 29 #include <string.h>
31 30
32 #include "expr.h" 31 #include "expr.h"
33 #include "util.h" 32 #include "util.h"
33
34 // this definition is in lwasm.h but we don't need the whole file here
35 extern void debug_message(int level, const char *fmt, ...);
34 36
35 lwasm_expr_stack_t *lwasm_expr_stack_create(void) 37 lwasm_expr_stack_t *lwasm_expr_stack_create(void)
36 { 38 {
37 lwasm_expr_stack_t *s; 39 lwasm_expr_stack_t *s;
38 40
66 68
67 lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper) 69 lwasm_expr_term_t *lwasm_expr_term_create_oper(int oper)
68 { 70 {
69 lwasm_expr_term_t *t; 71 lwasm_expr_term_t *t;
70 72
71 fprintf(stderr, "Creating operator term: %d\n", oper); 73 debug_message(10, "Creating operator term: %d", oper);
72 74
73 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); 75 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
74 t -> term_type = LWASM_TERM_OPER; 76 t -> term_type = LWASM_TERM_OPER;
75 t -> value = oper; 77 t -> value = oper;
76 return t; 78 return t;
77 } 79 }
78 80
79 lwasm_expr_term_t *lwasm_expr_term_create_int(int val) 81 lwasm_expr_term_t *lwasm_expr_term_create_int(int val)
80 { 82 {
81 lwasm_expr_term_t *t; 83 lwasm_expr_term_t *t;
82 fprintf(stderr, "Creating integer term: %d\n", val); 84 debug_message(10, "Creating integer term: %d", val);
83 85
84 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); 86 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
85 t -> term_type = LWASM_TERM_INT; 87 t -> term_type = LWASM_TERM_INT;
86 t -> value = val; 88 t -> value = val;
87 return t; 89 return t;
89 91
90 lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym) 92 lwasm_expr_term_t *lwasm_expr_term_create_sym(char *sym)
91 { 93 {
92 lwasm_expr_term_t *t; 94 lwasm_expr_term_t *t;
93 95
94 fprintf(stderr, "Creating symbol term: %s\n", sym); 96 debug_message(10, "Creating symbol term: %s", sym);
97
95 t = lwasm_alloc(sizeof(lwasm_expr_term_t)); 98 t = lwasm_alloc(sizeof(lwasm_expr_term_t));
96 t -> term_type = LWASM_TERM_SYM; 99 t -> term_type = LWASM_TERM_SYM;
97 t -> symbol = lwasm_strdup(sym); 100 t -> symbol = lwasm_strdup(sym);
98 return t; 101 return t;
99 } 102 }
110 113
111 case LWASM_TERM_SYM: 114 case LWASM_TERM_SYM:
112 return lwasm_expr_term_create_sym(t -> symbol); 115 return lwasm_expr_term_create_sym(t -> symbol);
113 116
114 default: 117 default:
115 fprintf(stderr, "lwasm_expr_term_dup(): invalid term type %d\n", t -> term_type); 118 debug_message(0, "lwasm_expr_term_dup(): invalid term type %d", t -> term_type);
116 exit(1); 119 exit(1);
117 } 120 }
118 // can't get here 121 // can't get here
119 } 122 }
120 123
122 { 125 {
123 lwasm_expr_stack_node_t *n; 126 lwasm_expr_stack_node_t *n;
124 127
125 if (!s) 128 if (!s)
126 { 129 {
127 fprintf(stderr, "lwasm_expr_stack_push(): invalid stack pointer\n"); 130 debug_message(0, "lwasm_expr_stack_push(): invalid stack pointer");
128 exit(1); 131 exit(1);
129 } 132 }
130 133
131 n = lwasm_alloc(sizeof(lwasm_expr_stack_node_t)); 134 n = lwasm_alloc(sizeof(lwasm_expr_stack_node_t));
132 n -> next = NULL; 135 n -> next = NULL;
179 as well as () 182 as well as ()
180 */ 183 */
181 int lwasm_expr_parse_term(lwasm_expr_stack_t *s, const char **p) 184 int lwasm_expr_parse_term(lwasm_expr_stack_t *s, const char **p)
182 { 185 {
183 lwasm_expr_term_t *t; 186 lwasm_expr_term_t *t;
184 fprintf(stderr, "Expression string %s\n", *p); 187 debug_message(2, "Expression string %s", *p);
188
185 eval_next: 189 eval_next:
186 if (**p == '(') 190 if (**p == '(')
187 { 191 {
188 fprintf(stderr, "Starting paren\n"); 192 debug_message(3, "Starting paren");
189 (*p)++; 193 (*p)++;
190 lwasm_expr_parse_expr(s, p, 0); 194 lwasm_expr_parse_expr(s, p, 0);
191 if (**p != ')') 195 if (**p != ')')
192 return -1; 196 return -1;
193 (*p)++; 197 (*p)++;
194 return 0; 198 return 0;
195 } 199 }
196 200
197 if (**p == '+') 201 if (**p == '+')
198 { 202 {
199 fprintf(stderr, "Unary +\n"); 203 debug_message(3, "Unary +");
200 (*p)++; 204 (*p)++;
201 goto eval_next; 205 goto eval_next;
202 } 206 }
203 207
204 if (**p == '-') 208 if (**p == '-')
428 } 432 }
429 else if (valtype & 4) 433 else if (valtype & 4)
430 { 434 {
431 // otherwise we must be decimal (if we're still allowed one) 435 // otherwise we must be decimal (if we're still allowed one)
432 val = decval; 436 val = decval;
433 fprintf(stderr, "End of decimal value\n"); 437 debug_message(3, "End of decimal value");
434 break; 438 break;
435 } 439 }
436 else 440 else
437 { 441 {
438 // bad value 442 // bad value
497 default: 501 default:
498 // digit 502 // digit
499 dval -= '0'; 503 dval -= '0';
500 if (dval > 9) 504 if (dval > 9)
501 dval -= 7; 505 dval -= 7;
502 fprintf(stderr, "Got digit: %d\n", dval); 506 debug_message(3, "Got digit: %d", dval);
503 // if (dval > 1) 507 // if (dval > 1)
504 // valtype &= 14; 508 // valtype &= 14;
505 // if (dval > 7) 509 // if (dval > 7)
506 // valtype &= 12; 510 // valtype &= 12;
507 // if (dval > 9) 511 // if (dval > 9)
664 // return potentially partial expression 668 // return potentially partial expression
665 if (lwasm_expr_reval(s, sfunc, state) < 0) 669 if (lwasm_expr_reval(s, sfunc, state) < 0)
666 goto cleanup_error; 670 goto cleanup_error;
667 671
668 if (lwasm_expr_is_constant(s)) 672 if (lwasm_expr_is_constant(s))
669 fprintf(stderr, "Constant expression evaluates to: %d\n", lwasm_expr_get_value(s)); 673 debug_message(3, "Constant expression evaluates to: %d", lwasm_expr_get_value(s));
670 674
671 return s; 675 return s;
672 676
673 cleanup_error: 677 cleanup_error:
674 lwasm_expr_stack_free(s); 678 lwasm_expr_stack_free(s);