started migration stack->tree
This commit is contained in:
95
eval.c
95
eval.c
@@ -1,27 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include "lceb.h"
|
||||
|
||||
int eval_cell(pos)
|
||||
STACKELT *pos;
|
||||
{
|
||||
STACKELT *prev=pos-1;
|
||||
int curop=pos->curop, nop=pos->nop;
|
||||
int val1, val2;
|
||||
char op;
|
||||
int res;
|
||||
|
||||
while (curop<nop) {
|
||||
op=pos->op[curop];
|
||||
val2=pos->val;
|
||||
res=-1;
|
||||
printf("eval_cell(%c, %d)\n", op, val2);
|
||||
while ((val1=prev->val) == -1)
|
||||
prev--;
|
||||
int eval_node(node, depth, pvals, pops)
|
||||
NODE *node;
|
||||
int depth;
|
||||
int *pvals;
|
||||
char *pops;
|
||||
{
|
||||
static int *vals, *val_zero;
|
||||
static char *ops, *ops_zero;
|
||||
static int ncalc;
|
||||
static *node_zero;
|
||||
int val1, val2, op, res=-1, i;
|
||||
|
||||
if (depth == 0) {
|
||||
val_zero=vals=pvals;
|
||||
ops_zero=ops=pops;
|
||||
node_zero=node;
|
||||
ncalc=0;
|
||||
}
|
||||
# ifdef DEBUG1
|
||||
for (i=0; i<=depth; ++i)
|
||||
printf(" ");
|
||||
printf("eval : depth=%d : ", depth);
|
||||
if (node->type == TREE_NODE) {
|
||||
printf("node(%c)\n", *ops);
|
||||
} else {
|
||||
printf("val(%d)\n", *vals);
|
||||
}
|
||||
# endif
|
||||
if (node->type == TREE_LEAF) {
|
||||
node->val=*vals;
|
||||
res=*vals;
|
||||
vals++;
|
||||
} else {
|
||||
op=*ops;
|
||||
node->op=*ops;
|
||||
ops++;
|
||||
ncalc++;
|
||||
//printf("NEW node(%s)\n", ops);
|
||||
val1=eval_node(node->left, depth+1, pvals, ops);
|
||||
if (val1 <= 0)
|
||||
return -1;
|
||||
val2=eval_node(node->right, depth+1, pvals, ops);
|
||||
if (val2 <= 0)
|
||||
return -1;
|
||||
switch (op) {
|
||||
case Nop:
|
||||
case End:
|
||||
return val2;
|
||||
break;
|
||||
case Add:
|
||||
res=val1+val2;
|
||||
break;
|
||||
@@ -36,27 +61,19 @@ int eval_cell(pos)
|
||||
if (val1 > val2 && (val1 % val2 == 0))
|
||||
res=val1/val2;
|
||||
break;
|
||||
case Nop:
|
||||
case End:
|
||||
fprintf(stderr, "Fatal: bad op [%d] in eval\n", op);
|
||||
exit(1);
|
||||
break;
|
||||
}
|
||||
curop++;
|
||||
if (res==-1)
|
||||
break;
|
||||
pos->val=res;
|
||||
printf("\t--> eval=%d\n", res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int eval_stack(stack)
|
||||
STACK *stack;
|
||||
{
|
||||
int pos, last=stack->last, res;
|
||||
STACKELT *pstack=stack->stack;
|
||||
|
||||
printf("+++++ eval_stack: addr=%p\n", pstack);
|
||||
for (pos=1; pos<last; ++pos) {
|
||||
//printf("eval_stack(%d/%d): addr=%p\n", pos, stack->last, pstack+pos);
|
||||
res=eval_cell(stack->stack+pos);
|
||||
//printf(" -> val(%d)=%d\n", pos, res);
|
||||
}
|
||||
if (res > 0)
|
||||
check_best(res, ncalc, node_zero, val_zero, ops_zero);
|
||||
# ifdef DEBUG1
|
||||
for (i=0; i<=depth; ++i)
|
||||
printf(" ");
|
||||
printf("res=%d\n", res);
|
||||
# endif
|
||||
return res;
|
||||
}
|
||||
|
Reference in New Issue
Block a user