aboutsummaryrefslogtreecommitdiff
path: root/tests/functions.sh
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-02 07:20:53 -0500
committerMustafa Quraish <[email protected]>2022-02-02 07:37:39 -0500
commit1a8f96c65f94227faa9747ef876a60f3c313c6f1 (patch)
treed80a396958ff2fc752b620cc5314e27e40b58ecb /tests/functions.sh
parentUse `type*` instead of `type&` to denote a pointer type (for now) (diff)
downloadcup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.tar.xz
cup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.zip
Type checking of expressions / functions!
This is a bit of a chonky commit, but it adds in the basics of checking the types of expressions / function calls / return types. There's still a lot of work to be done, including: (1) Adding new core types, and casting between allowed types automatically (2) Picking the corrent output type based on input types (for instance float+int == float) (3) We need much better error reporting, the error messages are really vague and unhelpful as-is (4) We also need to work to ensure that a function with a return type actually returns (5) Possible re-factoring to make stuff less hacky when we have more types / structs / arrays / etc.
Diffstat (limited to 'tests/functions.sh')
-rwxr-xr-xtests/functions.sh44
1 files changed, 22 insertions, 22 deletions
diff --git a/tests/functions.sh b/tests/functions.sh
index e8c79f2..c188bd1 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -8,61 +8,61 @@ set -e
echo -n "- Different argument counts: "
assert_exit_status_stdin 5 <<EOF
-fn test() { return 5; }
-fn main() { return test(); }
+fn test(): int { return 5; }
+fn main(): int { return test(); }
EOF
assert_exit_status_stdin 5 <<EOF
-fn test(a: int) { return a; }
-fn main() { return test(5); }
+fn test(a: int): int { return a; }
+fn main(): int { return test(5); }
EOF
assert_exit_status_stdin 5 <<EOF
-fn test(a: int, b: int) { return a+b; }
-fn main() { return test(2, 3); }
+fn test(a: int, b: int): int { return a+b; }
+fn main(): int { return test(2, 3); }
EOF
assert_exit_status_stdin 5 <<EOF
-fn test(a: int, b: int) { let n: int = a + b; return n; }
-fn main() { return test(2, 3); }
+fn test(a: int, b: int): int { let n: int = a + b; return n; }
+fn main(): int { return test(2, 3); }
EOF
assert_exit_status_stdin 5 <<EOF
-fn test(a: int, b: int, c: int, d: int, e: int) { return a+b+c+d+e; }
-fn main() { return test(1,1,1,1,1); }
+fn test(a: int, b: int, c: int, d: int, e: int): int { return a+b+c+d+e; }
+fn main(): int { return test(1,1,1,1,1); }
EOF
assert_compile_failure_stdin <<EOF
-fn test() { return 5; }
-fn main() { return test(5); }
+fn test(): int { return 5; }
+fn main(): int { return test(5); }
EOF
assert_compile_failure_stdin <<EOF
-fn test(a: int, b: int, c: int) { return 5; }
-fn main() { return test(5); }
+fn test(a: int, b: int, c: int): int { return 5; }
+fn main(): int { return test(5); }
EOF
assert_compile_failure_stdin <<EOF
-fn test(a: int, b: int, c: int) { return 5; }
-fn main() { return test(5, 6, 5, 8); }
+fn test(a: int, b: int, c: int): int { return 5; }
+fn main(): int { return test(5, 6, 5, 8); }
EOF
echo " OK"
echo -n "- Recursion: "
assert_exit_status_stdin 3 <<EOF
-fn test(n: int) { return n == 0 ? 0 : 1 + test(n-1); }
-fn main() { return test(3); }
+fn test(n: int): int { return n == 0 ? 0 : 1 + test(n-1); }
+fn main(): int { return test(3); }
EOF
assert_exit_status_stdin 55 <<EOF
-fn test(n: int) { return n == 0 ? 0 : n + test(n-1); }
-fn main() { return test(10); }
+fn test(n: int): int { return n == 0 ? 0 : n + test(n-1); }
+fn main(): int { return test(10); }
EOF
assert_exit_status_stdin 55 <<EOF
-fn test(n: int, accum: int) { return n == 0 ? accum : test(n-1, n+accum); }
-fn main() { return test(10,0); }
+fn test(n: int, accum: int): int { return n == 0 ? accum : test(n-1, n+accum); }
+fn main(): int { return test(10,0); }
EOF
echo " OK" \ No newline at end of file