本文整理汇总了C++中Evaluator::eval方法的典型用法代码示例。如果您正苦于以下问题:C++ Evaluator::eval方法的具体用法?C++ Evaluator::eval怎么用?C++ Evaluator::eval使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Evaluator
的用法示例。
在下文中一共展示了Evaluator::eval方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ui
void ui()
{
Evaluator eval;
std::string input;
std::cout << "\ntype an expression, \"exit\" to end\n";
while (true)
{
std::cout << ">>> ";
std::getline(std::cin, input);
if (input[0] == 'e' || input[0] == 'E')
break;
try
{
std::cout << eval.eval(input) << std::endl;
}
catch (std::invalid_argument err)
{
std::cout << "Error: " << err.what() << std::endl;
}
}
}
示例2: main
int main()
{
Evaluator eval;
vector<string> expression = { "1+2*3","2+2^2*3" ,"1==2", "1+3 > 2", "(4>=4) && 0", "(1+2)*3" }; //<--expressions we wish to evaluate go here
vector<string>::iterator exprItr;
for (exprItr = expression.begin();
exprItr != expression.end();
exprItr++)
{
try
{
int result = eval.eval(*exprItr);
cout << eval.infix_expression.c_str() << " = " << result << endl << endl;
}
catch (Syntax_Error e)
{
cout << eval.infix_expression.c_str() << endl << e.what() << " " << "@ char: " << eval.position << endl << endl;
}
}
system("pause");
return 0;
}
示例3: evaluate
int evaluate(T& src) {
Evaluator e;
try {
e.eval(src);
return 0;
} catch (const Error& e) {
std::cerr << DASHES << std::endl;
std::cerr << "Unhandled exception!" << std::endl << std::endl;
std::cerr << e.what() << std::endl;
std::cerr << DASHES << std::endl;
return 1;
}
}
示例4: runtime_error
Expr*
get_length(Layout_decl const* layout)
{
Evaluator eval;
Expr* e = 0;
for (Decl* d : layout->fields()) {
Type const* t1 = d->type();
// If member is constant, just add in the constant value
if (has_constant_length(t1))
e = add(e, make_int(precision(t1)));
// Otherwise, we have to form a call to the function
// that would compute this type.
else {
// FIXME: Do this right!
throw std::runtime_error("unimplemented dynamic length calc.");
e = add(e, zero());
}
}
// Compute ceil(e / 8).
Expr* b = make_int(8); // bits per byte
Expr* r = div(sub(add(e, b), one()), b);
// Try folding the result. If it works, good. If not,
// just return the previously computed expression.
//
// TODO: Maximally reduce the expression so that we only
// add the constant bits to the non-constant bits. Since
// addition is associative and commutative, we can
// partition the sequence of terms into constants and
// non-constants, and then sum the constant parts.
try {
Value v = eval.eval(r);
if (v.is_integer())
return make_int(v.get_integer());
else
throw std::runtime_error("failed to synth length");
}
catch(...) {
return r;
}
}
示例5: Literal_expr
Expr*
gather(Expr_seq const& subkeys)
{
// maintain the largest allowable key buffer
uint512_t buf = 0;
Evaluator ev;
// maintain the position to start writing
int pos = 0;
for (auto subkey : subkeys) {
// get the precision of the subkey
int prec = precision(subkey->type());
Value const& val = ev.eval(subkey);
// FIXME: for now we're only dealing with unsigned integer values
assert(val.is_integer());
std::stringstream ss;
ss << val.get_integer().decimal_str();
uint512_t i = 0;
ss >> i;
// shift the integer over by the amount already written
i = i << pos;
// add the length of the current integer to the pos
pos += prec;
// log-and the integer into the buffer
buf |= i;
}
char* bytes = new char[pos / 8];
char* key = reinterpret_cast<char*>(&buf);
std::copy(key, key + (pos / 8), bytes);
Array_value arr { bytes, (size_t) pos / 8 };
Type const* z = get_integer_type();
Expr* n = new Literal_expr(z, arr.len + 1);
// Create the array type.
Type const* c = get_character_type();
Type const* t = get_array_type(c, n);
return new Literal_expr(t, arr);
}
示例6:
// Evaluate the given expression.
inline Value
evaluate(Expr const* e)
{
Evaluator ev;
return ev.eval(e);
}