lang: Add more tests for function
This commit is contained in:
2
lang/interpret_test/TestAstFunc2/func-math1.output
Normal file
2
lang/interpret_test/TestAstFunc2/func-math1.output
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Vertex: test[sq1: 13]
|
||||||
|
Vertex: test[sq2: 42]
|
||||||
17
lang/interpret_test/TestAstFunc2/func-math1/main.mcl
Normal file
17
lang/interpret_test/TestAstFunc2/func-math1/main.mcl
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import "fmt"
|
||||||
|
|
||||||
|
# function statement
|
||||||
|
func sq1($x, $y) {
|
||||||
|
$y + $x * $x
|
||||||
|
}
|
||||||
|
|
||||||
|
# function expression
|
||||||
|
$sq2 = func($x, $y) {
|
||||||
|
$y + $x * $x
|
||||||
|
}
|
||||||
|
|
||||||
|
$x1 = sq1(3, 4) # 3^2 + 4 = 13
|
||||||
|
$x2 = $sq2(7, -7) # 7^2 + 2 = 42
|
||||||
|
|
||||||
|
test fmt.printf("sq1: %d", $x1) {}
|
||||||
|
test fmt.printf("sq2: %d", $x2) {}
|
||||||
1
lang/interpret_test/TestAstFunc2/func-shadowing1.output
Normal file
1
lang/interpret_test/TestAstFunc2/func-shadowing1.output
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Vertex: test[hello world]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
$foo = "bad1"
|
||||||
|
func bar($foo) {
|
||||||
|
"hello " + $foo # shadows parent var
|
||||||
|
}
|
||||||
|
test bar("world") {}
|
||||||
1
lang/interpret_test/TestAstFunc2/func-shadowing2.output
Normal file
1
lang/interpret_test/TestAstFunc2/func-shadowing2.output
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Vertex: test[hello world]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
$foo = "bad1"
|
||||||
|
$bar = func($foo) {
|
||||||
|
"hello " + $foo # shadows parent var
|
||||||
|
}
|
||||||
|
test $bar("world") {}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# err: err3: expected: Func, got: Int
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import "fmt"
|
||||||
|
|
||||||
|
# function expression
|
||||||
|
$notfn = 42
|
||||||
|
|
||||||
|
$x = $notfn(7)
|
||||||
|
|
||||||
|
print "msg" {
|
||||||
|
msg => fmt.printf("notfn: %d", $x),
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user