Mercurial > hg > dmlib
view dmevalw.c @ 740:48c48e94d87f
More work on the evaluator. Still broken, of course.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 22 Apr 2013 09:37:34 +0300 |
parents | 9b39c0a3f7b7 |
children | 7be7c7c5deaa |
line wrap: on
line source
#include "dmevalw.h" static const char *dm_oper_names[OP_NOPERS] = { "NONE", "+", "-", "*", "/", "%", "#", "~", "<<", ">>", "&", "|", "^", ">", "<", ">=", "<=", "FUNC", "VAR", "SUBEXPR", "CONST", }; static void dm_print_optree_do(FILE *out, DMEvalContext *ev, DMEvalNode *node, const int level) { int i; while (node != NULL) { switch (node->op) { case OP_FUNC: fprintf(out, "%s(", node->id != NULL ? node->id->name : "?ERROR"); for (i = 0; i < node->id->nargs; i++) { dm_print_optree_do(out, ev, node->args[i], level + 1); if (i < node->id->nargs - 1) fprintf(out, ","); } fprintf(out, ")"); break; case OP_VAR: fprintf(out, "%s", node->id != NULL ? node->id->name : "?ERROR"); break; case OP_CONST: fprintf(out, "%.1f", node->val); break; case OP_SUBEXPR: fprintf(out, "("); if (node->subexpr != NULL) dm_print_optree_do(out, ev, node->subexpr, level + 1); else fprintf(out, "?ERROR"); fprintf(out, ")"); break; default: if (node->op > 0 && node->op < OP_NOPERS) fprintf(out, "%s", dm_oper_names[node->op]); else fprintf(out, "ERROR!"); break; } node = node->next; } } void dm_print_optree(FILE *out, DMEvalContext *ev, DMEvalNode *node) { dm_print_optree_do(out, ev, node, 0); fprintf(out, "\n"); }