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
73
examples/functions/functions_test.fun
Executable file
73
examples/functions/functions_test.fun
Executable file
|
|
@ -0,0 +1,73 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*/
|
||||
|
||||
/*
|
||||
* Function Definition Testing
|
||||
*
|
||||
* Covers:
|
||||
* - Basic function syntax (`def`)
|
||||
* - Handling multiple parameter types
|
||||
* - Function return value testing
|
||||
* - Function composition examples
|
||||
*/
|
||||
|
||||
// Functions test: definitions, calls, parameters, locals, returns, and if/else.
|
||||
|
||||
print("=== Functions test start ===")
|
||||
|
||||
fun add(a, b)
|
||||
return a + b
|
||||
|
||||
fun abs(x)
|
||||
if (x < 0)
|
||||
return -x
|
||||
else
|
||||
return x
|
||||
|
||||
fun sum3(a, b, c)
|
||||
// local typed declaration
|
||||
number t = a + b
|
||||
return t + c
|
||||
|
||||
fun id(x)
|
||||
return x
|
||||
|
||||
// Calls in expressions
|
||||
print(add(2, 3)) // expect 5
|
||||
print(add(10, -3)) // expect 7
|
||||
print(abs(-7)) // expect 7
|
||||
print(abs(4)) // expect 4
|
||||
print(sum3(1, 2, 3)) // expect 6
|
||||
print(add(add(1, 2), 3)) // expect 6
|
||||
print(id(42)) // expect 42
|
||||
|
||||
// Call as statement (result dApache-2.0arded)
|
||||
add(100, 23)
|
||||
|
||||
// Globals interacting with functions
|
||||
number n = 5
|
||||
print(add(n, 10)) // expect 15
|
||||
|
||||
print("=== Functions test end ===")
|
||||
|
||||
/* Expected output:
|
||||
=== Functions test start ===
|
||||
5
|
||||
7
|
||||
7
|
||||
4
|
||||
6
|
||||
6
|
||||
42
|
||||
15
|
||||
=== Functions test end ===
|
||||
*/
|
||||
|
||||
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
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue