Commit 4c5996eb authored by Marius Wachtler's avatar Marius Wachtler

Lmabda: execute review comments

parent 87ab64e6
......@@ -67,7 +67,11 @@ public:
return true;
}
bool visit_lambda(AST_Lambda* node) { return true; }
bool visit_lambda(AST_Lambda* node) {
for (auto* d : node->args->defaults)
d->accept(this);
return true;
}
bool visit_name(AST_Name* node) {
if (node->ctx_type == AST_TYPE::Load)
......
......@@ -235,15 +235,21 @@ public:
}
virtual bool visit_lambda(AST_Lambda* node) {
assert(node == orig_node);
for (AST_expr* e : node->args->args)
e->accept(this);
if (node->args->vararg.size())
doWrite(node->args->vararg);
if (node->args->kwarg.size())
doWrite(node->args->kwarg);
node->body->accept(this);
if (node == orig_node) {
for (AST_expr* e : node->args->args)
e->accept(this);
if (node->args->vararg.size())
doWrite(node->args->vararg);
if (node->args->kwarg.size())
doWrite(node->args->kwarg);
node->body->accept(this);
} else {
for (auto* e : node->args->defaults)
e->accept(this);
(*map)[node] = new ScopingAnalysis::ScopeNameUsage(node, cur);
collect(node, map);
}
return true;
}
......@@ -293,15 +299,13 @@ static std::vector<ScopingAnalysis::ScopeNameUsage*> sortNameUsages(ScopingAnaly
}
void ScopingAnalysis::processNameUsages(ScopingAnalysis::NameUsageMap* usages) {
typedef ScopeNameUsage::StrSet StrSet;
// Resolve name lookups:
for (const auto& p : *usages) {
ScopeNameUsage* usage = p.second;
for (StrSet::iterator it2 = usage->read.begin(), end2 = usage->read.end(); it2 != end2; ++it2) {
if (usage->forced_globals.count(*it2))
for (const auto& name : usage->read) {
if (usage->forced_globals.count(name))
continue;
if (usage->written.count(*it2))
if (usage->written.count(name))
continue;
std::vector<ScopeNameUsage*> intermediate_parents;
......@@ -310,15 +314,15 @@ void ScopingAnalysis::processNameUsages(ScopingAnalysis::NameUsageMap* usages) {
while (parent) {
if (parent->node->type == AST_TYPE::ClassDef) {
parent = parent->parent;
} else if (parent->forced_globals.count(*it2)) {
} else if (parent->forced_globals.count(name)) {
break;
} else if (parent->written.count(*it2)) {
usage->got_from_closure.insert(*it2);
parent->referenced_from_nested.insert(*it2);
} else if (parent->written.count(name)) {
usage->got_from_closure.insert(name);
parent->referenced_from_nested.insert(name);
for (ScopeNameUsage* iparent : intermediate_parents) {
iparent->referenced_from_nested.insert(*it2);
iparent->got_from_closure.insert(*it2);
iparent->referenced_from_nested.insert(name);
iparent->got_from_closure.insert(name);
}
break;
......
......@@ -75,37 +75,6 @@ const std::string SourceInfo::getName() {
}
}
/*
const std::vector<AST_expr*>& SourceInfo::getArgNames() {
static std::vector<AST_expr*> empty;
AST_arguments* args = getArgsAST();
if (args == NULL)
return empty;
return args->args;
}
const std::vector<AST_expr*>* CLFunction::getArgNames() {
if (!source)
return NULL;
return &source->getArgNames();
//////////////
const std::vector<AST_stmt*>& SourceInfo::getBody() {
assert(ast);
switch (ast->type) {
case AST_TYPE::FunctionDef:
return ast_cast<AST_FunctionDef>(ast)->body;
case AST_TYPE::Module:
return ast_cast<AST_Module>(ast)->body;
default:
RELEASE_ASSERT(0, "%d", ast->type);
}
}
*/
EffortLevel::EffortLevel initialEffort() {
if (FORCE_OPTIMIZE)
return EffortLevel::MAXIMAL;
......
......@@ -1497,16 +1497,15 @@ private:
CLFunction*& cl = made[node];
if (cl == NULL) {
SourceInfo* si
= new SourceInfo(irstate->getSourceInfo()->parent_module, irstate->getSourceInfo()->scoping, node, body);
cl = new CLFunction(args->args.size(), args->defaults.size(), args->vararg.size(),
args->kwarg.size(), si);
SourceInfo* si = new SourceInfo(irstate->getSourceInfo()->parent_module, irstate->getSourceInfo()->scoping,
node, body);
cl = new CLFunction(args->args.size(), args->defaults.size(), args->vararg.size(), args->kwarg.size(), si);
}
return cl;
}
CompilerVariable* createFunction(AST* node, ExcInfo exc_info, AST_arguments* args, const std::vector<AST_stmt*>& body)
{
CompilerVariable* createFunction(AST* node, ExcInfo exc_info, AST_arguments* args,
const std::vector<AST_stmt*>& body) {
CLFunction* cl = this->_wrapFunction(node, args, body);
std::vector<ConcreteCompilerVariable*> defaults;
......
......@@ -619,15 +619,21 @@ private:
}
AST_expr* remapLambda(AST_Lambda* node) {
if (node->args->defaults.empty()) {
return node;
}
AST_Lambda* rtn = new AST_Lambda();
rtn->lineno = node->lineno;
rtn->col_offset = node->col_offset;
rtn->args = node->args;
// remap default arguments
rtn->args->defaults.clear();
for (auto& e : node->args->defaults)
rtn->args->defaults.push_back(remapExpr(e));
rtn->args = new AST_arguments();
rtn->args->args = node->args->args;
rtn->args->vararg = node->args->vararg;
rtn->args->kwarg = node->args->kwarg;
for (auto d : node->args->defaults) {
rtn->args->defaults.push_back(remapExpr(d));
}
rtn->body = node->body;
return rtn;
......
......@@ -223,7 +223,8 @@ public:
const std::string getName();
SourceInfo(BoxedModule* m, ScopingAnalysis* scoping, AST* ast, const std::vector<AST_stmt*>& body)
: parent_module(m), scoping(scoping), ast(ast), cfg(NULL), liveness(NULL), phis(NULL), arg_names(ast), body(body) {}
: parent_module(m), scoping(scoping), ast(ast), cfg(NULL), liveness(NULL), phis(NULL), arg_names(ast),
body(body) {}
};
typedef std::vector<CompiledFunction*> FunctionList;
......
s = lambda x: x**2
print s(8), s(100)
s = lambda x=5: x**2
print s(8), s(100), s()
for i in range(10):
print (lambda x, y: x < y)(i, 5)
t = lambda s: " ".join(s.split())
print t("test \tstr\ni\n ng")
def T(y):
return (lambda x: x < y)
print T(10)(1), T(10)(20)
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