view dmeval.h @ 510:43ea59887c69

Start work on making C64 formats encoding possible by changing DMDecodeOps to DMEncDecOps and adding fields and op enums for custom encode functions, renaming, etc. Split generic op sanity checking into a separate function in preparation for its use in generic encoding function.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Nov 2012 15:06:01 +0200
parents 32250b436bca
children 05eb24a608f0
line wrap: on
line source

#ifndef DMEVAL_H
#define DMEVAL_H

#include "dmlib.h"


typedef double DMValue;
#define DMCONVTYPE (int)


enum
{
    OP_NONE,

    OP_ADD,
    OP_SUB,
    OP_MUL,
    OP_DIV,
    OP_MOD,

    OP_LSHIFT,
    OP_RSHIFT,

    OP_AND,
    OP_OR,
    OP_XOR,
    
    OP_FUNC,
    OP_VAR,
    OP_SUBEXPR,
    OP_CONST,

    OP_NOPERS
} DMOperType;

enum
{
    ID_FUNC,
    ID_VAR
} DMEvalIdType;


typedef struct
{
    char *name;
    int type;
    DMValue (*func)(DMValue);
    DMValue *var;
} DMEvalId;


typedef struct DMEvalNode
{
    int op;
    DMValue val;
    DMEvalId *id;

    struct DMEvalNode *subexpr, *next, *prev;
} DMEvalNode;


typedef struct
{
    BOOL err;
    char *errStr;
    
    int nids;
    DMEvalId *ids;

    int mode, prev, expect;
} DMEvalContext;


DMEvalId *dm_eval_find_id(DMEvalContext *ev, const char *name);
DMEvalId *dm_eval_add_var(DMEvalContext *ev, const char *name, DMValue *var);
DMEvalId *dm_eval_add_func(DMEvalContext *ev, const char *name, DMValue (*func)(DMValue));

DMEvalContext *dm_eval_new(void);
void dm_eval_close(DMEvalContext *ev);
void dm_eval_free(DMEvalNode *node);
void dm_eval_clear_err(DMEvalContext *ev);

int dm_eval_parse_expr(DMEvalContext *ev, char *expr, DMEvalNode **result);
int dm_eval_reorder(DMEvalContext *ev, DMEvalNode *node, DMEvalNode **result);
int dm_eval_exec(DMEvalContext *ev, DMEvalNode *tree, DMValue *presult);

void dm_print_optree(DMEvalContext *ev, DMEvalNode *node);


#endif // DMEVAL_H