pull/185/head
Ibrahim Numanagić 2023-02-02 10:05:44 -08:00
parent 8fb65dcaa6
commit 2790e5db47
2 changed files with 24 additions and 1 deletions

View File

@ -282,8 +282,20 @@ void SimplifyVisitor::visit(FunctionStmt *stmt) {
// Expression to be used if function binding is modified by captures or decorators
ExprPtr finalExpr = nullptr;
// If there are captures, replace `fn` with `fn(cap1=cap1, cap2=cap2, ...)`
if (!captures.empty())
if (!captures.empty()) {
finalExpr = N<CallExpr>(N<IdExpr>(stmt->name), partialArgs);
// Add updated self reference if case function is recursive!
auto pa = partialArgs;
for (auto &a : pa) {
if (!a.name.empty())
a.value = N<IdExpr>(a.name);
else
a.value = clone(a.value);
}
f->suite = N<SuiteStmt>(
N<AssignStmt>(N<IdExpr>(rootName), N<CallExpr>(N<IdExpr>(rootName), pa)),
suite);
}
// Parse remaining decorators
for (auto i = stmt->decorators.size(); i-- > 0;) {

View File

@ -1232,3 +1232,14 @@ def foo():
foo()
#! name 'x' is not defined
#! name 'b' is not defined
#%% capture_recursive,barebones
def f(x: int) -> int:
z = 2 * x
def g(y: int) -> int:
if y == 0:
return 1
else:
return g(y - 1) * z
return g(4)
print(f(3)) #: 1296