Beispiel #1
0
	void eval() {
		if (cond->check(map)) {
			_then->eval(map);
		}
		else {
			_else->eval(map);
		}
	}
Beispiel #2
0
int interpret(string code) {
    TokenList tokens = Lexer(code).tokenize();
    Expression * exprTree = Parser(tokens).parse();
    int res = exprTree->eval();
    delete exprTree;
    return res;
}
Beispiel #3
0
// boucle avec plusieurs instructions (un bloc)
void testPour2()
{
    // x = 3
    Variable * x = new Variable("x", 3.0);
    cout << *x << " = " << x->eval() << endl;

    // i = 0
    Variable * i = new Variable("i");
    cout << *i << " = " << i->eval() << endl;
    // init = (i = 1)
    Expression * init = new Affectation(i,new Constante(1.0));
    // condition = (4 > i)
    Expression * condition = new Superieur(new Constante(4), i);
    // actionFinDeBoucle = (i = i + 1)
    Expression * actionFinDeBoucle = new Affectation(i, new Somme(i, new Constante(1)));
    // calcul =   {
    //              x = x + 9
    //              if (x > 20) x = x + 10  else x = x * 10
    //              y = 3
    //              y = x + -100
    //          }

    // bloc =   {
    //              x = x + 9
    //          }
    Bloc * bloc = new Bloc("b1", new Affectation(x, new Somme(x, new Constante(9))));

    // bloc =   {
    //              x = x + 9
    //              if (x > 20) x = x + 10  else x = x * 10
    //              y = 3
    //              y = x + -100
    //          }
    bloc->add(new IfThenElse(new Superieur(x, new Constante(20.0)),
                             new Affectation(x, new Somme(x, new Constante(10.0))),
                             new Affectation(x, new Produit(x, new Constante(10.0)))));
    Variable * y = new Variable("y", 5);
    bloc->add(new Affectation(y, new Somme(x, new Constante(-100))));


    // calcul =   {
    //              x = x + 9
    //              if (x > 20) x = x + 10  else x = x * 10
    //              y = 3
    //              y = x + -100
    //
    Expression * calcul(bloc);

    // pour = for (i = 1; 4 > i; i = i + 1) {
    //              x = x + 9
    //              if (x > 20) x = x + 10  else x = x * 10
    //              y = 3
    //              y = x + -100
    //          }
    Expression * pour = new Pour(init, condition, actionFinDeBoucle, calcul);
    cout << *pour << "\n EVAL pour : " << pour->eval() << endl;
    cout << *x << " = " << x->eval() << endl;
    cout << *y << " = " << y->eval() << endl;
}
Beispiel #4
0
// Calcul d'une factorielle
void testPour1()
{
    // x = 3
    Variable * x = new Variable("x", 3.0);
    cout << *x << " = " << x->eval() << endl;

    // i = 0
    Variable * i = new Variable("i");
    cout << *i << " = " << i->eval() << endl;

    // init = (i = 1)
    Expression * init = new Affectation(i,new Constante(1));
    // condition = (x > i - 1)
    Expression * condition = new Superieur(x, new Somme(i, new Constante(-1)));
    // actionFinDeBoucle = (i = i + 1)
    Expression * actionFinDeBoucle = new Affectation(i, new Somme(i, new Constante(1)));
    // calcul = (res = res * i)
    Variable * res = new Variable("res", 1);
    Expression * calcul(new Affectation(res, new Produit(res,i)));

    // pour = for (i = 1; x > i; i = i + 1) res = res * i
    Expression * pour = new Pour(init, condition, actionFinDeBoucle, calcul);
    cout << *pour << "\n EVAL pour : " << pour->eval() << endl;
    cout << x->eval() <<"!" << " = " << res->eval() << endl;

    // x = 5
    x->set(5.0);
    cout << *x << " = " << x->eval() << endl;
    // condition = (x > i - 1)
    condition = new Superieur(x, new Somme(i, new Constante(-1)));
    // pour = for (i = 1; x > i; i = i + 1) res = res * i
    res->set(1);
    pour = new Pour(init, condition, actionFinDeBoucle, calcul);
    cout << *pour <<  "\n EVAL pour : " << pour->eval() << endl;
    cout << x->eval() <<"!" << " = " << res->eval() << endl;

    Variable::effacerMemoire();
}
Beispiel #5
0
void testBinaire()
{
    // s = 1 + 2 * sin(PI/6)
    Somme * s = new Somme(new Constante(1.0), new Produit(new Constante(2.0), new Sin(new Constante(M_PI/6.0))));
    cout << "s : " << *s << " = " << s->eval() << endl;

    Expression * sbis = s->clone();
    cout << "clone de s : " << *sbis << " = " << sbis->eval() << endl;

    // s > 1
    Superieur comp(s->clone(), new Constante(1.8));
    cout << comp << " = " << (bool)comp.eval() << endl;

    delete s;
    delete sbis;
    cout << "destruction automatique des variables locales allouees sur la PILE: ICI COMP" << endl;
}
Beispiel #6
0
/// SASLThunkTouch
Expression* Thunk::touch()
{
    // if we haven't already evaluated, do it now
    if (!evaluated)
    {
        evaluated = 1;
        Expression* start = value();
        if (start)
        {
            start->eval(value, valueOps, context);
        }
    }
    Expression* val = value();
    if (val)
    {
        return val->touch();
    }
    return val;
}
Beispiel #7
0
//-----------------------------------------------------------------------------
void Mesh::init_cell_orientations(const Expression& global_normal)
{
  std::size_t gdim = geometry().dim();
  std::size_t ndim = global_normal.value_size();

  // Check that global_normal has the "right" size
  // Allowing 3 if gdim < 3 to avoid breaking legacy code.
  if (ndim < gdim && ndim <= 3)
  {
     dolfin_error("Mesh.cpp",
                  "initialize cell orientations",
                  "Global normal value size is %d, smaller than gdim (%d)",
                  ndim, gdim);
  }

  // Resize storage
  _cell_orientations.resize(num_cells());

  // Set orientation
  Array<double> values(ndim);
  Point up;
  for (CellIterator cell(*this); !cell.end(); ++cell)
  {
    // Extract cell midpoint as Array
    const Array<double> x(3, cell->midpoint().coordinates());

    // Evaluate global normal at cell midpoint
    global_normal.eval(values, x);

    // Extract values as Point
    for (unsigned int i = 0; i < ndim; i++)
      up[i] = values[i];
    for (unsigned int i = ndim; i < gdim; i++)
      up[i] = 0.0;

    // Set orientation as orientation relative to up direction.
    dolfin_assert(cell->index() < _cell_orientations.size());
    _cell_orientations[cell->index()] = cell->orientation(up);
  }
}
Beispiel #8
0
 virtual T eval(const ExpressionContext<T> &ctx) const override
 {
     return e1->eval(ctx) - e2->eval(ctx);
 }
