From bfe020bfa91640e7a6f1a516dc752fa754249088 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 21 Oct 2026 06:00:55 +0000 Subject: feat(codegen): support explicit template arguments Co-Authored-By: Claude Opus 5.5 --- tests/i/template-explicit-args/case.json | 1 + tests/i/template-explicit-args/expected.txt | 9 +++++ tests/i/template-explicit-args/notes.md | 5 +++ tests/i/template-explicit-args/program.cps | 47 ++++++++++++++++++++++++++ tests/iii/template-explicit-errors/case.json | 10 ++++++ tests/iii/template-explicit-errors/program.cps | 24 +++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 tests/i/template-explicit-args/case.json create mode 100644 tests/i/template-explicit-args/expected.txt create mode 100644 tests/i/template-explicit-args/notes.md create mode 100644 tests/i/template-explicit-args/program.cps create mode 100644 tests/iii/template-explicit-errors/case.json create mode 100644 tests/iii/template-explicit-errors/program.cps (limited to 'tests') diff --git a/tests/i/template-explicit-args/case.json b/tests/i/template-explicit-args/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/template-explicit-args/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/template-explicit-args/expected.txt b/tests/i/template-explicit-args/expected.txt new file mode 100644 index 0000000..1c46cb5 --- /dev/null +++ b/tests/i/template-explicit-args/expected.txt @@ -0,0 +1,9 @@ +2.000000 +3 +tail +0.000000 +2.500000 +9 +200 +100 +chain diff --git a/tests/i/template-explicit-args/notes.md b/tests/i/template-explicit-args/notes.md new file mode 100644 index 0000000..b487fdd --- /dev/null +++ b/tests/i/template-explicit-args/notes.md @@ -0,0 +1,5 @@ +# template-explicit-args + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: TEMPLATE is a project extension. Max explicitly selects REAL and INTEGER, producing 2.000000 and 3. Second binds its first type explicitly and deduces STRING for its second, returning tail. Zero's otherwise undeducible return type is explicitly REAL, producing 0.000000. CALL and bare Print statements produce 2.500000 and 9. Pick uses the ordinary exact overload without explicit arguments (200) and the template with them (100). Echo preserves the STRING chain through a result method, and discarded STRING results exercise statement cleanup without output. diff --git a/tests/i/template-explicit-args/program.cps b/tests/i/template-explicit-args/program.cps new file mode 100644 index 0000000..123bda6 --- /dev/null +++ b/tests/i/template-explicit-args/program.cps @@ -0,0 +1,47 @@ +TEMPLATE +FUNCTION Max(A : T, B : T) RETURNS T + IF A > B THEN + RETURN A + ENDIF + RETURN B +ENDFUNCTION + +TEMPLATE +FUNCTION Second(First : A, Last : B) RETURNS B + RETURN Last +ENDFUNCTION + +TEMPLATE +FUNCTION Zero() RETURNS T + RETURN (T)0 +ENDFUNCTION + +TEMPLATE +PROCEDURE Print(Value : T) + OUTPUT Value +ENDPROCEDURE + +TEMPLATE +FUNCTION Echo(Value : T) RETURNS T + RETURN Value +ENDFUNCTION + +TEMPLATE +OVERLOAD FUNCTION Pick(Value : T) RETURNS INTEGER + RETURN 100 +ENDFUNCTION +OVERLOAD FUNCTION Pick(Value : INTEGER) RETURNS INTEGER + RETURN 200 +ENDFUNCTION + +OUTPUT Max(1.5, 2.0) +OUTPUT Max(Max(1, 2), 3) +OUTPUT Second(7, "tail") +OUTPUT Zero() +CALL Print(2.5) +Print(9) +OUTPUT Pick(7) +OUTPUT Pick(7) +OUTPUT (STRING)Echo("chain").C_STR() +CALL Echo("discarded") +Echo("also discarded") diff --git a/tests/iii/template-explicit-errors/case.json b/tests/iii/template-explicit-errors/case.json new file mode 100644 index 0000000..657f69e --- /dev/null +++ b/tests/iii/template-explicit-errors/case.json @@ -0,0 +1,10 @@ +{ + "errors": [ + {"reason": "No matching overload for Max", "line": 19}, + {"reason": "No matching overload for Max", "line": 20}, + {"reason": "Explicit template arguments require a function template: Plain", "line": 21}, + {"reason": "Explicit template arguments are not allowed on builtin EOF", "line": 22}, + {"reason": "Explicit template arguments are not allowed on builtin MALLOC", "line": 23}, + {"reason": "Ambiguous call to Tied", "line": 24} + ] +} diff --git a/tests/iii/template-explicit-errors/program.cps b/tests/iii/template-explicit-errors/program.cps new file mode 100644 index 0000000..5236328 --- /dev/null +++ b/tests/iii/template-explicit-errors/program.cps @@ -0,0 +1,24 @@ +TEMPLATE +FUNCTION Max(A : T, B : T) RETURNS T + RETURN A +ENDFUNCTION + +FUNCTION Plain(Value : INTEGER) RETURNS INTEGER + RETURN Value +ENDFUNCTION + +TEMPLATE +OVERLOAD FUNCTION Tied(Value : A) RETURNS INTEGER + RETURN 1 +ENDFUNCTION +TEMPLATE +OVERLOAD FUNCTION Tied(Value : B) RETURNS INTEGER + RETURN 2 +ENDFUNCTION + +OUTPUT Max(1, 2) +OUTPUT Max(1, 2.5) +OUTPUT Plain(1) +OUTPUT EOF("file.txt") +CALL MALLOC(8) +OUTPUT Tied(1) -- cgit v1.3.1-4-g156e