Commit 39afd252 authored by Travis Hance's avatar Travis Hance

basic implementation of `exec code in dict`

parent ed5f850c
......@@ -888,12 +888,12 @@ Value ASTInterpreter::visit_print(AST_Print* node) {
}
Value ASTInterpreter::visit_exec(AST_Exec* node) {
RELEASE_ASSERT(!node->globals, "do not support exec with globals or locals yet");
assert(!node->locals);
// TODO implement the locals and globals arguments
Box* code = visit_expr(node->body).o;
exec(code);
Box* globals = node->globals == NULL ? NULL : visit_expr(node->globals).o;
Box* locals = node->locals == NULL ? NULL : visit_expr(node->locals).o;
exec(code, globals, locals);
return Value();
}
......
......@@ -379,8 +379,17 @@ Box* eval(Box* boxedCode) {
return evalOrExec<AST_Expression>(parsedExpr, body, module, boxedLocals);
}
Box* exec(Box* boxedCode) {
Box* boxedLocals = fastLocalsToBoxedLocals();
Box* exec(Box* boxedCode, Box* globals, Box* locals) {
// TODO boxedCode is allowed to be a tuple
// TODO need to handle passing in globals
if (locals == NULL) {
locals = globals;
}
if (locals == NULL) {
locals = fastLocalsToBoxedLocals();
}
BoxedModule* module = getCurrentModule();
// TODO same issues as in `eval`
......@@ -390,7 +399,7 @@ Box* exec(Box* boxedCode) {
AST_Suite* parsedSuite = new AST_Suite(std::move(parsedModule->interned_strings));
parsedSuite->body = parsedModule->body;
return evalOrExec<AST_Suite>(parsedSuite, parsedSuite->body, module, boxedLocals);
return evalOrExec<AST_Suite>(parsedSuite, parsedSuite->body, module, locals);
}
// If a function version keeps failing its speculations, kill it (remove it
......
......@@ -37,7 +37,7 @@ void compileAndRunModule(AST_Module* m, BoxedModule* bm);
// will we always want to generate unique function names? (ie will this function always be reasonable?)
CompiledFunction* cfForMachineFunctionName(const std::string&);
extern "C" Box* exec(Box* boxedCode);
extern "C" Box* exec(Box* boxedCode, Box* globals, Box* locals);
extern "C" Box* eval(Box* boxedCode);
}
......
......@@ -1711,15 +1711,29 @@ private:
}
void doExec(AST_Exec* node, UnwindInfo unw_info) {
// TODO locals and globals
RELEASE_ASSERT(!node->globals, "do not support exec with globals or locals yet");
assert(!node->locals);
CompilerVariable* body = evalExpr(node->body, unw_info);
ConcreteCompilerVariable* cbody = body->makeConverted(emitter, body->getBoxType());
llvm::Value* vbody = body->makeConverted(emitter, body->getBoxType())->getValue();
body->decvref(emitter);
emitter.createCall(unw_info, g.funcs.exec, cbody->getValue());
llvm::Value* vglobals;
if (node->globals) {
CompilerVariable* globals = evalExpr(node->globals, unw_info);
vglobals = globals->makeConverted(emitter, globals->getBoxType())->getValue();
globals->decvref(emitter);
} else {
vglobals = embedConstantPtr(NULL, g.llvm_value_type_ptr);
}
llvm::Value* vlocals;
if (node->locals) {
CompilerVariable* locals = evalExpr(node->locals, unw_info);
vlocals = locals->makeConverted(emitter, locals->getBoxType())->getValue();
locals->decvref(emitter);
} else {
vlocals = embedConstantPtr(NULL, g.llvm_value_type_ptr);
}
emitter.createCall3(unw_info, g.funcs.exec, vbody, vglobals, vlocals);
}
void doPrint(AST_Print* node, UnwindInfo unw_info) {
......
d = {}
exec "a = 5" in d
print d['a']
def f():
d2 = {}
exec "b = 6" in d2
print d2['b']
f()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment