diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-18 06:05:53 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-18 06:05:53 +0000 |
| commit | 16231050058131860f710073c17d3bca7c6637eb (patch) | |
| tree | 1da2ca606f1d513822ccd04b3053d5eee69ac19d /tests | |
| parent | 1b0b287629e6e3b6d8eb6182db72b28fd0510574 (diff) | |
feat(operators): support global binary operator overloads
Resolve exact operand types and preserve declared operator return types.
Reject implicit STRING to BOOLEAN conversion at typed expression boundaries.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i/operator-global/case.json | 1 | ||||
| -rw-r--r-- | tests/i/operator-global/expected.txt | 5 | ||||
| -rw-r--r-- | tests/i/operator-global/notes.md | 7 | ||||
| -rw-r--r-- | tests/i/operator-global/program.cps | 28 | ||||
| -rw-r--r-- | tests/iii/operator-global-errors/case.json | 24 | ||||
| -rw-r--r-- | tests/iii/operator-global-errors/notes.md | 3 | ||||
| -rw-r--r-- | tests/iii/operator-global-errors/program.cps | 22 |
7 files changed, 90 insertions, 0 deletions
diff --git a/tests/i/operator-global/case.json b/tests/i/operator-global/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/operator-global/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/operator-global/expected.txt b/tests/i/operator-global/expected.txt new file mode 100644 index 0000000..3460ce8 --- /dev/null +++ b/tests/i/operator-global/expected.txt @@ -0,0 +1,5 @@ +7 +2! +comparison +6 +5xyTRUE diff --git a/tests/i/operator-global/notes.md b/tests/i/operator-global/notes.md new file mode 100644 index 0000000..1026071 --- /dev/null +++ b/tests/i/operator-global/notes.md @@ -0,0 +1,7 @@ +# operator-global + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: operator overloading is a project extension, with exact parameter types. + +Trace: Box additions sum 2 + 3 + 2 to 7. Concatenation returns 2!. The comparison deliberately returns STRING, printed as comparison. Reversed INTEGER + Box gives 4 + 2 = 6. Builtin operations still print 5xyTRUE. diff --git a/tests/i/operator-global/program.cps b/tests/i/operator-global/program.cps new file mode 100644 index 0000000..9d27fcb --- /dev/null +++ b/tests/i/operator-global/program.cps @@ -0,0 +1,28 @@ +TYPE Box +DECLARE Value : INTEGER +ENDTYPE +OVERLOAD FUNCTION OPERATOR+(a : Box, b : Box) RETURNS Box +DECLARE result : Box +result.Value <- a.Value + b.Value +RETURN result +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR&(a : Box, b : STRING) RETURNS STRING +RETURN NUM_TO_STR(a.Value) & b +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR<(a : Box, b : Box) RETURNS STRING +RETURN "comparison" +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR+(a : INTEGER, b : Box) RETURNS INTEGER +RETURN a + b.Value +ENDFUNCTION +DECLARE a : Box +DECLARE b : Box +DECLARE result : Box +a.Value <- 2 +b.Value <- 3 +result <- a + b + a +OUTPUT result.Value +OUTPUT a & "!" +OUTPUT a < b +OUTPUT 4 + a +OUTPUT 2 + 3, "x" & "y", 1 < 2 diff --git a/tests/iii/operator-global-errors/case.json b/tests/iii/operator-global-errors/case.json new file mode 100644 index 0000000..eeb9965 --- /dev/null +++ b/tests/iii/operator-global-errors/case.json @@ -0,0 +1,24 @@ +{ + "errors": [ + { + "reason": "Cannot implicitly convert STRING to BOOLEAN", + "line": 12 + }, + { + "reason": "Operator \\+ cannot be applied to a record", + "line": 13 + }, + { + "reason": "requires a record, class or enum operand", + "line": 14 + }, + { + "reason": "Invalid operand count", + "line": 17 + }, + { + "reason": "cannot be redefined", + "line": 20 + } + ] +} diff --git a/tests/iii/operator-global-errors/notes.md b/tests/iii/operator-global-errors/notes.md new file mode 100644 index 0000000..24bb113 --- /dev/null +++ b/tests/iii/operator-global-errors/notes.md @@ -0,0 +1,3 @@ +# operator-global-errors + +Reject a STRING comparison result assigned to BOOLEAN, implicit INTEGER-to-REAL overload matching, builtin-only overloads, wrong arity and return-type-only duplicates. diff --git a/tests/iii/operator-global-errors/program.cps b/tests/iii/operator-global-errors/program.cps new file mode 100644 index 0000000..23bfae1 --- /dev/null +++ b/tests/iii/operator-global-errors/program.cps @@ -0,0 +1,22 @@ +TYPE Box +DECLARE Value : INTEGER +ENDTYPE +OVERLOAD FUNCTION OPERATOR+(a : Box, b : REAL) RETURNS INTEGER +RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR<(a : Box, b : Box) RETURNS STRING +RETURN "text" +ENDFUNCTION +DECLARE a : Box +DECLARE flag : BOOLEAN +flag <- a < a +OUTPUT a + 1 +OVERLOAD FUNCTION OPERATOR&(a : INTEGER, b : STRING) RETURNS STRING +RETURN b +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR*(a : Box) RETURNS INTEGER +RETURN 1 +ENDFUNCTION +OVERLOAD FUNCTION OPERATOR+(a : Box, b : REAL) RETURNS REAL +RETURN 2.0 +ENDFUNCTION |
