Added nested functions to the Fun parser. (0.40.0)
This commit is contained in:
parent
59484f5a85
commit
be75ee47f8
7 changed files with 130 additions and 15 deletions
69
examples/functions/nested_functions.fun
Executable file
69
examples/functions/nested_functions.fun
Executable file
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Demonstrates nested functions that are only visible inside the
|
||||
* outer function where they are defined. This example avoids
|
||||
* capturing outer variables (closures) and instead passes values
|
||||
* explicitly, which works with the current implementation.
|
||||
*
|
||||
* Added: 2026-04-02
|
||||
*/
|
||||
|
||||
print("=== Nested functions (local to outer function) ===")
|
||||
|
||||
// Outer function defines two inner helpers that are only usable inside outer
|
||||
fun outer(a, b)
|
||||
// Defined inside outer; not visible as a global symbol
|
||||
fun times3(x)
|
||||
return x * 3
|
||||
end
|
||||
|
||||
// Another local helper; also only visible within outer
|
||||
fun sum_plus1(x, y)
|
||||
return x + y + 1
|
||||
end
|
||||
|
||||
// Use the local helpers
|
||||
t1 = times3(a)
|
||||
t2 = times3(b)
|
||||
return sum_plus1(t1, t2)
|
||||
end
|
||||
|
||||
print("outer(2, 5) -> expected 2*3 + 5*3 + 1 = 22")
|
||||
print(outer(2, 5))
|
||||
|
||||
print("")
|
||||
print("=== Deeper nesting without variable capture ===")
|
||||
|
||||
// Demonstrates function-inside-function-inside-function.
|
||||
// Each level avoids referencing outer locals directly; values
|
||||
// are threaded through parameters instead.
|
||||
fun demo_deep(n)
|
||||
fun one(x)
|
||||
fun two(y)
|
||||
fun three(z)
|
||||
return z + 1
|
||||
end
|
||||
return three(y) + 1
|
||||
end
|
||||
return two(x) + 1
|
||||
end
|
||||
return one(n)
|
||||
end
|
||||
|
||||
print("demo_deep(4) -> expected 7")
|
||||
print(demo_deep(4))
|
||||
|
||||
/*
|
||||
Expected output:
|
||||
=== Nested functions (local to outer function) ===
|
||||
outer(2, 5) -> expected 2*3 + 5*3 + 1 = 22
|
||||
22
|
||||
|
||||
=== Deeper nesting without variable capture ===
|
||||
demo_deep(4) -> expected 7
|
||||
7
|
||||
*/
|
||||
22
examples/snippets/nested.fun
Normal file
22
examples/snippets/nested.fun
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Demonstrates nested functions that are only visible inside the
|
||||
* outer function where they are defined. This example avoids
|
||||
* capturing outer variables (closures) and instead passes values
|
||||
* explicitly, which works with the current implementation.
|
||||
*
|
||||
* Added: 2026-04-02
|
||||
*/
|
||||
|
||||
fun outer(x)
|
||||
fun inner(y)
|
||||
return y * 2
|
||||
end
|
||||
return inner(x) + inner(3)
|
||||
end
|
||||
|
||||
print(outer(5))
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
#include <net/cgi.fun>
|
||||
|
||||
cgi = CGI()
|
||||
pairs = cgi._parse_urlencoded("a=1&b=2&c=3")
|
||||
i = 0
|
||||
n = len(pairs)
|
||||
while (i < n)
|
||||
p = pairs[i]
|
||||
print(to_string(p[0]) + "=" + to_string(p[1]))
|
||||
i = i + 1
|
||||
|
|
@ -1,5 +1,17 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Demonstrates nested functions that are only visible inside the
|
||||
* outer function where they are defined. This example avoids
|
||||
* capturing outer variables (closures) and instead passes values
|
||||
* explicitly, which works with the current implementation.
|
||||
*
|
||||
* Added: 2026-04-02
|
||||
*/
|
||||
|
||||
#include <net/cgi.fun>
|
||||
|
||||
cgi = CGI()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue