本文整理汇总了C++中Interpreter::eval方法的典型用法代码示例。如果您正苦于以下问题:C++ Interpreter::eval方法的具体用法?C++ Interpreter::eval怎么用?C++ Interpreter::eval使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Interpreter
的用法示例。
在下文中一共展示了Interpreter::eval方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
using namespace lisp;
Parser parser;
Interpreter interpreter;
while (std::cin) {
std::string expr;
std::getline(std::cin, expr, ';');
if (expr.empty()) {
continue;
}
try {
std::istringstream is(expr);
List parsed = parser.parse(is);
std::cout << interpreter.eval(parsed) << "\n";
} catch (parse_error error) {
std::cerr << "parse error: " << error.what() << "\n";
} catch (eval_error error) {
std::cerr << "evaluation error: " << error.what() << "\n";
}
}
return 0;
}
示例2: main
int main() {
TEST_START(19);
Interpreter universe;
universe.eval(
"sub first { return 1 }\n"
"sub second { return scalar @_ }\n"
"sub third { return $_[0] }\n"
"sub fourth { return ++$_[0] }\n"
"sub fifth { @_ }\n"
);
Ref<Code> first = universe.eval("\\&first");
ok(first(), "first()");
is(first(), 1, "first() == 1");
Ref<Code> second = universe.eval("\\&second");
ok(second(1), "second(1)");
ok(second("foo"), "second(\"foo\")");
is(second(1), 1, "second(1) == 1");
is(second(1, 1), 2, "second(1, 1) == 2");
Array more = universe.list(1, 2, 3);
is(second(more), 3, "second(more) == 3");
is(second(1, 1, more), 5, "second(1, 1, more) == 5");
Ref<Code> third = universe.eval("\\&third");
ok(third(1), "third(1)");
ok(third("foo"), "third(\"foo\")");
is(third(1), 1, "third(1) == 1");
is(third ("foo"), "foo", "third(\"foo\") == \"foo\"");
is(third(more), 1, "third(more) == 1");
Ref<Code> fourth = universe.eval("\\&fourth");
Integer value = universe.value_of(1);
note("value = 1");
is(fourth(value), 2, "fourth(value) == 2");
is(value, 2, "value == 2");
is(second.list(1)[0], 1, "second(1) == 1");
is(second.list(1, 1)[0], 2, "second(1, 1) == 2");
Ref<Code> fifth = universe.eval("\\&fifth");
is(fifth.list(1, 2, 3).length(), 3u, "fifth(1, 2, 3).length() == 3");
is(fifth.list(1, 2, 3)[2], 3, "list[2] == 3");
TEST_END;
}