www.pudn.com > interpreter.zip > interpreter.tree.c


#include "./interpreter.h"
#include 

NODE trheap[TREE_HEAP_LEN];
int trheap_ptr = 0;
char strheap[STR_HEAP_LEN];
int strheap_ptr = 0;
extern double x[];

int setcleaf ( double value )  /* 定数の leaf をセットする */
{
    if ( trheap_ptr >= TREE_HEAP_LEN ) yyerror ("tree heap full");
    trheap[trheap_ptr].instr = CONSTLEAF;
    trheap[trheap_ptr].with.value = value;
    return (trheap_ptr++);
}

int setvleaf ( int i )
{
    if ( trheap_ptr >= TREE_HEAP_LEN ) yyerror ("tree heap full");
    trheap[trheap_ptr].instr = VARLEAF;
    trheap[trheap_ptr].to.var = &x[i];
    return (trheap_ptr++);
}

int binarynode ( OPERATION *op, int first, int second)
{
    if ( trheap_ptr >= TREE_HEAP_LEN ) yyerror ("tree heap full");
    trheap[trheap_ptr].instr = BINARY;
    trheap[trheap_ptr].with.op = op;
    trheap[trheap_ptr].to.the.left = first;
    trheap[trheap_ptr].to.the.right = second;
    return (trheap_ptr++);
}

int unarynode ( OPERATION *op, int first )
{
    if ( trheap_ptr >= TREE_HEAP_LEN ) yyerror ("tree heap full");
    trheap[trheap_ptr].instr = UNARY;
    trheap[trheap_ptr].with.op = op;
    trheap[trheap_ptr].to.the.left = first;
    trheap[trheap_ptr].to.the.right = 800;
    return (trheap_ptr++);
}

ec_tree ( int ptr )       /* ptr は trheap のポインタ */
{
    ecdtr ( ptr );
    trheap_ptr = 0;
}

ecdtr ( int ptr )
{
    switch (trheap[ptr].instr){
        case CONSTLEAF:
            iprg[prg_ctr].instr = PUTCONST;
            iprg[prg_ctr].ptr.value = trheap[ptr].with.value;
            prg_ctr++;
            break;
        case VARLEAF:
            iprg[prg_ctr].instr = SET;
            iprg[prg_ctr].var = trheap[ptr].to.var;
            prg_ctr++;
            break;
        case BINARY:
            ecdtr(trheap[ptr].to.the.left);
            ecdtr(trheap[ptr].to.the.right);
            iprg[prg_ctr].instr = BINARY;
            iprg[prg_ctr].ptr.op = trheap[ptr].with.op;
            prg_ctr++;
            break;
        case UNARY:
            ecdtr(trheap[ptr].to.the.left);
            iprg[prg_ctr].instr = UNARY;
            iprg[prg_ctr].ptr.op = trheap[ptr].with.op;
            prg_ctr++;
            break;
            
    }
}

char *putstring (char* str)
{
    int i, len;
    char *str2;
    len = strlen (str);
    str2 = &strheap[strheap_ptr];
    for ( i = 0; i <= len; ++i,++strheap_ptr){
        strheap[strheap_ptr] = str[i];
    }
    strheap[strheap_ptr] = '\0';
        if ( len % 2 ) strheap_ptr++; /* segmentation error をさけるため */
    if ( strheap_ptr >= STR_HEAP_LEN ) yyerror ("string heap full");
    return ( str2 );
}