From 3651ab5c0c73106b4ce49fea31ccf12951fb1d8c Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 20 Jul 2019 22:27:21 -0400 Subject: [PATCH] lang: Add more tests for function --- .../TestAstFunc2/func-math1.output | 2 ++ .../TestAstFunc2/func-math1/main.mcl | 17 +++++++++++++++++ .../TestAstFunc2/func-shadowing1.output | 1 + .../TestAstFunc2/func-shadowing1/main.mcl | 5 +++++ .../TestAstFunc2/func-shadowing2.output | 1 + .../TestAstFunc2/func-shadowing2/main.mcl | 5 +++++ .../TestAstFunc2/invalid-function-call.output | 1 + .../TestAstFunc2/invalid-function-call/main.mcl | 10 ++++++++++ 8 files changed, 42 insertions(+) create mode 100644 lang/interpret_test/TestAstFunc2/func-math1.output create mode 100644 lang/interpret_test/TestAstFunc2/func-math1/main.mcl create mode 100644 lang/interpret_test/TestAstFunc2/func-shadowing1.output create mode 100644 lang/interpret_test/TestAstFunc2/func-shadowing1/main.mcl create mode 100644 lang/interpret_test/TestAstFunc2/func-shadowing2.output create mode 100644 lang/interpret_test/TestAstFunc2/func-shadowing2/main.mcl create mode 100644 lang/interpret_test/TestAstFunc2/invalid-function-call.output create mode 100644 lang/interpret_test/TestAstFunc2/invalid-function-call/main.mcl diff --git a/lang/interpret_test/TestAstFunc2/func-math1.output b/lang/interpret_test/TestAstFunc2/func-math1.output new file mode 100644 index 00000000..a86ee659 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-math1.output @@ -0,0 +1,2 @@ +Vertex: test[sq1: 13] +Vertex: test[sq2: 42] diff --git a/lang/interpret_test/TestAstFunc2/func-math1/main.mcl b/lang/interpret_test/TestAstFunc2/func-math1/main.mcl new file mode 100644 index 00000000..6b41c8ae --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-math1/main.mcl @@ -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) {} diff --git a/lang/interpret_test/TestAstFunc2/func-shadowing1.output b/lang/interpret_test/TestAstFunc2/func-shadowing1.output new file mode 100644 index 00000000..5a72b413 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-shadowing1.output @@ -0,0 +1 @@ +Vertex: test[hello world] diff --git a/lang/interpret_test/TestAstFunc2/func-shadowing1/main.mcl b/lang/interpret_test/TestAstFunc2/func-shadowing1/main.mcl new file mode 100644 index 00000000..52272054 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-shadowing1/main.mcl @@ -0,0 +1,5 @@ +$foo = "bad1" +func bar($foo) { + "hello " + $foo # shadows parent var +} +test bar("world") {} diff --git a/lang/interpret_test/TestAstFunc2/func-shadowing2.output b/lang/interpret_test/TestAstFunc2/func-shadowing2.output new file mode 100644 index 00000000..5a72b413 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-shadowing2.output @@ -0,0 +1 @@ +Vertex: test[hello world] diff --git a/lang/interpret_test/TestAstFunc2/func-shadowing2/main.mcl b/lang/interpret_test/TestAstFunc2/func-shadowing2/main.mcl new file mode 100644 index 00000000..da9e8699 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/func-shadowing2/main.mcl @@ -0,0 +1,5 @@ +$foo = "bad1" +$bar = func($foo) { + "hello " + $foo # shadows parent var +} +test $bar("world") {} diff --git a/lang/interpret_test/TestAstFunc2/invalid-function-call.output b/lang/interpret_test/TestAstFunc2/invalid-function-call.output new file mode 100644 index 00000000..04ce91a8 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/invalid-function-call.output @@ -0,0 +1 @@ +# err: err3: expected: Func, got: Int diff --git a/lang/interpret_test/TestAstFunc2/invalid-function-call/main.mcl b/lang/interpret_test/TestAstFunc2/invalid-function-call/main.mcl new file mode 100644 index 00000000..a10835c6 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/invalid-function-call/main.mcl @@ -0,0 +1,10 @@ +import "fmt" + +# function expression +$notfn = 42 + +$x = $notfn(7) + +print "msg" { + msg => fmt.printf("notfn: %d", $x), +}