218 |
|
{ |
219 |
|
EPNODE *ep; |
220 |
|
|
221 |
< |
if (epar->nkids < 0) /* we don't really handle this properly */ |
222 |
< |
epar->nkids *= -1; |
221 |
> |
if (epar->nkids < 0) /* can't handle array allocations */ |
222 |
> |
return; |
223 |
|
|
224 |
|
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
225 |
< |
while (ep->type == epar->type) { |
225 |
> |
while (ep->type == epar->type && ep->nkids > 0) { |
226 |
|
EPNODE *ep1 = ep->v.kid; |
227 |
|
while (ep1->sibling != NULL) |
228 |
|
ep1 = ep1->sibling; |
229 |
|
ep1->sibling = ep->sibling; |
230 |
< |
epar->nkids += nekids(ep) - 1; |
230 |
> |
epar->nkids += ep->nkids - 1; |
231 |
|
ep1 = ep->v.kid; |
232 |
|
*ep = *ep1; |
233 |
|
efree(ep1); /* not epfree()! */ |
236 |
|
|
237 |
|
|
238 |
|
void |
239 |
< |
epoptimize( /* flatten operations and lists -> arrays */ |
239 |
> |
epoptimize( /* flatten operations, lists -> arrays */ |
240 |
|
EPNODE *epar |
241 |
|
) |
242 |
|
{ |
243 |
|
EPNODE *ep; |
244 |
|
|
245 |
|
if ((epar->type == '+') | (epar->type == '*')) |
246 |
< |
epflatten(epar); /* commutative & associative */ |
246 |
> |
epflatten(epar); /* flatten associative operations */ |
247 |
|
|
248 |
|
if (epar->nkids) /* do children if any */ |
249 |
|
for (ep = epar->v.kid; ep != NULL; ep = ep->sibling) |
346 |
|
) |
347 |
|
{ |
348 |
|
EPNODE *ep1 = ep->v.kid; |
349 |
< |
EPNODE *ep2 = ep1->sibling; |
350 |
< |
double d; |
349 |
> |
double den = evalue(ep1->sibling); |
350 |
|
|
351 |
< |
d = evalue(ep2); |
353 |
< |
if (d == 0.0) { |
351 |
> |
if (den == 0.0) { |
352 |
|
wputs("Division by zero\n"); |
353 |
|
errno = ERANGE; |
354 |
|
return(0.0); |
355 |
|
} |
356 |
< |
return(envalue(ep1) / d); |
356 |
> |
return(envalue(ep1) / den); |
357 |
|
} |
358 |
|
|
359 |
|
static double |
560 |
|
EPNODE *ek |
561 |
|
) |
562 |
|
{ |
563 |
< |
if (ep->nkids < 0) /* we don't really handle this properly */ |
564 |
< |
ep->nkids *= -1; |
563 |
> |
if (ep->nkids < 0) { |
564 |
> |
eputs("Cannot add kid to EPNODE array\n"); |
565 |
> |
quit(1); |
566 |
> |
} |
567 |
|
ep->nkids++; |
568 |
|
if (ep->v.kid == NULL) |
569 |
|
ep->v.kid = ek; |