From 77a53d41a63f06089ecc83c84fccd7c4a42a89af Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Wed, 13 Mar 2019 15:36:46 -0700 Subject: switch to WarpXParser --- Source/Parser/wp_parser_y.c | 1082 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1082 insertions(+) create mode 100644 Source/Parser/wp_parser_y.c (limited to 'Source/Parser/wp_parser_y.c') diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c new file mode 100644 index 000000000..c4815a2a9 --- /dev/null +++ b/Source/Parser/wp_parser_y.c @@ -0,0 +1,1082 @@ +#include +#include +#include +#include +#include +#include "wp_parser_y.h" +#include "wp_parser.tab.h" + +static struct wp_node* wp_root = NULL; + +/* This is called by a bison rule to store the original AST in a + * static variable. Accessing this directly is not thread safe. So + * this will be duplicated later for each thread. + */ +void +wp_defexpr (struct wp_node* body) +{ + wp_root = body; +} + +struct wp_node* +wp_newnumber (double d) +{ + struct wp_number* r = malloc(sizeof(struct wp_number)); + r->type = WP_NUMBER; + r->value = d; + return (struct wp_node*) r; +} + +struct wp_symbol* +wp_makesymbol (char* name) +{ + struct wp_symbol* symbol = malloc(sizeof(struct wp_symbol)); + symbol->type = WP_SYMBOL; + symbol->name = strdup(name); + symbol->pointer = NULL; + return symbol; +} + +struct wp_node* +wp_newsymbol (struct wp_symbol* symbol) +{ + return (struct wp_node*) symbol; +} + +struct wp_node* +wp_newnode (enum wp_node_t type, struct wp_node* l, struct wp_node* r) +{ + struct wp_node* tmp = malloc(sizeof(struct wp_node)); + tmp->type = type; + tmp->l = l; + tmp->r = r; + return tmp; +} + +struct wp_node* +wp_newf1 (enum wp_f1_t ftype, struct wp_node* l) +{ + struct wp_f1* tmp = malloc(sizeof(struct wp_f1)); + tmp->type = WP_F1; + tmp->l = l; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +struct wp_node* +wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, struct wp_node* r) +{ + struct wp_f2* tmp = malloc(sizeof(struct wp_f2)); + tmp->type = WP_F2; + tmp->l = l; + tmp->r = r; + tmp->ftype = ftype; + return (struct wp_node*) tmp; +} + +void +yyerror (char const *s, ...) +{ + va_list vl; + va_start(vl, s); + vfprintf(stderr, s, vl); + fprintf(stderr, "\n"); + va_end(vl); +} + +/*******************************************************************/ + +struct wp_parser* +wp_parser_new (void) +{ + struct wp_parser* my_parser = malloc(sizeof(struct wp_parser)); + + my_parser->sz_mempool = wp_ast_size(wp_root); + my_parser->p_root = malloc(my_parser->sz_mempool); + my_parser->p_free = my_parser->p_root; + + my_parser->ast = wp_parser_ast_dup(my_parser, wp_root,1); /* 1: free the source wp_root */ + + if (my_parser->p_root + my_parser->sz_mempool != my_parser->p_free) { + yyerror("wp_parser_new: error in memory size"); + exit(1); + } + + wp_ast_optimize(my_parser->ast); + + return my_parser; +} + +void +wp_parser_delete (struct wp_parser* parser) +{ + free(parser->p_root); + free(parser); +} + +static size_t +wp_aligned_size (size_t N) +{ + const unsigned int align_size = 16; + size_t x = N + (align_size-1); + x -= x & (align_size-1); + return x; +} + +static +void* +wp_parser_allocate (struct wp_parser* my_parser, size_t N) +{ + void* r = my_parser->p_free; + my_parser->p_free = (char*)r + wp_aligned_size(N); + return r; +} + +struct wp_parser* +wp_parser_dup (struct wp_parser* source) +{ + struct wp_parser* dest = malloc(sizeof(struct wp_parser)); + dest->sz_mempool = source->sz_mempool; + dest->p_root = malloc(dest->sz_mempool); + dest->p_free = dest->p_root; + + dest->ast = wp_parser_ast_dup(dest, source->ast, 0); /* 0: don't free the source */ + + return dest; +} + +static +double +wp_call_f1 (struct wp_f1* f1) +{ + double a = wp_ast_eval(f1->l); + switch (f1->ftype) { + case WP_SQRT: return sqrt(a); + case WP_EXP: return exp(a); + case WP_LOG: return log(a); + case WP_LOG10: return log10(a); + case WP_SIN: return sin(a); + case WP_COS: return cos(a); + case WP_TAN: return tan(a); + case WP_ASIN: return asin(a); + case WP_ACOS: return acos(a); + case WP_ATAN: return atan(a); + case WP_SINH: return sinh(a); + case WP_COSH: return cosh(a); + case WP_TANH: return tanh(a); + case WP_ABS: return fabs(a); + case WP_POW_M3: return 1.0/(a*a*a); + case WP_POW_M2: return 1.0/(a*a); + case WP_POW_M1: return 1.0/a; + case WP_POW_P1: return a; + case WP_POW_P2: return a*a; + case WP_POW_P3: return a*a*a; + default: + yyerror("wp_call_f1: Unknow function %d", f1->ftype); + return 0.0; + } +} + +static +double +wp_call_f2 (struct wp_f2* f2) +{ + double a = wp_ast_eval(f2->l); + double b = wp_ast_eval(f2->r); + switch (f2->ftype) { + case WP_POW: + return pow(a,b); + case WP_GT: + return (a > b) ? 1.0 : 0.0; + case WP_LT: + return (a < b) ? 1.0 : 0.0; + case WP_HEAVISIDE: + return (a < 0.0) ? 0.0 : ((a > 0.0) ? 1.0 : b); + case WP_MIN: + return (a < b) ? a : b; + case WP_MAX: + return (a > b) ? a : b; + default: + yyerror("wp_call_f2: Unknow function %d", f2->ftype); + return 0.0; + } +} + +double +wp_ast_eval (struct wp_node* node) +{ + double result; + + switch (node->type) + { + case WP_NUMBER: + result = ((struct wp_number*)node)->value; + break; + case WP_SYMBOL: + result = *(((struct wp_symbol*)node)->pointer); + break; + case WP_ADD: + result = wp_ast_eval(node->l) + wp_ast_eval(node->r); + break; + case WP_SUB: + result = wp_ast_eval(node->l) - wp_ast_eval(node->r); + break; + case WP_MUL: + result = wp_ast_eval(node->l) * wp_ast_eval(node->r); + break; + case WP_DIV: + result = wp_ast_eval(node->l) / wp_ast_eval(node->r); + break; + case WP_NEG: + result = -wp_ast_eval(node->l); + break; + case WP_F1: + result = wp_call_f1((struct wp_f1*)node); + break; + case WP_F2: + result = wp_call_f2((struct wp_f2*)node); + break; + case WP_ADD_VP: + result = node->lvp.v + *(node->rp); + break; + case WP_ADD_PP: + result = *(node->lvp.p) + *(node->rp); + break; + case WP_SUB_VP: + result = node->lvp.v - *(node->rp); + break; + case WP_SUB_PP: + result = *(node->lvp.p) - *(node->rp); + break; + case WP_MUL_VP: + result = node->lvp.v * *(node->rp); + break; + case WP_MUL_PP: + result = *(node->lvp.p) * *(node->rp); + break; + case WP_DIV_VP: + result = node->lvp.v / *(node->rp); + break; + case WP_DIV_PP: + result = *(node->lvp.p) / *(node->rp); + break; + case WP_NEG_P: + result = -*(node->lvp.p); + break; + default: + yyerror("wp_ast_eval: unknown node type %d\n", node->type); + } + + return result; +} + +size_t +wp_ast_size (struct wp_node* node) +{ + size_t result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_aligned_size(sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_aligned_size(sizeof(struct wp_symbol)) + + wp_aligned_size(strlen(((struct wp_symbol*)node)->name)+1); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_NEG: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + case WP_F1: + result = wp_aligned_size(sizeof(struct wp_f1)) + + wp_ast_size(node->l); + break; + case WP_F2: + result = wp_aligned_size(sizeof(struct wp_f2)) + + wp_ast_size(node->l) + wp_ast_size(node->r); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->r); + break; + case WP_NEG_P: + result = wp_aligned_size(sizeof(struct wp_node)) + + wp_ast_size(node->l); + break; + default: + yyerror("wp_ast_size: unknown node type %d\n", node->type); + exit(1); + } + + return result; +} + +struct wp_node* +wp_parser_ast_dup (struct wp_parser* my_parser, struct wp_node* node, int move) +{ + void* result; + + switch (node->type) + { + case WP_NUMBER: + result = wp_parser_allocate(my_parser, sizeof(struct wp_number)); + memcpy(result, node , sizeof(struct wp_number)); + break; + case WP_SYMBOL: + result = wp_parser_allocate(my_parser, sizeof(struct wp_symbol)); + memcpy(result, node , sizeof(struct wp_symbol)); + ((struct wp_symbol*)result)->name = wp_parser_allocate + (my_parser, strlen(((struct wp_symbol*)node)->name)+1); + strcpy(((struct wp_symbol*)result)->name, + ((struct wp_symbol*)node )->name); + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + ((struct wp_node*)result)->r = NULL; + break; + case WP_F1: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f1)); + memcpy(result, node , sizeof(struct wp_f1)); + ((struct wp_f1*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f1*)node)->l, move); + break; + case WP_F2: + result = wp_parser_allocate(my_parser, sizeof(struct wp_f2)); + memcpy(result, node , sizeof(struct wp_f2)); + ((struct wp_f2*)result)->l = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->l, move); + ((struct wp_f2*)result)->r = wp_parser_ast_dup(my_parser, ((struct wp_f2*)node)->r, move); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->r = wp_parser_ast_dup(my_parser, node->r, move); + break; + case WP_NEG_P: + result = wp_parser_allocate(my_parser, sizeof(struct wp_node)); + memcpy(result, node , sizeof(struct wp_node)); + ((struct wp_node*)result)->l = wp_parser_ast_dup(my_parser, node->l, move); + break; + default: + yyerror("wp_ast_dup: unknown node type %d\n", node->type); + exit(1); + } + if (move) { + /* Note that we only do this on the original AST. We do not + * need to call free for AST stored in wp_parser because the + * memory is not allocated with malloc directly. + */ + if (node->type == WP_SYMBOL) { + free(((struct wp_symbol*)node)->name); + } + free((void*)node); + } + return (struct wp_node*)result; +} + +#define WP_MOVEUP_R(node, v) \ + struct wp_node* n = node->r->r; \ + double* p = node->r->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_MOVEUP_L(node, v) \ + struct wp_node* n = node->l->r; \ + double* p = node->l->rp; \ + node->r = n; \ + node->lvp.v = v; \ + node->rp = p; +#define WP_EVAL_R(node) node->r->lvp.v +#define WP_EVAL_L(node) node->l->lvp.v + +#define WP_NEG_MOVEUP(node) \ + node->r = node->l->r; \ + node->lvp.v = -node->l->lvp.v; \ + node->rp = node->l->rp; + +void +wp_ast_optimize (struct wp_node* node) +{ + /* No need to free memory because we only call this on ASTs in + * wp_parser that are allocated from the memory pool. + */ + switch (node->type) + { + case WP_NUMBER: + case WP_SYMBOL: + break; + case WP_ADD: + case WP_ADD_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_ADD_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_SUB: + case WP_SUB_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = -wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_SUB_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_ADD_VP) + { + double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SUB_VP) + { + double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_ADD_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_SUB_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_SUB_VP; + } + break; + case WP_MUL: + case WP_MUL_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_MUL_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_DIV: + case WP_DIV_PP: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_SYMBOL) + { + node->lvp.v = wp_ast_eval(node->l); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_NUMBER) + { + node->lvp.v = 1./wp_ast_eval(node->r); + node->rp = ((struct wp_symbol*)(node->l))->pointer; + node->r = node->l; + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_SYMBOL && + node->r->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + node->type = WP_DIV_PP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_MUL_VP) + { + double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_DIV_VP; + } + else if (node->l->type == WP_NUMBER && + node->r->type == WP_DIV_VP) + { + double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + WP_MOVEUP_R(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_MUL_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP && + node->r->type == WP_NUMBER) + { + double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + WP_MOVEUP_L(node, v); + node->type = WP_DIV_VP; + } + break; + case WP_NEG: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->l->type == WP_SYMBOL) + { + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->type = WP_NEG_P; + } + else if (node->l->type == WP_ADD_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_SUB_VP; + } + else if (node->l->type == WP_SUB_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_ADD_VP; + } + else if (node->l->type == WP_MUL_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_MUL_VP; + } + else if (node->l->type == WP_DIV_VP) + { + WP_NEG_MOVEUP(node); + node->type = WP_DIV_VP; + } + break; + case WP_F1: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_F2: + wp_ast_optimize(node->l); + wp_ast_optimize(node->r); + if (node->l->type == WP_NUMBER && + node->r->type == WP_NUMBER) + { + double v = wp_ast_eval(node); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + else if (node->r->type == WP_NUMBER && ((struct wp_f2*)node)->ftype == WP_POW) + { + struct wp_node* n = node->l; + double v = wp_ast_eval(node->r); + if (-3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M3; + } else if (-2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M2; + } else if (-1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_M1; + } else if (0.0 == v) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = 1.0; + } else if (1.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P1; + } else if (2.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P2; + } else if (3.0 == v) { + ((struct wp_f1*)node)->type = WP_F1; + ((struct wp_f1*)node)->l = n; + ((struct wp_f1*)node)->ftype = WP_POW_P3; + } + } + break; + case WP_ADD_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v + wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_SUB_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v - wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_MUL_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v * wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_DIV_VP: + wp_ast_optimize(node->r); + if (node->r->type == WP_NUMBER) + { + double v = node->lvp.v / wp_ast_eval(node->r); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + case WP_NEG_P: + wp_ast_optimize(node->l); + if (node->l->type == WP_NUMBER) + { + double v = -wp_ast_eval(node->l); + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = v; + } + break; + default: + yyerror("wp_ast_optimize: unknown node type %d\n", node->type); + exit(1); + } +} + +static +void +wp_ast_print_f1 (struct wp_f1* f1) +{ + wp_ast_print(f1->l); + switch (f1->ftype) { + case WP_SQRT: printf("SQRT\n"); break; + case WP_EXP: printf("EXP\n"); break; + case WP_LOG: printf("LOG\n"); break; + case WP_LOG10: printf("LOG10\n"); break; + case WP_SIN: printf("SIN\n"); break; + case WP_COS: printf("COS\n"); break; + case WP_TAN: printf("TAN\n"); break; + case WP_ASIN: printf("ASIN\n"); break; + case WP_ACOS: printf("ACOS\n"); break; + case WP_ATAN: printf("ATAN\n"); break; + case WP_SINH: printf("SINH\n"); break; + case WP_COSH: printf("COSH\n"); break; + case WP_TANH: printf("TANH\n"); break; + case WP_ABS: printf("ABS\n"); break; + case WP_POW_M3: printf("POW(,-3)\n"); break; + case WP_POW_M2: printf("POW(,-2)\n"); break; + case WP_POW_M1: printf("POW(,-1)\n"); break; + case WP_POW_P1: printf("POW(,1)\n"); break; + case WP_POW_P2: printf("POW(,2)\n"); break; + case WP_POW_P3: printf("POW(,3)\n"); break; + default: + yyerror("wp_ast+print_f1: Unknow function %d", f1->ftype); + } +} + +static +void +wp_ast_print_f2 (struct wp_f2* f2) +{ + wp_ast_print(f2->l); + wp_ast_print(f2->r); + switch (f2->ftype) { + case WP_POW: + printf("POW\n"); + break; + case WP_GT: + printf("GT\n"); + break; + case WP_LT: + printf("LT\n"); + break; + case WP_HEAVISIDE: + printf("HEAVISIDE\n"); + break; + case WP_MIN: + printf("MIN\n"); + break; + case WP_MAX: + printf("MAX\n"); + break; + default: + yyerror("wp_ast_print_f2: Unknow function %d", f2->ftype); + } +} + +void +wp_ast_print (struct wp_node* node) +{ + switch (node->type) + { + case WP_NUMBER: + printf("NUMBER: %.17g\n", wp_ast_eval(node)); + break; + case WP_SYMBOL: + printf("VARIABLE: %s\n", ((struct wp_symbol*)node)->name); + break; + case WP_ADD: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("ADD\n"); + break; + case WP_SUB: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("SUB\n"); + break; + case WP_MUL: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("MUL\n"); + break; + case WP_DIV: + wp_ast_print(node->l); + wp_ast_print(node->r); + printf("DIV\n"); + break; + case WP_NEG: + wp_ast_print(node->l); + printf("NEG\n"); + break; + case WP_F1: + wp_ast_print_f1((struct wp_f1*)node); + break; + case WP_F2: + wp_ast_print_f2((struct wp_f2*)node); + break; + case WP_ADD_VP: + printf("ADD: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_VP: + printf("SUM: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_VP: + printf("MUL: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_VP: + printf("DIV: %.17g %s\n", node->lvp.v, ((struct wp_symbol*)(node->r))->name); + break; + case WP_NEG_P: + printf("NEG: %s\n", ((struct wp_symbol*)(node->l))->name); + break; + case WP_ADD_PP: + printf("ADD: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_SUB_PP: + printf("SUB: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_MUL_PP: + printf("MUL: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + case WP_DIV_PP: + printf("DIV: %s %s\n", ((struct wp_symbol*)(node->l))->name, + ((struct wp_symbol*)(node->r))->name); + break; + default: + yyerror("wp_ast_print: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_ast_regvar (struct wp_node* node, char const* name, double* p) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_symbol*)node)->pointer = p; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_NEG: + wp_ast_regvar(node->l, name, p); + break; + case WP_F1: + wp_ast_regvar(node->l, name, p); + break; + case WP_F2: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_regvar(node->r, name, p); + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + case WP_NEG_P: + wp_ast_regvar(node->l, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_regvar(node->l, name, p); + wp_ast_regvar(node->r, name, p); + node->lvp.p = ((struct wp_symbol*)(node->l))->pointer; + node->rp = ((struct wp_symbol*)(node->r))->pointer; + break; + default: + yyerror("wp_ast_regvar: unknown node type %d\n", node->type); + exit(1); + } +} + +void wp_ast_setconst (struct wp_node* node, char const* name, double c) +{ + switch (node->type) + { + case WP_NUMBER: + break; + case WP_SYMBOL: + if (strcmp(name, ((struct wp_symbol*)node)->name) == 0) { + ((struct wp_number*)node)->type = WP_NUMBER; + ((struct wp_number*)node)->value = c; + } + break; + case WP_ADD: + case WP_SUB: + case WP_MUL: + case WP_DIV: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG: + wp_ast_setconst(node->l, name, c); + break; + case WP_F1: + wp_ast_setconst(node->l, name, c); + break; + case WP_F2: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + case WP_ADD_VP: + case WP_SUB_VP: + case WP_MUL_VP: + case WP_DIV_VP: + wp_ast_setconst(node->r, name, c); + break; + case WP_NEG_P: + wp_ast_setconst(node->l, name, c); + break; + case WP_ADD_PP: + case WP_SUB_PP: + case WP_MUL_PP: + case WP_DIV_PP: + wp_ast_setconst(node->l, name, c); + wp_ast_setconst(node->r, name, c); + break; + default: + yyerror("wp_ast_setconst: unknown node type %d\n", node->type); + exit(1); + } +} + +void +wp_parser_regvar (struct wp_parser* parser, char const* name, double* p) +{ + wp_ast_regvar(parser->ast, name, p); +} + +void +wp_parser_setconst (struct wp_parser* parser, char const* name, double c) +{ + wp_ast_setconst(parser->ast, name, c); + wp_ast_optimize(parser->ast); +} + -- cgit v1.2.3 From 4844eb55a7c1dca03f15d07ff22a34e3ab3d134c Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Sat, 16 Mar 2019 11:22:19 -0700 Subject: add unary plus sign and fix a bug --- Source/Parser/wp_parser.tab.c | 141 ++++++++++++++++++++++-------------------- Source/Parser/wp_parser.tab.h | 5 +- Source/Parser/wp_parser.y | 3 +- Source/Parser/wp_parser_y.c | 14 +++-- 4 files changed, 88 insertions(+), 75 deletions(-) (limited to 'Source/Parser/wp_parser_y.c') diff --git a/Source/Parser/wp_parser.tab.c b/Source/Parser/wp_parser.tab.c index fbc6920c2..c0cf2fad8 100644 --- a/Source/Parser/wp_parser.tab.c +++ b/Source/Parser/wp_parser.tab.c @@ -112,7 +112,8 @@ extern int yydebug; F2 = 262, EOL = 263, POW = 264, - NEG = 265 + NEG = 265, + UPLUS = 266 }; #endif @@ -129,7 +130,7 @@ union YYSTYPE enum wp_f1_t f1; enum wp_f2_t f2; -#line 133 "wp_parser.tab.c" /* yacc.c:355 */ +#line 134 "wp_parser.tab.c" /* yacc.c:355 */ }; typedef union YYSTYPE YYSTYPE; @@ -146,7 +147,7 @@ int yyparse (void); /* Copy the second part of user declarations. */ -#line 150 "wp_parser.tab.c" /* yacc.c:358 */ +#line 151 "wp_parser.tab.c" /* yacc.c:358 */ #ifdef short # undef short @@ -388,21 +389,21 @@ union yyalloc /* YYFINAL -- State number of the termination state. */ #define YYFINAL 2 /* YYLAST -- Last index in YYTABLE. */ -#define YYLAST 97 +#define YYLAST 96 /* YYNTOKENS -- Number of terminals. */ -#define YYNTOKENS 22 +#define YYNTOKENS 23 /* YYNNTS -- Number of nonterminals. */ #define YYNNTS 3 /* YYNRULES -- Number of rules. */ -#define YYNRULES 16 +#define YYNRULES 17 /* YYNSTATES -- Number of states. */ -#define YYNSTATES 36 +#define YYNSTATES 38 /* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned by yylex, with out-of-bounds checking. */ #define YYUNDEFTOK 2 -#define YYMAXUTOK 265 +#define YYMAXUTOK 266 #define YYTRANSLATE(YYX) \ ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) @@ -415,7 +416,7 @@ static const yytype_uint8 yytranslate[] = 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 19, 20, 14, 12, 21, 13, 2, 15, 2, 2, + 20, 21, 14, 12, 22, 13, 2, 15, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 16, 11, 17, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -437,7 +438,7 @@ static const yytype_uint8 yytranslate[] = 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 18 + 5, 6, 7, 8, 9, 18, 19 }; #if YYDEBUG @@ -445,7 +446,7 @@ static const yytype_uint8 yytranslate[] = static const yytype_uint8 yyrline[] = { 0, 58, 58, 59, 68, 69, 70, 71, 72, 73, - 74, 75, 76, 77, 78, 79, 80 + 74, 75, 76, 77, 78, 79, 80, 81 }; #endif @@ -456,7 +457,7 @@ static const char *const yytname[] = { "$end", "error", "$undefined", "NODE", "NUMBER", "SYMBOL", "F1", "F2", "EOL", "\"**\"", "'^'", "'='", "'+'", "'-'", "'*'", "'/'", "'<'", "'>'", - "NEG", "'('", "')'", "','", "$accept", "input", "exp", YY_NULLPTR + "NEG", "UPLUS", "'('", "')'", "','", "$accept", "input", "exp", YY_NULLPTR }; #endif @@ -466,8 +467,8 @@ static const char *const yytname[] = static const yytype_uint16 yytoknum[] = { 0, 256, 257, 258, 259, 260, 261, 262, 263, 264, - 94, 61, 43, 45, 42, 47, 60, 62, 265, 40, - 41, 44 + 94, 61, 43, 45, 42, 47, 60, 62, 265, 266, + 40, 41, 44 }; # endif @@ -485,10 +486,10 @@ static const yytype_uint16 yytoknum[] = STATE-NUM. */ static const yytype_int8 yypact[] = { - -18, 15, -18, -18, -18, -17, -14, 25, 25, 71, - 25, 25, -3, 43, -18, 25, 25, 25, 25, 25, - 25, 25, 52, 33, -18, -3, 80, 80, 7, 7, - -3, -3, -18, 25, 61, -18 + -18, 17, -18, -18, -18, -17, -14, 27, 27, 27, + 70, 27, 27, -2, -2, 40, -18, 27, 27, 27, + 27, 27, 27, 27, 50, 29, -18, -2, 79, 79, + 9, 9, -2, -2, -18, 27, 60, -18 }; /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. @@ -497,9 +498,9 @@ static const yytype_int8 yypact[] = static const yytype_uint8 yydefact[] = { 2, 0, 1, 4, 5, 0, 0, 0, 0, 0, - 0, 0, 13, 0, 3, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 10, 14, 6, 7, 8, 9, - 11, 12, 15, 0, 0, 16 + 0, 0, 0, 14, 13, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 10, 15, 6, 7, + 8, 9, 11, 12, 16, 0, 0, 17 }; /* YYPGOTO[NTERM-NUM]. */ @@ -511,7 +512,7 @@ static const yytype_int8 yypgoto[] = /* YYDEFGOTO[NTERM-NUM]. */ static const yytype_int8 yydefgoto[] = { - -1, 1, 9 + -1, 1, 10 }; /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If @@ -519,54 +520,54 @@ static const yytype_int8 yydefgoto[] = number is the opposite. If YYTABLE_NINF, syntax error. */ static const yytype_uint8 yytable[] = { - 12, 13, 10, 22, 23, 11, 15, 0, 25, 26, - 27, 28, 29, 30, 31, 2, 15, 0, 0, 3, - 4, 5, 6, 20, 21, 0, 34, 0, 7, 3, - 4, 5, 6, 0, 8, 0, 0, 0, 7, 0, - 0, 0, 15, 0, 8, 16, 17, 18, 19, 20, - 21, 0, 15, 0, 33, 16, 17, 18, 19, 20, - 21, 15, 0, 24, 16, 17, 18, 19, 20, 21, - 15, 0, 32, 16, 17, 18, 19, 20, 21, 14, - 15, 35, 0, 16, 17, 18, 19, 20, 21, 15, - 0, 0, 0, 0, 18, 19, 20, 21 + 13, 14, 15, 11, 24, 25, 12, 17, 0, 0, + 27, 28, 29, 30, 31, 32, 33, 2, 17, 0, + 0, 3, 4, 5, 6, 22, 23, 0, 36, 7, + 8, 3, 4, 5, 6, 0, 0, 9, 17, 7, + 8, 18, 19, 20, 21, 22, 23, 9, 0, 17, + 0, 35, 18, 19, 20, 21, 22, 23, 0, 17, + 0, 26, 18, 19, 20, 21, 22, 23, 0, 17, + 0, 34, 18, 19, 20, 21, 22, 23, 16, 17, + 0, 37, 18, 19, 20, 21, 22, 23, 17, 0, + 0, 0, 0, 20, 21, 22, 23 }; static const yytype_int8 yycheck[] = { - 7, 8, 19, 10, 11, 19, 9, -1, 15, 16, - 17, 18, 19, 20, 21, 0, 9, -1, -1, 4, - 5, 6, 7, 16, 17, -1, 33, -1, 13, 4, - 5, 6, 7, -1, 19, -1, -1, -1, 13, -1, - -1, -1, 9, -1, 19, 12, 13, 14, 15, 16, - 17, -1, 9, -1, 21, 12, 13, 14, 15, 16, - 17, 9, -1, 20, 12, 13, 14, 15, 16, 17, - 9, -1, 20, 12, 13, 14, 15, 16, 17, 8, - 9, 20, -1, 12, 13, 14, 15, 16, 17, 9, - -1, -1, -1, -1, 14, 15, 16, 17 + 7, 8, 9, 20, 11, 12, 20, 9, -1, -1, + 17, 18, 19, 20, 21, 22, 23, 0, 9, -1, + -1, 4, 5, 6, 7, 16, 17, -1, 35, 12, + 13, 4, 5, 6, 7, -1, -1, 20, 9, 12, + 13, 12, 13, 14, 15, 16, 17, 20, -1, 9, + -1, 22, 12, 13, 14, 15, 16, 17, -1, 9, + -1, 21, 12, 13, 14, 15, 16, 17, -1, 9, + -1, 21, 12, 13, 14, 15, 16, 17, 8, 9, + -1, 21, 12, 13, 14, 15, 16, 17, 9, -1, + -1, -1, -1, 14, 15, 16, 17 }; /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing symbol of state STATE-NUM. */ static const yytype_uint8 yystos[] = { - 0, 23, 0, 4, 5, 6, 7, 13, 19, 24, - 19, 19, 24, 24, 8, 9, 12, 13, 14, 15, - 16, 17, 24, 24, 20, 24, 24, 24, 24, 24, - 24, 24, 20, 21, 24, 20 + 0, 24, 0, 4, 5, 6, 7, 12, 13, 20, + 25, 20, 20, 25, 25, 25, 8, 9, 12, 13, + 14, 15, 16, 17, 25, 25, 21, 25, 25, 25, + 25, 25, 25, 25, 21, 22, 25, 21 }; /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ static const yytype_uint8 yyr1[] = { - 0, 22, 23, 23, 24, 24, 24, 24, 24, 24, - 24, 24, 24, 24, 24, 24, 24 + 0, 23, 24, 24, 25, 25, 25, 25, 25, 25, + 25, 25, 25, 25, 25, 25, 25, 25 }; /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ static const yytype_uint8 yyr2[] = { 0, 2, 0, 3, 1, 1, 3, 3, 3, 3, - 3, 3, 3, 2, 3, 4, 6 + 3, 3, 3, 2, 2, 3, 4, 6 }; @@ -1247,89 +1248,95 @@ yyreduce: { wp_defexpr((yyvsp[-1].n)); } -#line 1251 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1252 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 4: #line 68 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnumber((yyvsp[0].d)); } -#line 1257 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1258 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 5: #line 69 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newsymbol((yyvsp[0].s)); } -#line 1263 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1264 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 6: #line 70 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnode(WP_ADD, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1269 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1270 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 7: #line 71 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnode(WP_SUB, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1275 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1276 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 8: #line 72 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnode(WP_MUL, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1281 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1282 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 9: #line 73 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnode(WP_DIV, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1287 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1288 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 10: #line 74 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = (yyvsp[-1].n); } -#line 1293 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1294 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 11: #line 75 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newf2(WP_LT, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1299 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1300 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 12: #line 76 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newf2(WP_GT, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1305 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1306 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 13: #line 77 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newnode(WP_NEG, (yyvsp[0].n), NULL); } -#line 1311 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1312 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 14: #line 78 "wp_parser.y" /* yacc.c:1646 */ - { (yyval.n) = wp_newf2(WP_POW, (yyvsp[-2].n), (yyvsp[0].n)); } -#line 1317 "wp_parser.tab.c" /* yacc.c:1646 */ + { (yyval.n) = (yyvsp[0].n); } +#line 1318 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 15: #line 79 "wp_parser.y" /* yacc.c:1646 */ - { (yyval.n) = wp_newf1((yyvsp[-3].f1), (yyvsp[-1].n)); } -#line 1323 "wp_parser.tab.c" /* yacc.c:1646 */ + { (yyval.n) = wp_newf2(WP_POW, (yyvsp[-2].n), (yyvsp[0].n)); } +#line 1324 "wp_parser.tab.c" /* yacc.c:1646 */ break; case 16: #line 80 "wp_parser.y" /* yacc.c:1646 */ + { (yyval.n) = wp_newf1((yyvsp[-3].f1), (yyvsp[-1].n)); } +#line 1330 "wp_parser.tab.c" /* yacc.c:1646 */ + break; + + case 17: +#line 81 "wp_parser.y" /* yacc.c:1646 */ { (yyval.n) = wp_newf2((yyvsp[-5].f2), (yyvsp[-3].n), (yyvsp[-1].n)); } -#line 1329 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1336 "wp_parser.tab.c" /* yacc.c:1646 */ break; -#line 1333 "wp_parser.tab.c" /* yacc.c:1646 */ +#line 1340 "wp_parser.tab.c" /* yacc.c:1646 */ default: break; } /* User semantic actions sometimes alter yychar, and that requires @@ -1557,5 +1564,5 @@ yyreturn: #endif return yyresult; } -#line 83 "wp_parser.y" /* yacc.c:1906 */ +#line 84 "wp_parser.y" /* yacc.c:1906 */ diff --git a/Source/Parser/wp_parser.tab.h b/Source/Parser/wp_parser.tab.h index add7999ce..f36b4b611 100644 --- a/Source/Parser/wp_parser.tab.h +++ b/Source/Parser/wp_parser.tab.h @@ -52,7 +52,8 @@ extern int yydebug; F2 = 262, EOL = 263, POW = 264, - NEG = 265 + NEG = 265, + UPLUS = 266 }; #endif @@ -69,7 +70,7 @@ union YYSTYPE enum wp_f1_t f1; enum wp_f2_t f2; -#line 73 "wp_parser.tab.h" /* yacc.c:1909 */ +#line 74 "wp_parser.tab.h" /* yacc.c:1909 */ }; typedef union YYSTYPE YYSTYPE; diff --git a/Source/Parser/wp_parser.y b/Source/Parser/wp_parser.y index e3a007949..3081125cc 100644 --- a/Source/Parser/wp_parser.y +++ b/Source/Parser/wp_parser.y @@ -38,7 +38,7 @@ %left '+' '-' %left '*' '/' %left '<' '>' -%nonassoc NEG +%nonassoc NEG UPLUS %right POW /* This specifies the type of `exp` (i.e., struct wp_node*). Rules @@ -75,6 +75,7 @@ exp: | exp '<' exp { $$ = wp_newf2(WP_LT, $1, $3); } | exp '>' exp { $$ = wp_newf2(WP_GT, $1, $3); } | '-'exp %prec NEG { $$ = wp_newnode(WP_NEG, $2, NULL); } +| '+'exp %prec UPLUS { $$ = $2; } | exp POW exp { $$ = wp_newf2(WP_POW, $1, $3); } | F1 '(' exp ')' { $$ = wp_newf1($1, $3); } | F2 '(' exp ',' exp ')' { $$ = wp_newf2($1, $3, $5); } diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c index c4815a2a9..21b6cc96e 100644 --- a/Source/Parser/wp_parser_y.c +++ b/Source/Parser/wp_parser_y.c @@ -443,7 +443,8 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = ((struct wp_number*)(node->l))->value + + ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -505,7 +506,8 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = ((struct wp_number*)(node->l))->value + - ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -567,7 +569,8 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = ((struct wp_number*)(node->l))->value + * ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -629,7 +632,8 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = ((struct wp_number*)(node->l))->value + / ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -688,7 +692,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->l); if (node->l->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = -((struct wp_number*)(node->l))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } -- cgit v1.2.3 From 109e41ccb6edb85016b899943d64212430634208 Mon Sep 17 00:00:00 2001 From: Weiqun Zhang Date: Sat, 16 Mar 2019 17:24:47 -0700 Subject: inline parser's eval function --- Source/Parser/wp_parser_c.h | 76 +++++++++++++++++++++ Source/Parser/wp_parser_y.c | 156 +++++++++++++------------------------------- Source/Parser/wp_parser_y.h | 4 +- 3 files changed, 123 insertions(+), 113 deletions(-) (limited to 'Source/Parser/wp_parser_y.c') diff --git a/Source/Parser/wp_parser_c.h b/Source/Parser/wp_parser_c.h index 1c55433a0..91833b3b8 100644 --- a/Source/Parser/wp_parser_c.h +++ b/Source/Parser/wp_parser_c.h @@ -13,4 +13,80 @@ extern "C" { } #endif +#ifdef __cplusplus + +inline +double +wp_ast_eval (struct wp_node* node) +{ + double result; + + switch (node->type) + { + case WP_NUMBER: + result = ((struct wp_number*)node)->value; + break; + case WP_SYMBOL: + result = *(((struct wp_symbol*)node)->pointer); + break; + case WP_ADD: + result = wp_ast_eval(node->l) + wp_ast_eval(node->r); + break; + case WP_SUB: + result = wp_ast_eval(node->l) - wp_ast_eval(node->r); + break; + case WP_MUL: + result = wp_ast_eval(node->l) * wp_ast_eval(node->r); + break; + case WP_DIV: + result = wp_ast_eval(node->l) / wp_ast_eval(node->r); + break; + case WP_NEG: + result = -wp_ast_eval(node->l); + break; + case WP_F1: + result = wp_call_f1(((struct wp_f1*)node)->ftype, + wp_ast_eval(((struct wp_f1*)node)->l)); + break; + case WP_F2: + result = wp_call_f2(((struct wp_f2*)node)->ftype, + wp_ast_eval(((struct wp_f2*)node)->l), + wp_ast_eval(((struct wp_f2*)node)->r)); + break; + case WP_ADD_VP: + result = node->lvp.v + *(node->rp); + break; + case WP_ADD_PP: + result = *(node->lvp.p) + *(node->rp); + break; + case WP_SUB_VP: + result = node->lvp.v - *(node->rp); + break; + case WP_SUB_PP: + result = *(node->lvp.p) - *(node->rp); + break; + case WP_MUL_VP: + result = node->lvp.v * *(node->rp); + break; + case WP_MUL_PP: + result = *(node->lvp.p) * *(node->rp); + break; + case WP_DIV_VP: + result = node->lvp.v / *(node->rp); + break; + case WP_DIV_PP: + result = *(node->lvp.p) / *(node->rp); + break; + case WP_NEG_P: + result = -*(node->lvp.p); + break; + default: + yyerror("wp_ast_eval: unknown node type %d\n", node->type); + } + + return result; +} + +#endif + #endif diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c index 21b6cc96e..ac752a948 100644 --- a/Source/Parser/wp_parser_y.c +++ b/Source/Parser/wp_parser_y.c @@ -145,12 +145,10 @@ wp_parser_dup (struct wp_parser* source) return dest; } -static double -wp_call_f1 (struct wp_f1* f1) +wp_call_f1 (enum wp_f1_t type, double a) { - double a = wp_ast_eval(f1->l); - switch (f1->ftype) { + switch (type) { case WP_SQRT: return sqrt(a); case WP_EXP: return exp(a); case WP_LOG: return log(a); @@ -172,18 +170,15 @@ wp_call_f1 (struct wp_f1* f1) case WP_POW_P2: return a*a; case WP_POW_P3: return a*a*a; default: - yyerror("wp_call_f1: Unknow function %d", f1->ftype); + yyerror("wp_call_f1: Unknow function %d", type); return 0.0; } } -static double -wp_call_f2 (struct wp_f2* f2) +wp_call_f2 (enum wp_f2_t type, double a, double b) { - double a = wp_ast_eval(f2->l); - double b = wp_ast_eval(f2->r); - switch (f2->ftype) { + switch (type) { case WP_POW: return pow(a,b); case WP_GT: @@ -197,79 +192,11 @@ wp_call_f2 (struct wp_f2* f2) case WP_MAX: return (a > b) ? a : b; default: - yyerror("wp_call_f2: Unknow function %d", f2->ftype); + yyerror("wp_call_f2: Unknow function %d", type); return 0.0; } } -double -wp_ast_eval (struct wp_node* node) -{ - double result; - - switch (node->type) - { - case WP_NUMBER: - result = ((struct wp_number*)node)->value; - break; - case WP_SYMBOL: - result = *(((struct wp_symbol*)node)->pointer); - break; - case WP_ADD: - result = wp_ast_eval(node->l) + wp_ast_eval(node->r); - break; - case WP_SUB: - result = wp_ast_eval(node->l) - wp_ast_eval(node->r); - break; - case WP_MUL: - result = wp_ast_eval(node->l) * wp_ast_eval(node->r); - break; - case WP_DIV: - result = wp_ast_eval(node->l) / wp_ast_eval(node->r); - break; - case WP_NEG: - result = -wp_ast_eval(node->l); - break; - case WP_F1: - result = wp_call_f1((struct wp_f1*)node); - break; - case WP_F2: - result = wp_call_f2((struct wp_f2*)node); - break; - case WP_ADD_VP: - result = node->lvp.v + *(node->rp); - break; - case WP_ADD_PP: - result = *(node->lvp.p) + *(node->rp); - break; - case WP_SUB_VP: - result = node->lvp.v - *(node->rp); - break; - case WP_SUB_PP: - result = *(node->lvp.p) - *(node->rp); - break; - case WP_MUL_VP: - result = node->lvp.v * *(node->rp); - break; - case WP_MUL_PP: - result = *(node->lvp.p) * *(node->rp); - break; - case WP_DIV_VP: - result = node->lvp.v / *(node->rp); - break; - case WP_DIV_PP: - result = *(node->lvp.p) / *(node->rp); - break; - case WP_NEG_P: - result = -*(node->lvp.p); - break; - default: - yyerror("wp_ast_eval: unknown node type %d\n", node->type); - } - - return result; -} - size_t wp_ast_size (struct wp_node* node) { @@ -451,14 +378,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_ADD_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = wp_ast_eval(node->r); + node->lvp.v = ((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_ADD_VP; @@ -473,28 +400,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_ADD_VP) { - double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_SUB_VP) { - double v = wp_ast_eval(node->l) + WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value + WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_SUB_VP; } else if (node->l->type == WP_ADD_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_SUB_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) + wp_ast_eval(node->r); + double v = WP_EVAL_L(node) + ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_SUB_VP; } @@ -514,14 +441,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_SUB_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = -wp_ast_eval(node->r); + node->lvp.v = -((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_ADD_VP; @@ -536,28 +463,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_ADD_VP) { - double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_SUB_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_SUB_VP) { - double v = wp_ast_eval(node->l) - WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value - WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_ADD_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_ADD_VP; } else if (node->l->type == WP_SUB_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) - wp_ast_eval(node->r); + double v = WP_EVAL_L(node) - ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_SUB_VP; } @@ -577,14 +504,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_MUL_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = wp_ast_eval(node->r); + node->lvp.v = ((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_MUL_VP; @@ -599,28 +526,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_MUL_VP) { - double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_DIV_VP) { - double v = wp_ast_eval(node->l) * WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value * WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_DIV_VP; } else if (node->l->type == WP_MUL_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_DIV_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) * wp_ast_eval(node->r); + double v = WP_EVAL_L(node) * ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_DIV_VP; } @@ -640,14 +567,14 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_SYMBOL) { - node->lvp.v = wp_ast_eval(node->l); + node->lvp.v = ((struct wp_number*)(node->l))->value; node->rp = ((struct wp_symbol*)(node->r))->pointer; node->type = WP_DIV_VP; } else if (node->l->type == WP_SYMBOL && node->r->type == WP_NUMBER) { - node->lvp.v = 1./wp_ast_eval(node->r); + node->lvp.v = 1./((struct wp_number*)(node->r))->value; node->rp = ((struct wp_symbol*)(node->l))->pointer; node->r = node->l; node->type = WP_MUL_VP; @@ -662,28 +589,28 @@ wp_ast_optimize (struct wp_node* node) else if (node->l->type == WP_NUMBER && node->r->type == WP_MUL_VP) { - double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_DIV_VP; } else if (node->l->type == WP_NUMBER && node->r->type == WP_DIV_VP) { - double v = wp_ast_eval(node->l) / WP_EVAL_R(node); + double v = ((struct wp_number*)(node->l))->value / WP_EVAL_R(node); WP_MOVEUP_R(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_MUL_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_MUL_VP; } else if (node->l->type == WP_DIV_VP && node->r->type == WP_NUMBER) { - double v = WP_EVAL_L(node) / wp_ast_eval(node->r); + double v = WP_EVAL_L(node) / ((struct wp_number*)(node->r))->value; WP_MOVEUP_L(node, v); node->type = WP_DIV_VP; } @@ -726,7 +653,9 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->l); if (node->l->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = wp_call_f1 + (((struct wp_f1*)node)->ftype, + ((struct wp_number*)(((struct wp_f1*)node)->l))->value); ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -737,14 +666,17 @@ wp_ast_optimize (struct wp_node* node) if (node->l->type == WP_NUMBER && node->r->type == WP_NUMBER) { - double v = wp_ast_eval(node); + double v = wp_call_f2 + (((struct wp_f2*)node)->ftype, + ((struct wp_number*)(((struct wp_f2*)node)->l))->value, + ((struct wp_number*)(((struct wp_f2*)node)->r))->value); ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } else if (node->r->type == WP_NUMBER && ((struct wp_f2*)node)->ftype == WP_POW) { struct wp_node* n = node->l; - double v = wp_ast_eval(node->r); + double v = ((struct wp_number*)(node->r))->value; if (-3.0 == v) { ((struct wp_f1*)node)->type = WP_F1; ((struct wp_f1*)node)->l = n; @@ -779,7 +711,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v + wp_ast_eval(node->r); + double v = node->lvp.v + ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -788,7 +720,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v - wp_ast_eval(node->r); + double v = node->lvp.v - ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -797,7 +729,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v * wp_ast_eval(node->r); + double v = node->lvp.v * ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -806,7 +738,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->r); if (node->r->type == WP_NUMBER) { - double v = node->lvp.v / wp_ast_eval(node->r); + double v = node->lvp.v / ((struct wp_number*)(node->r))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -815,7 +747,7 @@ wp_ast_optimize (struct wp_node* node) wp_ast_optimize(node->l); if (node->l->type == WP_NUMBER) { - double v = -wp_ast_eval(node->l); + double v = -((struct wp_number*)(node->l))->value; ((struct wp_number*)node)->type = WP_NUMBER; ((struct wp_number*)node)->value = v; } @@ -893,7 +825,7 @@ wp_ast_print (struct wp_node* node) switch (node->type) { case WP_NUMBER: - printf("NUMBER: %.17g\n", wp_ast_eval(node)); + printf("NUMBER: %.17g\n", ((struct wp_number*)node)->value); break; case WP_SYMBOL: printf("VARIABLE: %s\n", ((struct wp_symbol*)node)->name); diff --git a/Source/Parser/wp_parser_y.h b/Source/Parser/wp_parser_y.h index 9c1a0d448..c583d1a33 100644 --- a/Source/Parser/wp_parser_y.h +++ b/Source/Parser/wp_parser_y.h @@ -143,13 +143,15 @@ void wp_parser_regvar (struct wp_parser* parser, char const* name, double* p); void wp_parser_setconst (struct wp_parser* parser, char const* name, double c); /* We need to walk the tree in these functions */ -double wp_ast_eval (struct wp_node* node); void wp_ast_optimize (struct wp_node* node); size_t wp_ast_size (struct wp_node* node); void wp_ast_print (struct wp_node* node); void wp_ast_regvar (struct wp_node* node, char const* name, double* p); void wp_ast_setconst (struct wp_node* node, char const* name, double c); +double wp_call_f1 (enum wp_f1_t type, double a); +double wp_call_f2 (enum wp_f2_t type, double a, double b); + #ifdef __cplusplus } #endif -- cgit v1.2.3 From 595134e48b1041655ee0bd97cdafa60cd41e3b98 Mon Sep 17 00:00:00 2001 From: Andrew Myers Date: Mon, 18 Mar 2019 14:08:29 -0400 Subject: do explicit casts in the Parser --- Source/Parser/wp_parser_y.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'Source/Parser/wp_parser_y.c') diff --git a/Source/Parser/wp_parser_y.c b/Source/Parser/wp_parser_y.c index ac752a948..402c4e67b 100644 --- a/Source/Parser/wp_parser_y.c +++ b/Source/Parser/wp_parser_y.c @@ -21,7 +21,7 @@ wp_defexpr (struct wp_node* body) struct wp_node* wp_newnumber (double d) { - struct wp_number* r = malloc(sizeof(struct wp_number)); + struct wp_number* r = (struct wp_number*) malloc(sizeof(struct wp_number)); r->type = WP_NUMBER; r->value = d; return (struct wp_node*) r; @@ -30,7 +30,7 @@ wp_newnumber (double d) struct wp_symbol* wp_makesymbol (char* name) { - struct wp_symbol* symbol = malloc(sizeof(struct wp_symbol)); + struct wp_symbol* symbol = (struct wp_symbol*) malloc(sizeof(struct wp_symbol)); symbol->type = WP_SYMBOL; symbol->name = strdup(name); symbol->pointer = NULL; @@ -46,7 +46,7 @@ wp_newsymbol (struct wp_symbol* symbol) struct wp_node* wp_newnode (enum wp_node_t type, struct wp_node* l, struct wp_node* r) { - struct wp_node* tmp = malloc(sizeof(struct wp_node)); + struct wp_node* tmp = (struct wp_node*) malloc(sizeof(struct wp_node)); tmp->type = type; tmp->l = l; tmp->r = r; @@ -56,7 +56,7 @@ wp_newnode (enum wp_node_t type, struct wp_node* l, struct wp_node* r) struct wp_node* wp_newf1 (enum wp_f1_t ftype, struct wp_node* l) { - struct wp_f1* tmp = malloc(sizeof(struct wp_f1)); + struct wp_f1* tmp = (struct wp_f1*) malloc(sizeof(struct wp_f1)); tmp->type = WP_F1; tmp->l = l; tmp->ftype = ftype; @@ -66,7 +66,7 @@ wp_newf1 (enum wp_f1_t ftype, struct wp_node* l) struct wp_node* wp_newf2 (enum wp_f2_t ftype, struct wp_node* l, struct wp_node* r) { - struct wp_f2* tmp = malloc(sizeof(struct wp_f2)); + struct wp_f2* tmp = (struct wp_f2*) malloc(sizeof(struct wp_f2)); tmp->type = WP_F2; tmp->l = l; tmp->r = r; @@ -89,7 +89,7 @@ yyerror (char const *s, ...) struct wp_parser* wp_parser_new (void) { - struct wp_parser* my_parser = malloc(sizeof(struct wp_parser)); + struct wp_parser* my_parser = (struct wp_parser*) malloc(sizeof(struct wp_parser)); my_parser->sz_mempool = wp_ast_size(wp_root); my_parser->p_root = malloc(my_parser->sz_mempool); @@ -135,7 +135,7 @@ wp_parser_allocate (struct wp_parser* my_parser, size_t N) struct wp_parser* wp_parser_dup (struct wp_parser* source) { - struct wp_parser* dest = malloc(sizeof(struct wp_parser)); + struct wp_parser* dest = (struct wp_parser*) malloc(sizeof(struct wp_parser)); dest->sz_mempool = source->sz_mempool; dest->p_root = malloc(dest->sz_mempool); dest->p_free = dest->p_root; @@ -267,7 +267,7 @@ wp_parser_ast_dup (struct wp_parser* my_parser, struct wp_node* node, int move) case WP_SYMBOL: result = wp_parser_allocate(my_parser, sizeof(struct wp_symbol)); memcpy(result, node , sizeof(struct wp_symbol)); - ((struct wp_symbol*)result)->name = wp_parser_allocate + ((struct wp_symbol*)result)->name = (char*) wp_parser_allocate (my_parser, strlen(((struct wp_symbol*)node)->name)+1); strcpy(((struct wp_symbol*)result)->name, ((struct wp_symbol*)node )->name); -- cgit v1.2.3