HIDDEN union tree * eval_bool(struct bu_list *hp) { int done=0; union tree *final_tree; struct tokens *tok; while (done != 1) { do_inter(hp); do_union_subtr(hp); done = do_paren(hp); } tok = BU_LIST_NEXT(tokens, hp); final_tree = tok->tp; BU_LIST_DEQUEUE(&tok->l); bu_free((char *)tok, "tok"); return final_tree; }
static int evaluate( char * * line, long *val ) { long arg; char * ptr; char * str; char * endptr; int ercode; operator * op; int expr_oper; // looking for term or operator expr_oper = 0; coper = 0; cvalue = 0; nparens = 0; ptr = *line; while( *ptr ) { if( *ptr == ' ' ) { if( ignore_blanks ) { ptr++; continue; } else { break; } } switch( expr_oper ) { case 0: // look for term str = get_exp( ptr ); if( str == NULL ) { // nothing is error return( not_ok ); } op = get_op( str ); if( *(str +1) == NULC ) { if( NULL != op ) { push_op( op->operc ); ptr++; break; } if( (*str == '-' ) || (*str == '+' ) ) { push_op(*str); ++ptr; break; } } arg = strtol( str, &endptr, 10 ); if( (((arg == LONG_MIN) || (arg == LONG_MAX)) && errno == ERANGE) || (str == endptr) ) { return( not_ok ); } push_val( arg ); ptr += endptr - str; // to the next unprocessed char expr_oper = 1; // look for operator next break; case 1: // look for operator op = get_op( ptr ); if( NULL == op ) { return( not_ok ); } if( ')' == *ptr ) { ercode = do_paren(); if( ok > ercode ) { return( ercode ); } } else { while( coper && op->priority <= get_prio_m1() ) { do_expr(); } push_op( op->operc ); expr_oper = 0; // look for term next } ptr++; break; } } while( 1 < cvalue ) { ercode = do_expr(); if( ok > ercode ) return ercode; } if( !coper ) { *line = ptr; // next scan position return( pop_val( val ) ); // no operations left return result } else { return( not_ok ); } }
int evaluate(char *line, double *val) //-------------------------------------------------------------------- // Evaluates an ASCII mathematical expression // INPUT: line: String to evaluate // val: Storage to receive double result // // RETURN: SUCCESS = 0 if successful // E_ERROR = -1 if syntax error // R_ERROR = -2 if runtime error // DUV_ZERO= -3 Division by 0 // // Side effects: Removes all whitespace from the string and converts // it to U.C. //-------------------------------------------------------------------- { double arg; char *ptr = line, *str, *endptr; int ercode; struct operator *op; strupr(line); rmallws(line); state = op_sptr = arg_sptr = parens = 0; while (*ptr) { switch (state) { case 0: if (NULL != (str = get_exp(ptr))) { if (NULL != (op = get_op(str)) && strlen(str) == op->taglen) { push_op(op->token); ptr += op->taglen; break; } if (SUCCESS == strcmp(str, "-")) { push_op(*str); ++ptr; break; } if (SUCCESS == strcmp(str, "PI")) push_arg(Pi); else { if (0.0 == (arg = strtod(str, &endptr)) && NULL == strchr(str, '0')) { return E_ERROR; } push_arg(arg); } ptr += strlen(str); } else return E_ERROR; state = 1; break; case 1: if (NULL != (op = get_op(ptr))) { if (')' == *ptr) { if (SUCCESS > (ercode = do_paren())) return ercode; } else { while (op_sptr && op->precedence <= getTOSprec()) { do_op(); } push_op(op->token); state = 0; } ptr += op->taglen; } else return E_ERROR; break; } } while (1 < arg_sptr) { if (SUCCESS > (ercode = do_op())) return ercode; } if (!op_sptr) return pop_arg(val); else return E_ERROR; }