static int eval_exp(const char **s) { int base; while (isspace(**s)) ++*s; switch (**s) { // Unary '+' and '-' operators case '+': ++*s; base = eval_exp(s); break; case '-': ++*s; base = -eval_exp(s); break; case '(': ++*s; // Eat "(" base = eval_sum(s); if (**s != ')') longjmp(err_jmp_buf, 1); ++*s; // Eat ")" break; default: base = eval_num(s); } // Check if we have an exponent while (isspace(**s)) ++*s; if (**s == '*' && *(*s + 1) == '*') { *s += 2; // Eat "**" return pow(base, eval_exp(s)); } return base; }
void exec_for() { int cond; char *temp, *temp2; int brace ; get_token(); eval_exp(&cond); if(*token!=';') sntx_err(SEMI_EXPECTED); prog++; temp = prog; for(;;) { eval_exp(&cond); if(*token!=';') sntx_err(SEMI_EXPECTED); prog++; temp2 = prog; brace = 1; while(brace) { get_token(); if(*token=='(') brace++; if(*token==')') brace--; } if(cond) interp_block(); else { find_eob(); return; } prog = temp2; eval_exp(&cond); prog = temp; } }
/* Execute a for loop. */ void exec_for(void) { int cond; char *temp, *temp2; int brace ; get_token(); eval_exp(&cond); /* initialization expression */ if(*token != ';') sntx_err(SEMI_EXPECTED); prog++; /* get past the ; */ temp = prog; for(;;) { eval_exp(&cond); /* check the condition */ if(*token != ';') sntx_err(SEMI_EXPECTED); prog++; /* get past the ; */ temp2 = prog; /* find the start of the for block */ brace = 1; while(brace) { get_token(); if(*token == '(') brace++; if(*token == ')') brace--; } if(cond) interp_block(); /* if true, interpret */ else { /* otherwise, skip around loop */ find_eob(); return; } prog = temp2; eval_exp(&cond); /* do the increment */ prog = temp; /* loop back to top */ } }
//dfs to check whether the leaf was tainted void arithmetic_check(ast_t *p, state_t *state){ unsigned int addr = 0; ast_list_t *c; //child node value_t e; switch(p->tag){ case int_ast: //ignore return; break; case var_ast: e = lookup_var(p->info.varname, state->tbl); if(e.taint){ if(is_first_taint){ fprintf(stderr, "%s", p->info.varname); is_first_taint = 0; }else{ fprintf(stderr, ", %s", p->info.varname); } } break; case node_ast: if(p->info.node.tag == READSECRETINT){ if(is_first_taint){ fprintf(stderr, "Direct", addr); is_first_taint = 0; }else{ fprintf(stderr, ", Direct", addr); } } else if(p->info.node.tag == MEM){ is_first_eval = 0; e = load(eval_exp(p->info.node.arguments->elem, state->tbl, state->mem).value,state->mem); addr = eval_exp(p->info.node.arguments->elem, state->tbl, state->mem).value; //printf("test --- %d\n", e.taint); arithmetic_check(p->info.node.arguments->elem, state); //printf("\n dfs eval_exp(p->info.node.arguments->elem, state->tbl, state->mem).value %d\n", eval_exp(p->info.node.arguments->elem, state->tbl, state->mem).value); //printf("test --- %d\n", e.taint); if(e.taint){ if(is_first_taint){ fprintf(stderr, "mem[%d]", addr); is_first_taint = 0; } else{ fprintf(stderr, ", mem[%d]", addr); } } return; } c = p->info.node.arguments; while(c){ arithmetic_check(c->elem, state); c = c->next; } return; break; } }
void set() // usage: set(int *ip, int i) >>> *ip = i; { int ii, i, *ip; start_check(); eval_exp(&ii); get_token(); // look for comma separator if(*token != ',') sntx_err(SYNTAX); // syntax error eval_exp(&i); ip = (int *)ii; *ip = i; finish_check(); }
double Parser::Evaluate (void) { double k; prog=expression; eval_exp (&k); return k; }
void func_ret() { int value; value = 0; eval_exp(&value); ret_value = value; }
/* Put a character to the display. */ int call_putch() { int value; eval_exp(&value); printf("%c", value); return value; }
void sinhcosh(const T& x, T* p_sinh, T* p_cosh) { typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type; typedef typename mpl::front<typename T::float_types>::type fp_type; switch(eval_fpclassify(x)) { case FP_NAN: case FP_INFINITE: if(p_sinh) *p_sinh = x; if(p_cosh) { *p_cosh = x; if(eval_get_sign(x) < 0) p_cosh->negate(); } return; case FP_ZERO: if(p_sinh) *p_sinh = x; if(p_cosh) *p_cosh = ui_type(1); return; default: ; } bool small_sinh = eval_get_sign(x) < 0 ? x.compare(fp_type(-0.5)) > 0 : x.compare(fp_type(0.5)) < 0; if(p_cosh || !small_sinh) { T e_px, e_mx; eval_exp(e_px, x); eval_divide(e_mx, ui_type(1), e_px); if(p_sinh) { if(small_sinh) { small_sinh_series(x, *p_sinh); } else { eval_subtract(*p_sinh, e_px, e_mx); eval_ldexp(*p_sinh, *p_sinh, -1); } } if(p_cosh) { eval_add(*p_cosh, e_px, e_mx); eval_ldexp(*p_cosh, *p_cosh, -1); } } else { small_sinh_series(x, *p_sinh); } }
static int eval_product(const char **s) { bool is_times = true; int product = 1; for (;;) { if (is_times) product *= eval_exp(s); else product /= eval_exp(s); switch (**s) { case '*': ++*s; is_times = true; continue; case '/': ++*s; is_times = false; continue; default: return product; } } }
/* Interpret a single statement or block of code. When interp_block() returns from its initial call, the final brace (or a return) in main() has been encountered. */ void interp_block(void) { int value; char block = 0; do { token_type = get_token(); /* If interpreting single statement, return on first semicolon. */ /* see what kind of token is up */ if(token_type == IDENTIFIER) { /* Not a keyword, so process expression. */ putback(); /* restore token to input stream for further processing by eval_exp() */ eval_exp(&value); /* process the expression */ if(*token!=';') sntx_err(SEMI_EXPECTED); } else if(token_type==BLOCK) { /* if block delimiter */ if(*token == '{') /* is a block */ block = 1; /* interpreting block, not statement */ else return; /* is a }, so return */ } else /* is keyword */ switch(tok) { case CHAR: case INT: /* declare local variables */ putback(); decl_local(); break; case RETURN: /* return from function call */ func_ret(); return; case IF: /* process an if statement */ exec_if(); break; case ELSE: /* process an else statement */ find_eob(); /* find end of else block and continue execution */ break; case WHILE: /* process a while loop */ exec_while(); break; case DO: /* process a do-while loop */ exec_do(); break; case FOR: /* process a for loop */ exec_for(); break; case END: exit(0); } } while (tok != FINISHED && block); }
void motors() { int lspeed, rspeed; start_check(); eval_exp(&lspeed); if ((lspeed < -100) || (lspeed > 100)) sntx_err(PARAM_ERR); get_token(); // look for comma separator if(*token != ',') sntx_err(SYNTAX); // syntax error eval_exp(&rspeed); if ((rspeed < -100) || (rspeed > 100)) sntx_err(PARAM_ERR); setPWM(lspeed, rspeed); finish_check(); }
/* Return from a function. */ void func_ret(void) { int value; value = 0; /* get return value, if any */ eval_exp(&value); ret_value = value; }
int get() // usage: int i = get(int *ip) >>> i = *ip; { int ii, *ip; start_check(); eval_exp(&ii); finish_check(); ip = (int *)ii; return *ip; }
void delay() { int del; start_check(); eval_exp(&del); if ((del < 0) || (del > 1000000)) sntx_err(PARAM_ERR); delayMS(del ); finish_check(); }
static Eina_Bool eval_promote_num(const Eolian_Expression *expr, Eolian_Expression *lhs, Eolian_Expression *rhs, int mask, int emask) { /* make sure the output can be a number */ if (!(mask & EOLIAN_MASK_NUMBER)) return expr_type_error(expr, EOLIAN_MASK_NUMBER, mask); /* eval into primitive value */ if (!eval_exp(expr->lhs, emask, lhs)) return EINA_FALSE; if (!eval_exp(expr->rhs, emask, rhs)) return EINA_FALSE; /* promote so both sides are of the same type */ if (!promote(lhs, rhs)) return EINA_FALSE; return EINA_TRUE; }
Eolian_Value database_expr_eval(const Eolian_Expression *expr, Eolian_Expression_Mask mask) { Eolian_Expression out; Eolian_Value ret; ret.type = EOLIAN_EXPR_UNKNOWN; if (!mask) return ret; if (!eval_exp(expr, mask, &out)) return ret; ret.type = out.type; ret.value = out.value; return ret; }
void exec_do() { int cond; char *temp; putback(); temp = prog; get_token(); interp_block(); get_token(); if(tok!=WHILE) sntx_err(WHILE_EXPECTED); eval_exp(&cond); if(cond) prog = temp; }
/* Execute a while loop. */ void exec_while(void) { int cond; char *temp; putback(); temp = prog; /* save location of top of while loop */ get_token(); eval_exp(&cond); /* check the conditional expression */ if(cond) interp_block(); /* if true, interpret */ else { /* otherwise, skip around loop */ find_eob(); return; } prog = temp; /* loop back to top */ }
/* Execute a do loop. */ void exec_do(void) { int cond; char *temp; putback(); temp = prog; /* save location of top of do loop */ get_token(); /* get start of loop */ interp_block(); /* interpret loop */ get_token(); if(tok != WHILE) sntx_err(WHILE_EXPECTED); eval_exp(&cond); /* check the loop condition */ if(cond) prog = temp; /* if true loop; otherwise, continue on */ }
void exec_while() { int cond; char *temp; putback(); temp = prog; get_token(); eval_exp(&cond); if(cond) interp_block(); else { find_eob(); return; } prog = temp; }
void exec_if() { int cond; eval_exp(&cond); if(cond) { interp_block(); } else { find_eob(); get_token(); if(tok!=ELSE) { putback(); return; } interp_block(); } }
void interp_block() { int value; char block = 0; do { token_type = get_token(); if(token_type==IDENTIFIER) { putback(); eval_exp(&value); if(*token!=';') sntx_err(SEMI_EXPECTED); } else if(token_type==BLOCK) { if(*token=='{') block = 1; else return; } else switch(tok) { case CHAR: case INT: putback(); decl_local(); break; case RETURN: func_ret(); return; case IF: exec_if(); break; case ELSE: find_eob(); break; case WHILE: exec_while(); break; case DO: exec_do(); break; case FOR: exec_for(); break; case END: return; } } while (tok != FINISHED && block); }
//main function main(){ //Asking the user to enter the expression char *exp;//To store the expression exp=(char*)malloc(10000*sizeof(char)); printf("\nEnter the expression to be evaluated: "); //module to get the expression int len=0; char c; while(c=getchar(), c!='\n'){ exp[len]=c; ++len; } exp[len]='\0'; //Getting the result eval_exp(exp,len); }
/* Let Function */ int opcode_let(char *buf) { int status = 0; int res = 0; char *name; char *value; if(strstr(buf, "=") != NULL) { name = strtok (buf, "="); value = strtok (NULL, " "); str_addnewline(value); res = eval_exp(value); update_variable(name, res); } else status = 1; return status; }
void get_args() { int value, count, temp[NUM_PARAMS]; struct variable_type i; count = 0; get_token(); if(*token!='(') sntx_err(PAREN_EXPECTED); do { eval_exp(&value); temp[count] = value; get_token(); count++; } while(*token==','); count--; for(; count>=0; count--) { i.value = temp[count]; i.var_type = ARG; local_push(i); } }
/* A built-in console output function. */ int print(void) { int i; get_token(); if(*token!='(') sntx_err(PAREN_EXPECTED); get_token(); if(token_type==STRING) { /* output a string */ printf("%s ", token); } else { /* output a number */ putback(); eval_exp(&i); printf("%d ", i); } get_token(); if(*token!= ')') sntx_err(PAREN_EXPECTED); get_token(); if(*token!=';') sntx_err(SEMI_EXPECTED); putback(); return 0; }
void print() { int i; start_check(); get_token(); while(*token !=')') { if(token_type==STRING) { uart0SendString(token); } else { putback(); eval_exp(&i); printNumber(10, 10, FALSE, ' ', i); } get_token(); } uart0SendChar('\n'); get_token(); if(*token!=';') sntx_err(SEMI_EXPECTED); putback(); }
/* Execute an if statement. */ void exec_if(void) { int cond; eval_exp(&cond); /* get if expression */ if(cond) { /* is true so process target of IF */ interp_block(); } else { /* otherwise skip around IF block and process the ELSE, if present */ find_eob(); /* find start of next line */ get_token(); if(tok != ELSE) { putback(); /* restore token if no ELSE is present */ return; } interp_block(); } }
/************************************************************************* Calculates the new values for fields to update. Note that row_upd_copy_columns must have been called first. */ UNIV_INLINE void row_upd_eval_new_vals( /*==================*/ upd_t* update) /* in: update vector */ { que_node_t* exp; upd_field_t* upd_field; ulint n_fields; ulint i; n_fields = upd_get_n_fields(update); for (i = 0; i < n_fields; i++) { upd_field = upd_get_nth_field(update, i); exp = upd_field->exp; eval_exp(exp); dfield_copy_data(&(upd_field->new_val), que_node_get_val(exp)); } }