Beispiel #9
0
 virtual T eval(const ExpressionContext<T> &ctx) const override
 {
     return pow(e1->eval(ctx), e2->eval(ctx));
 }
Beispiel #10
0
double Block::eval()
{
	Expression * tmp = _expressions.back();
	return tmp->eval();
}
Beispiel #11
0
void testEval() {
    Expression * expr = new Multiply(new Number(3), new Multiply(new Number(4), new Number(5)));
    check("eval", expr->eval() == 60);
}
Beispiel #12
0
int creation() {
    string pathExe;
    cout << "Entrer le path de l'excercice que vous voulez créer (.txt) : ";
    cin >> pathExe;

    string pathRes;
    cout << "Entrer le path du resultat que vous voulez stocker (.txt) : ";
    cin >> pathRes;

    ofstream exe;
    exe.open(pathExe);
    ofstream res;
    res.open(pathRes);

    Expression* e;

    string s;
    double d;

    string question = " = ?";
    string result = " = ";

    int choice = -1;

    do {
        viewMenuCreation();
        cin >> choice;


        switch(choice) {
            case 1:
            {
                cout << "\n\t\tA (constante)\n" << endl;

                e = sConstante();

                s = e->afficher();
                exe << s << "\n";

                d = e->eval();
                res << d << "\n";

                printContent("", "", d);

                break;
            }
            case 2:
            {
                cout << "\n\t\tcos(A)\n" << endl;
                e = sCos(sConstante());

                s = e->afficher();
                exe << s << question << "\n";

                d = e->eval();
                res << s << result << d << "\n";

                printContent(s, result, d);

                break;
            }
            case 3:
            {
                cout << "\n\t\t(C + (B * sin(A)))\n" << endl;
                e = sSomme(sConstante(), sProduit(sConstante(), sSin(sConstante())));

                s = e->afficher();
                exe << s << question << "\n";

                d = e->eval();
                res << s << result << d << "\n";

                printContent(s, result, d);

                break;
            }
            case 4:
            {
                cout << "\n\t\t((D + (C * sin(B))) > A) => true(1), false(0)\n" << endl;
                e = sSuperieur(sSomme(sConstante(), sProduit(sConstante(), sSin(sConstante()))), sConstante());

                s = e->afficher();
                exe << s << question << "\n";

                d = e->eval();
                res << s << result << d << "\n";

                printContent(s, result, d);

                break;
            }
            case 5:
            {
                cout << "\n\t\tx = A\n\t(y <- (C + (B * x))) = ?\n" << endl;
                Variable *e1;
                e1 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";

                printContent(s, result, d);
                ////////////////////////////////////
                e = sAffectation(sVariableAChercher(), sSomme(sConstante(), sProduit(sConstante(), e1->clone())));

                s = e->afficher();
                exe << s << question << "\n";

                d = e->eval();
                res << s << result << d << "\n";

                printContent(s, result, d);

                break;
            }
            case 6:
            {
                cout << "\n\t\tx = A\n\t\t((x > D)) ? (x + C) : (x + B)\n\t\t=> EVAL ternaire = ?\n" << endl;
                Variable *e1;
                e1 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ////////////////////////////////////
                e = sConditionnel(sSuperieur(e1->clone(), sConstante()),
                                    sSomme(e1->clone(), sConstante()),
                                    sProduit(e1->clone(), sConstante()));

                s = e->afficher();
                exe << s << "\n=> EVAL ternaire" << question << "\n";

                d = e->eval();
                res << s << "\n=> EVAL ternaire" << result << d << "\n";

                printContent(s, "\n=> EVAL ternaire" + result, d);

                break;
            }
            case 7:
            {
                cout << "\n\t\tx = A\n\t\tif ((x > D)) {\n\t\t\t(x <- (x + C))\n\t\t} else {\n\t\t\t(x <- (x * B))\n\t\t}\n\t\t=> EVAL if then else = ?\n" << endl;
                Variable *e1;
                e1 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ////////////////////////////////////
                e = sIfThenElse(sSuperieur(e1->clone(), sConstante()),
                                    sAffectation(e1, sSomme(e1->clone(), sConstante())),
                                    sAffectation(e1, sProduit(e1->clone(), sConstante())));

                s = e->afficher();
                exe << s << "\n=> EVAL if then else" << question << "\n";

                d = e->eval();
                res << s << "\n=> EVAL if then else" << result << d << "\n";

                printContent(s, "\n=> EVAL if then else" + result, d);

                break;
            }
            case 8:
            {
                cout << "\n\t\tx = A\n\t\ty = B\n\t\t{\n\t\t\t(x <- (x + C))\n\t\t\tif ((x > F)) {\n\t\t\t(x <- (x + E))\n\t\t} else {\n\t\t\t(x <- (x * D))\n\t\t}\n\t\t\t(y <- (x + G))\n\t\t}\n\t\t=> EVAL bloc = ?\n" << endl;

                Variable *e1; //x
                e1 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ///////////////////////////////
                Variable *e2; //y
                e2 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ///////////////////////////////
                Bloc* b;
                b = sBloc("b1", sAffectation(e1, sSomme(e1->clone(), sConstante())));
                b->add(sIfThenElse(sSuperieur(e1->clone(), sConstante()),
                                    sAffectation(e1, sSomme(e1->clone(), sConstante())),
                                    sAffectation(e1, sProduit(e1->clone(), sConstante()))));



                b->add(sAffectation(e2, sSomme(e1->clone(), sConstante())));

                s = b->afficher();
                exe << s << "\n=> EVAL bloc" << question << "\n";

                d = b->eval();
                res << s << "\n=> EVAL bloc" << result << d << "\n";

                printContent(s, "\n=> EVAL bloc" + result, d);

                break;
            }
            case 9:
            {
                cout << "\n\t\tx = A\n\t\ti = B\n\t\tfor ((i <- E); (x > (i + D)); (i <- (i + C))) {\n\t\t\t(res <- (res * i))\n\t\t}\n\t\t=>EVAL pour = ?\n" << endl;

                Variable *e1; //x
                e1 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ///////////////////////////////
                Variable *e2; //i
                e2 = sVariable();

                s = e1->afficher();
                d = e1->eval();

                exe << s << result << d << "\n";
                res << s << result << d << "\n";
                ///////////////////////////////
                Variable *e3; //res
                e3 = new Variable("res", 1);
                ///////////////////////////////
                e = sPour(sAffectation(e2, sConstante()),
                            sSuperieur(e1->clone(), sSomme(e2->clone(), sConstante())),
                            sAffectation(e2, sSomme(e2->clone(), sConstante())),
                            sAffectation(e3, sProduit(e3->clone(), e2->clone())));

                s = e->afficher();
                exe << s << "\n=> EVAL pour" << question << "\n";

                d = e->eval();
                res << s << "\n=> EVAL pour" << result << d << "\n";

                printContent(s, "\n=> EVAL bloc" + result, d);

                break;
            }
            case 0:
            {
                s = "******************************************************************\n\n";
                exe << s;
                res << s;
                break;
            }
            case 999:
            {
                cout << "Sauvegarder des fichiers et quitter......" << endl;
                exe.close();
                res.close();
                break;
            }
            default:
                cout << "Le cas inconnu......" << endl;
                break;
        }
    } while (choice != 999);

    return 0;
}
Variant SimpleFunctionCallExpression::eval(VariableEnvironment &env) const {
  SET_LINE;
  String name(m_name->get(env));
  String originalName = name;
  bool renamed = false;

  // handling closure
  if (name[0] == '0') {
    const char *id = strchr(name.data(), ':');
    if (id) {
      int pos = id - name.data();
      String sid = name.substr(pos + 1);
      String function = name.substr(0, pos);
      const Function *fs = RequestEvalState::findFunction(function.data());
      if (fs) {
        const FunctionStatement *fstmt =
          dynamic_cast<const FunctionStatement *>(fs);
        if (fstmt) {
          ObjectData *closure = (ObjectData*)sid.toInt64();
          return ref(fstmt->invokeClosure(Object(closure), env, this));
        }
      }
    }
  } else {
    name = get_renamed_function(name, &renamed);
    if (name[0] == '\\') {
      name = name.substr(1); // try namespaced function first
      renamed = true;
    }
  }

  // fast path for interpreted fn
  const Function *fs = RequestEvalState::findFunction(name.data());
  if (fs) {
    return ref(fs->directInvoke(env, this));
  }

  if (originalName[0] == '\\') {
    name = originalName.lastToken('\\');
    name = get_renamed_function(name, &renamed);
    renamed = true;
    fs = RequestEvalState::findFunction(name.data());
    if (fs) {
      return ref(fs->directInvoke(env, this));
    }
  }

  const CallInfo *cit1;
  void *vt1;
  get_call_info_or_fail(cit1, vt1, name);
  ArrayInit ai(m_params.size(), true);
  bool invokeClosure = false;
  Variant arg0;
  for (unsigned int i = 0; i < m_params.size(); ++i) {
    Expression *param = m_params[i].get();
    if (i == 0 &&
        (name == "call_user_func" || name == "call_user_func_array")) {
      ASSERT(!cit1->mustBeRef(i) && !cit1->isRef(i));
      arg0 = param->eval(env);
      if (arg0.instanceof("closure")) {
        invokeClosure = true;
      } else {
        ai.set(arg0);
      }
      continue;
    }
    if (cit1->mustBeRef(i)) {
      ai.setRef(param->refval(env));
    } else if (cit1->isRef(i)) {
      ai.setRef(param->refval(env, 0));
    } else {
      ai.set(param->eval(env));
    }
  }
  if (invokeClosure) {
    String sfunction = arg0.toString();
    const char *id = sfunction.data();
    assert(id[0] == '0');
    id = strchr(id, ':');
    ASSERT(id);
    int pos = id - sfunction.data();
    String sid = sfunction.substr(pos + 1);
    sfunction = sfunction.substr(0, pos);
    const Function *fs = RequestEvalState::findFunction(sfunction.data());
    const FunctionStatement *fstmt =
      dynamic_cast<const FunctionStatement *>(fs);
    ObjectData *closure = (ObjectData*)sid.toInt64();
    return ref(fstmt->invokeClosure(Object(closure), env, this));
  }
  return (cit1->getFunc())(vt1, Array(ai.create()));
}