Mercurial > hg-old > index.cgi
diff src/expr.c @ 15:1f598d89b9b0
Started creating expression parser
author | lost |
---|---|
date | Thu, 23 Oct 2008 03:37:48 +0000 |
parents | b28d7cb60779 |
children | df0c4a46af8f |
line wrap: on
line diff
--- a/src/expr.c Thu Oct 23 02:44:07 2008 +0000 +++ b/src/expr.c Thu Oct 23 03:37:48 2008 +0000 @@ -19,57 +19,88 @@ */ /* -This file contains implementations associated with the expression evaluator -used by LWASM. - +This file contains the actual expression evaluator which uses the LWVAL +mechanism to store results. */ -#include <malloc.h> - #define __expr_c_seen__ + +#include <ctype.h> +#include <stdlib.h> + #include "expr.h" +#include "lwval.h" + +// parse a single term out of the expression; return NULL if +// end of expression; return LWVAL_TYPE_ERR if error +/* +The following is handled by lwasm_parse_term: -LWVAL *lwval_construct_int(int value) +- constants +- parsing a symbol +- unary - +- unary + +- () + +*/ +LWVAL *lwasm_parse_term(char **ptr) { - LWVAL *v; + int sign = 1; + int s = 0; + LWVAL *rval; - v = malloc(sizeof(LWVAL)); - if (!v) - return NULL; +start_term: + if (!**ptr || isspace(**ptr) || **ptr == ')') + return s ? lwval_construct_err(1) : NULL; + + s = 1; + // unary + - NOOP + if (**ptr == '+') + { + (*ptr)++; + goto start_term; + } + + // unary - - applied once the rest of the term is worked out + if (**ptr == '-') + { + (*ptr)++; + sign = -sign; + goto start_term; + } - v -> lwval_type = LWVAL_TYPE_INT; - v -> dt.lwval_int = value; - - return v; + // parens + if (**ptr == '(') + { + LWVAL *v; + (*ptr)++; + rval = lwasm_parse_expr(ptr); + if (**ptr != ')') + { + lwval_destroy(rval); + return lwval_construct_err(1); + } + (*ptr)++; + goto ret; + } + + // parse an actual term here; no more futzing with expressions + +ret: + // apply negation if appropriate + if (sign < 0) + lwval_neg(rval); + return rval; } -void lwval_destroy(LWVAL *value) -{ - if (value) - free(value); -} - -// v1 + v2 -> v3, return v3 -LWVAL *lwval_add(LWVAL *v1, LWVAL *v2, LWVAL *v3) +// parse an expression +LWVAL *lwasm_parse_expr(char **ptr) { } -// v1 - v2 -> v3, return v3 -LWVAL *lwval_sub(LWVAL *v1, LWVAL *v2, LWVAL *v3) -{ -} - -// v1 * v2 -> v3, return v3 -LWVAL *lwval_mul(LWVAL *v1, LWVAL *v2, LWVAL *v3) +// attempt to evaluate/simplify expression +int lwasm_eval_expr(LWVAL *expr) { } -// v1 / v2 -> v3, return v3 -LWVAL *lwval_div(LWVAL *v1, LWVAL *v2, LWVAL *v3) -{ -} -// v1 % v2 -> v3, return v3 -LWVAL *lwval_mod(LWVAL *v1, LWVAL *v2, LWVAL *v3) -{ -}