AST_Real RangeIterator::eval(AST_Expression exp, VarSymbolTable symbolTable) { EvalExp evaluator(symbolTable); AST_Expression result = evaluator.eval(exp); ERROR_UNLESS(result->expressionType() == EXPREAL || result->expressionType() == EXPINTEGER, "RangeIterator::getVal:\n" "Expression type should be EXPREAL or EXPINTEGER \n"); if (result->expressionType() == EXPREAL) { return result->getAsReal()->val(); } else { return result->getAsInteger()->val(); } }
void eval_binary_exp_exp_test() { AST_Expression_Integer intExp = (AST_Expression_Integer) newAST_Expression_Integer(2); AST_Expression_Integer intExp2 = (AST_Expression_Integer) newAST_Expression_Integer(3); AST_Expression_BinOp binOp = (AST_Expression_BinOp) newAST_Expression_BinOp(intExp, intExp2, BINOPEXP); EvalExp *evalExp = new EvalExp(NULL); AST_Expression result = evalExp->eval(binOp); BOOST_CHECK(result->expressionType() == EXPINTEGER); BOOST_CHECK(result->getAsInteger()->val() == 8); }
AST_Real BraceIterator::next() { AST_Expression exp = *_braceExpElementsIter; AST_Real value; switch (exp->expressionType()){ case EXPINTEGER: value = exp->getAsInteger()->val(); break; case EXPREAL: value = (int)exp->getAsReal()->val(); break; default: ERROR("BraceIterator::next():\n" "Incorrect brace expression element's type."); } _braceExpElementsIter++; return value; }
void eval_binary_exp_combi_test() { // 24 / ((2 + 1 + (-1)) ^ 3) AST_Expression_Integer intExp = (AST_Expression_Integer) newAST_Expression_Integer(24); AST_Expression_Integer intExp2 = (AST_Expression_Integer) newAST_Expression_Real(2); AST_Expression_Integer intExp3 = (AST_Expression_Integer) newAST_Expression_Integer(1); AST_Expression_Integer intExp4 = (AST_Expression_Integer) newAST_Expression_Integer(1); AST_Expression_Integer intExp5 = (AST_Expression_Integer) newAST_Expression_Integer(3); AST_Expression_BinOp binOp = (AST_Expression_BinOp) newAST_Expression_BinOp(intExp2, intExp3, BINOPADD); AST_Expression_UMinus uMinus = (AST_Expression_UMinus) newAST_Expression_UnaryMinus(intExp4); AST_Expression_BinOp binOp1 = (AST_Expression_BinOp) newAST_Expression_BinOp(binOp, uMinus, BINOPADD); AST_Expression_BinOp binOp2 = (AST_Expression_BinOp) newAST_Expression_BinOp(binOp1, intExp5, BINOPEXP); AST_Expression_BinOp binOp3 = (AST_Expression_BinOp) newAST_Expression_BinOp(intExp, binOp2, BINOPDIV); EvalExp *evalExp = new EvalExp(NULL); AST_Expression result = evalExp->eval(binOp3); BOOST_CHECK(result->expressionType() == EXPREAL); BOOST_CHECK(result->getAsReal()->val() == 3); }
Type TypeCheck_::check_expression(AST_Expression e) { Type ct,t1,t2,t; switch (e->expressionType()) { case EXPBINOP: { AST_Expression_BinOp b = e->getAsBinOp(); return check_binop(b->left() , b->right() , b->binopType()); } case EXPUMINUS: { AST_Expression_UMinus b = e->getAsUMinus(); t = check_expression(b->exp()); if ( check_equal(t , T("Integer")) or check_equal(t , T("Real")) ) return t; throw "Type Error (3)"; } case EXPOUTPUT : { AST_Expression_Output b = e->getAsOutput(); return check_expression(b->expressionList()->front() ); } case EXPIF: { AST_Expression_If b = e->getAsIf(); ct = check_expression(b->condition() ); t1 = check_expression(b->then() ); t2 = check_expression(b->else_exp()); // Falta el elseIF if ( !check_equal(ct, T("Boolean")) ) throw "Type Error (4)"; if ( !check_equal(t1,t2) ) throw "Type Error (5)"; return t1; } case EXPCALL: { // Añadir las funciones en la listaaaa de variables AST_Expression_Call c = e->getAsCall(); if ( toStr(c->name()) == "sample" ) return T("Boolean"); if ( toStr(c->name()) == "pre" ) return check_expression(c->arguments()->front()); return T("Real"); } case EXPCOMPREF: { AST_Expression_ComponentReference b = e->getAsComponentReference(); VarInfo tt = varEnv->lookup( toStr(b->names()->front()) ); if (tt == NULL) { cerr << "Var:" << b->names()->front() << ":"; throw "Variable no existe (8)"; } if (b->indexes()->front()->size() == 0) return tt->type(); else { Type t = tt->type(); AST_ExpressionListIterator exit; foreach(exit , b->indexes()->front() ) if (t->getType() == TYARRAY) t = t->getAsArray()->arrayOf(); else throw "Type Error (7)"; return t; } break; } case EXPDERIVATIVE: return T("Real"); case EXPBOOLEAN: return T("Boolean"); case EXPSTRING: return T("String"); case EXPREAL: return T("Real"); case EXPINTEGER: return T("Integer"); case EXPBOOLEANNOT: { AST_Expression_BooleanNot b = e->getAsBooleanNot(); t = check_expression(b->exp()); if ( !check_equal(t, T("Boolean")) ) throw "Type Error (6)"; return t; } default: throw "No implrementado aun! (check_expression)"; } }
int MMO_EvalInitExp_::foldTraverseElement (AST_Expression exp) { int ret = 0; switch (exp->expressionType ()) { case EXPCOMPREF: { AST_Expression_ComponentReference cr = exp->getAsComponentReference (); VarInfo vi = _vt->lookup (cr->name ()); if (vi == NULL) { Error::getInstance ()->add (exp->lineNum (), EM_IR | EM_VARIABLE_NOT_FOUND, ER_Error, "%s", cr->name ().c_str ()); return (ret); } if (!vi->isConstant ()) { Error::getInstance ()->add ( exp->lineNum (), EM_IR | EM_INIT_EXP, ER_Error, "Only constants allowed inside initial expressions. %s", cr->name ().c_str ()); return (ret); } return (vi->value ()); } case EXPBOOLEAN: { AST_Expression_Boolean eb = exp->getAsBoolean (); if (eb->value ()) { return (1); } else { return (0); } } case EXPBOOLEANNOT: { AST_Expression_BooleanNot ebn = exp->getAsBooleanNot (); int res = foldTraverse (ebn->exp ()); if (res == 0) { return (1); } else { return (0); } } case EXPREAL: Error::getInstance ()->add ( 0, EM_IR | EM_INIT_EXP, ER_Warning, "Implicit conversion from Real to Integer, in initial expression."); return (exp->getAsReal ()->val ()); case EXPINTEGER: return (exp->getAsInteger ()->val ()); default: Error::getInstance ()->add ( 0, EM_IR | EM_INIT_EXP, ER_Warning, "Initial expression not recognized, returning zero as default value."); break; } return (ret); }