From dd5bbae055fd34ce2e12ef63f8a261c6b15dc83e Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 21 Oct 2026 05:43:35 +0000 Subject: feat(parser): parse TEMPLATE declarations, template ids and parameter packs Co-Authored-By: Claude Opus 5.5 --- tests/i/template-unused/case.json | 1 + tests/i/template-unused/expected.txt | 1 + tests/i/template-unused/lib.cps | 48 ++++++++++++++++++++++++++++ tests/i/template-unused/notes.md | 5 +++ tests/i/template-unused/program.cps | 2 ++ tests/iii/template-header-errors/case.json | 36 +++++++++++++++++++++ tests/iii/template-header-errors/program.cps | 39 ++++++++++++++++++++++ tests/iii/template-pack-errors/case.json | 32 +++++++++++++++++++ tests/iii/template-pack-errors/program.cps | 32 +++++++++++++++++++ 9 files changed, 196 insertions(+) create mode 100644 tests/i/template-unused/case.json create mode 100644 tests/i/template-unused/expected.txt create mode 100644 tests/i/template-unused/lib.cps create mode 100644 tests/i/template-unused/notes.md create mode 100644 tests/i/template-unused/program.cps create mode 100644 tests/iii/template-header-errors/case.json create mode 100644 tests/iii/template-header-errors/program.cps create mode 100644 tests/iii/template-pack-errors/case.json create mode 100644 tests/iii/template-pack-errors/program.cps (limited to 'tests') diff --git a/tests/i/template-unused/case.json b/tests/i/template-unused/case.json new file mode 100644 index 0000000..3415f39 --- /dev/null +++ b/tests/i/template-unused/case.json @@ -0,0 +1 @@ +{"sources": ["program.cps", "lib.cps"]} diff --git a/tests/i/template-unused/expected.txt b/tests/i/template-unused/expected.txt new file mode 100644 index 0000000..7f8f011 --- /dev/null +++ b/tests/i/template-unused/expected.txt @@ -0,0 +1 @@ +7 diff --git a/tests/i/template-unused/lib.cps b/tests/i/template-unused/lib.cps new file mode 100644 index 0000000..3de8ac3 --- /dev/null +++ b/tests/i/template-unused/lib.cps @@ -0,0 +1,48 @@ +TEMPLATE +FUNCTION Identity(Value : T) RETURNS T + IF Value < Value THEN + RETURN (T)Value + ENDIF + RETURN Value +ENDFUNCTION + +TEMPLATE +OVERLOAD PROCEDURE Visit(First : T, Others : Rest...) + OUTPUT Identity(First) + CALL Visit(Others...) + Visit>>(Others...) +ENDPROCEDURE + +TEMPLATE +CLASS Box + PRIVATE Value : T + PUBLIC PROCEDURE NEW(V : T) + Value <- V + ENDPROCEDURE + PUBLIC FUNCTION Get() RETURNS T + RETURN Value + ENDFUNCTION + PUBLIC FUNCTION AsBox(V : Box) RETURNS Box + RETURN (Box)V + ENDFUNCTION +ENDCLASS + +TEMPLATE +CLASS Pair + Left : A + Right : B +ENDCLASS + +TEMPLATE +CLASS Bundle + PUBLIC PROCEDURE Send(Values : Rest...) + CALL Visit(Values...) + ENDPROCEDURE +ENDCLASS + +TEMPLATE +PROCEDURE NestedTypes(Value : Box>) + DECLARE Other : Box> + Other <- NEW Box>() + Value <- (Box>)Value +ENDPROCEDURE diff --git a/tests/i/template-unused/notes.md b/tests/i/template-unused/notes.md new file mode 100644 index 0000000..c3ff7fd --- /dev/null +++ b/tests/i/template-unused/notes.md @@ -0,0 +1,5 @@ +# template-unused + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: TEMPLATE is a project extension. The library holds unused function, class and variadic templates, including casts, nested type ids, explicit calls and pack forwarding. Their bodies are parsed without being emitted or counted as top-level code. The entry unit prints INTEGER 7. diff --git a/tests/i/template-unused/program.cps b/tests/i/template-unused/program.cps new file mode 100644 index 0000000..d8b89a2 --- /dev/null +++ b/tests/i/template-unused/program.cps @@ -0,0 +1,2 @@ +INCLUDE "lib.cps" +OUTPUT 7 diff --git a/tests/iii/template-header-errors/case.json b/tests/iii/template-header-errors/case.json new file mode 100644 index 0000000..f74cc0d --- /dev/null +++ b/tests/iii/template-header-errors/case.json @@ -0,0 +1,36 @@ +{ + "errors": [ + { + "reason": "Duplicate template parameter", + "line": 1 + }, + { + "reason": "template parameter pack must be last", + "line": 6 + }, + { + "reason": "only one parameter pack", + "line": 10 + }, + { + "reason": "TEMPLATE cannot prefix TYPE", + "line": 14 + }, + { + "reason": "Operator templates are not supported", + "line": 17 + }, + { + "reason": "TEMPLATE declarations are only allowed at top level", + "line": 23 + }, + { + "reason": "TEMPLATE declarations are only allowed at top level", + "line": 30 + }, + { + "reason": "Expected '<' after TEMPLATE", + "line": 35 + } + ] +} diff --git a/tests/iii/template-header-errors/program.cps b/tests/iii/template-header-errors/program.cps new file mode 100644 index 0000000..73c34a2 --- /dev/null +++ b/tests/iii/template-header-errors/program.cps @@ -0,0 +1,39 @@ +TEMPLATE +FUNCTION Duplicate(Value : T) RETURNS T + RETURN Value +ENDFUNCTION + +TEMPLATE +PROCEDURE NotLast() +ENDPROCEDURE + +TEMPLATE +PROCEDURE TwoPacks() +ENDPROCEDURE + +TEMPLATE +TYPE Alias = ^T + +TEMPLATE +OVERLOAD FUNCTION OPERATOR+(Value : T) RETURNS T + RETURN Value +ENDFUNCTION + +IF TRUE THEN + TEMPLATE + PROCEDURE Nested(Value : T) + OUTPUT 99 + ENDPROCEDURE +ENDIF + +CLASS Outer + TEMPLATE + PROCEDURE Member(Value : T) + ENDPROCEDURE +ENDCLASS + +TEMPLATE <> +PROCEDURE Empty() +ENDPROCEDURE + +OUTPUT 0 diff --git a/tests/iii/template-pack-errors/case.json b/tests/iii/template-pack-errors/case.json new file mode 100644 index 0000000..53b5a23 --- /dev/null +++ b/tests/iii/template-pack-errors/case.json @@ -0,0 +1,32 @@ +{ + "errors": [ + { + "reason": "A pack parameter must be last", + "line": 2 + }, + { + "reason": "pack parameter must use the current template pack and cannot be ARRAY", + "line": 6 + }, + { + "reason": "pack parameter must use the current template pack and cannot be ARRAY", + "line": 10 + }, + { + "reason": "parameter pack must be expanded", + "line": 15 + }, + { + "reason": "Only a parameter pack may be expanded", + "line": 20 + }, + { + "reason": "Only the current template parameter pack may use", + "line": 25 + }, + { + "reason": "template parameter pack requires", + "line": 29 + } + ] +} diff --git a/tests/iii/template-pack-errors/program.cps b/tests/iii/template-pack-errors/program.cps new file mode 100644 index 0000000..51ffa46 --- /dev/null +++ b/tests/iii/template-pack-errors/program.cps @@ -0,0 +1,32 @@ +TEMPLATE +PROCEDURE NotLast(Values : Rest..., Extra : INTEGER) +ENDPROCEDURE + +TEMPLATE +PROCEDURE ArrayPack(Values : ARRAY OF Rest...) +ENDPROCEDURE + +TEMPLATE +PROCEDURE WrongPack(Values : T...) +ENDPROCEDURE + +TEMPLATE +PROCEDURE BarePack(Values : Rest...) + OUTPUT Values +ENDPROCEDURE + +TEMPLATE +PROCEDURE ScalarExpansion(Value : T) + CALL Missing(Value...) +ENDPROCEDURE + +TEMPLATE +PROCEDURE WrongTypeExpansion(Value : T) + CALL Missing(Value) +ENDPROCEDURE + +TEMPLATE +PROCEDURE MissingDots(Values : Rest) +ENDPROCEDURE + +OUTPUT 0 -- cgit v1.3.1-4-g156e