mirror of https://github.com/exaloop/codon.git
Fix #189
parent
8fb65dcaa6
commit
2790e5db47
|
@ -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;) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue