2 * tree creation, deletion and evaluation routines for complex-number
13 #include "complex.tab.h"
15 #define allocnode() rem_malloc(sizeof(Node))
17 Node
*n_asgn(sym
, arg
) /* node for assignment operator */
21 Node
*n
= allocnode();
24 n
->contents
.sym
= sym
;
31 Node
*n_binop(op
, left
, right
) /* node for binary operator */
35 Node
*n
= allocnode();
44 Node
*n_unop(op
, arg
) /* node for unary operator */
48 Node
*n
= allocnode();
57 Node
*n_func(type
, sym
, arg
) /* node for function */
62 Node
*n
= allocnode();
65 n
->contents
.sym
= sym
;
72 Node
*n_symbol(type
, sym
) /* node for symbol - VAR or CONST */
76 Node
*n
= allocnode();
79 n
->contents
.sym
= sym
;
86 Node
*n_number(real
, imag
) /* node for number */
89 Node
*n
= allocnode();
92 n
->contents
.val
.real
= real
;
93 n
->contents
.val
.imag
= imag
;
100 Complex
eval_tree(n
) /* evaluate the complex value of a tree */
105 case NUMBER
: return n
->contents
.val
;
107 case C_BLTIN
: return (*(n
->contents
.sym
->u
.cptr
))(eval_tree(n
->left
));
109 case R_BLTIN
: { Complex rv
;
110 rv
.real
= (*(n
->contents
.sym
->u
.rptr
))(eval_tree(n
->left
));
115 case UFUNC
: { UserFunc
*uf
= &n
->contents
.sym
->u
.ufunc
;
116 uf
->param
->u
.val
= eval_tree(n
->left
);
117 return eval_tree(uf
->tree
);
122 case CONST
: return n
->contents
.sym
->u
.val
;
124 case '+': return cadd(eval_tree(n
->left
), eval_tree(n
->right
));
125 case '-': return csub(eval_tree(n
->left
), eval_tree(n
->right
));
126 case '*': return cmul(eval_tree(n
->left
), eval_tree(n
->right
));
127 case '/': return cdiv(eval_tree(n
->left
), eval_tree(n
->right
));
128 case '^': return cpow(eval_tree(n
->left
), eval_tree(n
->right
));
130 case '(': return eval_tree(n
->left
);
131 case UMINUS
: return cneg(eval_tree(n
->left
));
132 case '\'': return conj(eval_tree(n
->left
));
134 case '=': return n
->contents
.sym
->u
.val
= eval_tree(n
->left
);
136 default: /* should NEVER see this... */
137 execerror("internal - unknown node-type", NULL
);
140 return ({Complex zero
={}; zero
;});
143 /* delete all nodes of a tree */
144 /* not used in current implementation */
151 delete_tree(n->left);
152 delete_tree(n->right);