本文整理汇总了C++中Expression::eval方法的典型用法代码示例。如果您正苦于以下问题:C++ Expression::eval方法的具体用法?C++ Expression::eval怎么用?C++ Expression::eval使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression::eval方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: eval
void eval() {
if (cond->check(map)) {
_then->eval(map);
}
else {
_else->eval(map);
}
}
示例2: interpret
int interpret(string code) {
TokenList tokens = Lexer(code).tokenize();
Expression * exprTree = Parser(tokens).parse();
int res = exprTree->eval();
delete exprTree;
return res;
}
示例3: testPour2
// 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;
}
示例4: testPour1
// 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();
}
示例5: testBinaire
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;
}
示例6: touch
/// 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;
}
示例7: init_cell_orientations
//-----------------------------------------------------------------------------
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);
}
}
示例8: eval
virtual T eval(const ExpressionContext<T> &ctx) const override
{
return e1->eval(ctx) - e2->eval(ctx);
}
示例9: eval
double Block::eval()
{
Expression * tmp = _expressions.back();
return tmp->eval();
}
示例10: testEval
void testEval() {
Expression * expr = new Multiply(new Number(3), new Multiply(new Number(4), new Number(5)));
check("eval", expr->eval() == 60);
}
示例11: creation
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();
//.........这里部分代码省略.........
示例12: eval
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()));
}