view dmevalw.c @ 726:9b39c0a3f7b7

Cosmetics.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 21 Apr 2013 18:32:49 +0300
parents f3cab48c6746
children 48c48e94d87f
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");
}