本文整理汇总了C++中Code::execute方法的典型用法代码示例。如果您正苦于以下问题:C++ Code::execute方法的具体用法?C++ Code::execute怎么用?C++ Code::execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Code
的用法示例。
在下文中一共展示了Code::execute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char** argv) {
const char* script = 0;
std::string mode = INTERPRETE;
for (int32_t i = 1; i < argc; i++) {
if (string(argv[i]) == "-d") {
mode = DISASM;
} else if (string(argv[i]) == "-j") {
mode = JIT;
} else {
script = argv[i];
}
}
if (script == 0) {
cout << "Usage: interpreter [-d | -j] <source_file>" << endl;
return WRONG_ARG_COUNT;
}
const char* expr = loadFile(script);
if (expr == 0) {
cout << "Cannot read file: " << script << endl;
return CANNOT_READ_SOURCE;
}
Translator* translator = new BytecodeTranslatorImpl();
Code* code = 0;
Status* translateStatus = translator->translate(expr, &code);
if (translateStatus == 0) {
//todo
} else if (translateStatus->isError()) {
uint32_t position = translateStatus->getPosition();
uint32_t line = 0, offset = 0;
positionToLineOffset(expr, position, line, offset);
cout << "Cannot translate expression: expression at " << line << ", " << offset << "; "
<< "error '" << translateStatus->getError().c_str() << "'" << endl;
} else {
assert(code != 0);
if (mode == DISASM) {
code->disassemble(cout);
} else if (mode == JIT) {
} else {
vector<Var*> t;
code->execute(t);
}
delete code;
}
delete translateStatus;
delete translator;
return 0;
}
示例2: step
bool Thread::step()
{
if (state == RUN)
{
Thread::Frame& frame = frames.back();
ThunkPrototype* thunk = frame.thunk->thunkPrototype();
size_t nextInstr = frame.nextInstr;
Code* code = thunk->body[nextInstr];
frame.nextInstr++;
// Uncomment to get *verbose* debug info on scripting
/*cout << thunk;
for (size_t i = 0; i < frames.size(); ++i)
cout << "[" << frames[i].stack.size() << "]";
cout << " " << usl->debug.find(thunk, nextInstr) << ": ";
code->dump(cout);
cout << endl;*/
code->execute(this);
while (true)
{
Thread::Frame& frame = frames.back();
if (frame.nextInstr < frame.thunk->thunkPrototype()->body.size())
break;
Value* retVal = frame.stack.back();
frames.pop_back();
if (!frames.empty())
{
frames.back().stack.push_back(retVal);
}
else
{
#ifdef DEBUG_USL
retVal->dump(cout);
cout << endl;
#endif
state = STOP;
break;
}
}
return true;
}
else
{
return false;
}
}
示例3: main
int main(int argc, char **argv) {
string impl = "printer";
const char *script = NULL;
for (int32_t i = 1; i < argc; i++) {
if (string(argv[i]) == "-j") {
impl = "jit";
} else {
script = argv[i];
}
}
Translator *translator = Translator::create(impl);
if (translator == 0) {
return 1;
}
const char *expr = "double x; double y;"
"x += 8.0; y = 2.0;"
"print('Hello, x=',x,' y=',y,'\n');"
;
bool isDefaultExpr = true;
if (script != NULL) {
expr = loadFile(script);
if (expr == 0) {
printf("Cannot read file: %s\n", script);
return 1;
}
isDefaultExpr = false;
}
Code *code = 0;
Status *translateStatus = translator->translate(expr, &code);
if (translateStatus->isError()) {
uint32_t position = translateStatus->getPosition();
uint32_t line = 0, offset = 0;
positionToLineOffset(expr, position, line, offset);
printf("Cannot translate expression: expression at %d,%d; "
"error '%s'\n",
line, offset,
translateStatus->getError().c_str());
} else {
if (true) { return 0; }
assert(code != 0);
vector<Var *> vars;
if (isDefaultExpr) {
Var *xVar = new Var(VT_DOUBLE, "x");
Var *yVar = new Var(VT_DOUBLE, "y");
vars.push_back(xVar);
vars.push_back(yVar);
xVar->setDoubleValue(42.0);
}
Status *execStatus = code->execute(vars);
if (execStatus->isError()) {
printf("Cannot execute expression: error: %s\n",
execStatus->getError().c_str());
} else {
if (isDefaultExpr) {
printf("x evaluated to %f\n", vars[0]->getDoubleValue());
for (uint32_t i = 0; i < vars.size(); i++) {
delete vars[i];
}
}
}
delete code;
delete execStatus;
}
delete translateStatus;
delete translator;
if (!isDefaultExpr) {
delete [] expr;
}
return 0;
}
示例4: main
int main(int argc, char** argv) {
if (argc == 1) {
cout << "enter path to source code as parameter" << endl;
return 1;
}
ifstream ifs (argv[1]);
if (!ifs) {
cout << "can not open file: " << argv[1] << endl;
return 1;
}
string source(
(istreambuf_iterator<char>(ifs)),
istreambuf_iterator<char>()
);
Parser parser;
Status* status = parser.parseProgram(source);
if (status) {
if (status->isError()) {
cout << status->getError() << endl;
delete status;
return 1;
}
}
Code* code = new InterpreterCodeImpl(cout);
CodeBuilderVisitor visitor(code);
visitor.start(parser.top());
if (argc > 2) {
code->disassemble();
}
if (argc == 3) {
cout << endl << "---------------------" << endl;
}
if (argc == 4) {
return 0;
}
vector<Var*> vars;
Status* exec_status = code->execute(vars);
if (exec_status) {
cout << exec_status->getError() << endl;
delete exec_status;
return 1;
} else {
// cout << endl;
}
delete exec_status;
delete status;
return 0;
}
示例5: main
//.........这里部分代码省略.........
// const char* expr = "int a; a = 6; int b; b = 3;"
// "function void mprint(int t) { print(b); function void add(int t) { print (b);} }"
// "mprint(b);";
// const char* expr = "int t; t = 4;"
// "function int strlen(int t) native 'strlen'; function int strlen2(int t) native 'strlen2'; strlen2(t); strlen(t);";
// const char* expr = "int a; a = 8; while (5 > 7) { int a; a = 7; print (a); }";
// const char* expr = "int i; i = 0; for (i in -3..4) {"
// "print(i);"
// "}";
// const char* expr = "int a; a = 31; if (a > 0)"
// "{ int a; a = 3; print(a);} "
// "else {int a; a = 4; print(a);}";
// const char* expr =
// "print(12345678, '\n');"
// "print(-1234567890123456, '\n');"
// "print(1.0, '\n');"
// "print(-2.0, '\n');"
// "print(1e8, '\n');"
// "print(1e-8, '');";
// const char * expr = "print('asd' + 'asdsa');";
// const char* expr =
// "function int fact(int n) {\
// if (n < 3) {\
// return n;\
// }\
// return n*fact(n-1) + fact(n-2);\
// }\
// fact(1);";
//const char* expr = "int l; function int add(int a){ return 8;} l = 3; l*add(l); ";
cout << "expr = " << expr <<endl;
bool isDefaultExpr = true;
if (script != NULL) {
expr = loadFile(script);
if (expr == 0) {
printf("Cannot read file: %s\n", script);
return 1;
}
isDefaultExpr = false;
}
Code* code = 0;
Status* translateStatus = translator->translate(expr, &code);
if (translateStatus->isError()) {
uint32_t position = translateStatus->getPosition();
uint32_t line = 0, offset = 0;
positionToLineOffset(expr, position, line, offset);
printf("Cannot translate expression: expression at %d,%d; "
"error '%s'\n",
line, offset,
translateStatus->getErrorCstr());
} else {
if (impl != "intepreter") {
assert(code != 0);
vector<Var*> vars;
if (isDefaultExpr) {
Var* xVar = new Var(VT_DOUBLE, "x");
Var* yVar = new Var(VT_DOUBLE, "y");
vars.push_back(xVar);
vars.push_back(yVar);
xVar->setDoubleValue(42.0);
}
Status* execStatus = code->execute(vars);
if (execStatus->isError()) {
printf("Cannot execute expression: error: %s\n",
execStatus->getErrorCstr());
} else {
if (isDefaultExpr) {
printf("x evaluated to %f\n", vars[0]->getDoubleValue());
for (uint32_t i = 0; i < vars.size(); i++) {
delete vars[i];
}
}
}
delete code;
delete execStatus;
}
}
delete translateStatus;
delete translator;
if (!isDefaultExpr) {
delete [] expr;
}
return 0;
}