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

Lmabda: execute review comments

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