pocketlang/tests/lang/closure.pk
Thakee Nathees 88c45cccac function and else if keywords are changed.
- `func` keyword has change to `function`.
- `elsif` changed to `else if`
2022-04-17 07:33:40 +05:30

88 lines
1.2 KiB
Plaintext

## Simple upvalue.
def f1
local = "foo"
return function
return local
end
end
assert(f1()() == "foo")
def add3(x)
return function(y)
return function(z)
return x + y + z
end
end
end
assert(add3(1)(2)(3) == 6);
assert(add3(7)(6)(4) == 17);
## Upvalue external to the inner function.
def f2
local = "bar"
return function
fn = function
return local
end
return fn
end
end
assert(f2()()() == "bar")
## Check if upvalues are shared between closures.
def f3
local = "baz"
_fn1 = function(x)
local = x
end
_fn2 = function
return local
end
return [_fn1, _fn2]
end
fns = f3()
fns[0]("qux")
assert(fns[1]() == "qux")
def f4
a = []
x = 10
for i in 0..2
j = i ## 'i' is shared, but 'j' doesn't
list_append(
a,
function
return x + j
end
)
end
x = 20
return a
end
a = f4()
assert(a[0]() == 20)
assert(a[1]() == 21)
def f5
l1 = 12
return function ## c1
l2 = 34
return function ## c2
l3 = 56
return function ## c3
return function ## c4
return function ## c5
return l1 + l2 + l3
end
end
end
end
end
end
print(f5()()()()()() == 102)
print('All TESTS PASSED')