diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-21 06:14:00 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-21 06:14:00 +0000 |
| commit | db8c7f14d2f74c3db205782acf8222cb7d91a047 (patch) | |
| tree | 31b6e17c0cb818ba0cc700cd1e0ca896a6b9156f /tests | |
| parent | 25ae363250e479701ed532c55c255e043c8240da (diff) | |
feat(codegen): instantiate class templates
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i/template-classes/case.json | 1 | ||||
| -rw-r--r-- | tests/i/template-classes/expected.txt | 15 | ||||
| -rw-r--r-- | tests/i/template-classes/notes.md | 7 | ||||
| -rw-r--r-- | tests/i/template-classes/program.cps | 110 | ||||
| -rw-r--r-- | tests/iii/template-class-errors/case.json | 8 | ||||
| -rw-r--r-- | tests/iii/template-class-errors/program.cps | 13 | ||||
| -rw-r--r-- | tests/iii/template-class-pack-field/case.json | 5 | ||||
| -rw-r--r-- | tests/iii/template-class-pack-field/program.cps | 5 |
8 files changed, 164 insertions, 0 deletions
diff --git a/tests/i/template-classes/case.json b/tests/i/template-classes/case.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/i/template-classes/case.json @@ -0,0 +1 @@ +{} diff --git a/tests/i/template-classes/expected.txt b/tests/i/template-classes/expected.txt new file mode 100644 index 0000000..9413c45 --- /dev/null +++ b/tests/i/template-classes/expected.txt @@ -0,0 +1,15 @@ +FALSE +0 +7 +3 +2.500000 +30 +20 +10 +5 +9 +alpha +beta +1 +2.500000 +c diff --git a/tests/i/template-classes/notes.md b/tests/i/template-classes/notes.md new file mode 100644 index 0000000..96cfe22 --- /dev/null +++ b/tests/i/template-classes/notes.md @@ -0,0 +1,7 @@ +# Class templates + +Human review: pending. + +TEMPLATE is a compiler extension. Holder forces Box<INTEGER> to instantiate during the type pre-pass. Make<Box<BOOLEAN>>() first creates Box<BOOLEAN> through an explicit function argument and returns its default FALSE value. Holder's integer field defaults to zero. Box's explicit constructor then stores 7. Pair preserves its INTEGER and REAL fields, giving 3 and 2.500000. Stack3 pushes 10, 20 and 30 into three fixed scalar slots and pops them in reverse order. + +Keyword NEW constructs a fresh Box containing 5; DELETE releases that heap object. The nested Box copies the inner integer value 9. Box<STRING> assignment copies its owning STRING field independently: changing Text to beta leaves Copy equal to alpha, and both values remain valid through cleanup. Group expands three method parameters and forwards them to PrintAll, yielding 1, 2.500000 and c. ShowGroup() instantiates an internally empty Group<> and its parameterless Show, producing no output. diff --git a/tests/i/template-classes/program.cps b/tests/i/template-classes/program.cps new file mode 100644 index 0000000..90427d3 --- /dev/null +++ b/tests/i/template-classes/program.cps @@ -0,0 +1,110 @@ +TEMPLATE <T> +CLASS Box + PRIVATE Value : T + PUBLIC OVERLOAD PROCEDURE NEW() + ENDPROCEDURE + PUBLIC OVERLOAD PROCEDURE NEW(V : T) + Value <- V + ENDPROCEDURE + PUBLIC FUNCTION Get() RETURNS T + RETURN Value + ENDFUNCTION + PUBLIC PROCEDURE Set(V : T) + Value <- V + ENDPROCEDURE +ENDCLASS + +TEMPLATE <T> +FUNCTION Make() RETURNS T + DECLARE Value : T + RETURN Value +ENDFUNCTION + +TEMPLATE <A, B> +CLASS Pair + First : A + Second : B + PROCEDURE NEW(Left : A, Right : B) + First <- Left + Second <- Right + ENDPROCEDURE +ENDCLASS + +TEMPLATE <T> +CLASS Stack3 + PRIVATE First : T + PRIVATE Second : T + PRIVATE Third : T + PRIVATE Count : INTEGER + PROCEDURE Push(Value : T) + Count <- Count + 1 + CASE OF Count + 1 : First <- Value + 2 : Second <- Value + 3 : Third <- Value + ENDCASE + ENDPROCEDURE + FUNCTION Pop() RETURNS T + Count <- Count - 1 + CASE OF Count + 2 : RETURN Third + 1 : RETURN Second + OTHERWISE : RETURN First + ENDCASE + ENDFUNCTION +ENDCLASS + +OVERLOAD PROCEDURE PrintAll() +ENDPROCEDURE +TEMPLATE <T, Rest...> +OVERLOAD PROCEDURE PrintAll(First : T, Others : Rest...) + OUTPUT First + CALL PrintAll(Others...) +ENDPROCEDURE + +TEMPLATE <Types...> +CLASS Group + PROCEDURE Show(Values : Types...) + CALL PrintAll(Values...) + ENDPROCEDURE +ENDCLASS +TEMPLATE <Types...> +PROCEDURE ShowGroup(Values : Types...) + DECLARE Items : Group<Types...> + CALL Items.Show(Values...) +ENDPROCEDURE + +// Instantiation during the type pre-pass defers the Box method prototypes. +TYPE Holder + DECLARE Item : Box<INTEGER> +ENDTYPE +OUTPUT Make<Box<BOOLEAN>>().Get() +DECLARE H : Holder +OUTPUT H.Item.Get() +DECLARE B : Box<INTEGER>(7) +OUTPUT B.Get() +DECLARE P : Pair<INTEGER, REAL>(3, 2.5) +OUTPUT P.First +OUTPUT P.Second +DECLARE S : Stack3<INTEGER> +CALL S.Push(10) +CALL S.Push(20) +CALL S.Push(30) +OUTPUT S.Pop() +OUTPUT S.Pop() +OUTPUT S.Pop() +B <- NEW Box<INTEGER>(5) +OUTPUT B.Get() +DELETE B +DECLARE Inner : Box<INTEGER>(9) +DECLARE Outer : Box<Box<INTEGER>>(Inner) +OUTPUT Outer.Get().Get() +DECLARE Text : Box<STRING>("alpha") +DECLARE Copy : Box<STRING> +Copy <- Text +CALL Text.Set("beta") +OUTPUT Copy.Get() +OUTPUT Text.Get() +DECLARE Values : Group<INTEGER, REAL, CHAR> +CALL Values.Show(1, 2.5, 'c') +CALL ShowGroup() diff --git a/tests/iii/template-class-errors/case.json b/tests/iii/template-class-errors/case.json new file mode 100644 index 0000000..100438f --- /dev/null +++ b/tests/iii/template-class-errors/case.json @@ -0,0 +1,8 @@ +{ + "errors": [ + {"reason": "No matching overload for Make", "line": 10}, + {"reason": "Wrong number of template arguments for Box", "line": 11}, + {"reason": "Class template Box requires template arguments", "line": 12}, + {"reason": "Unknown or invalid template argument Nope for Box", "line": 13} + ] +} diff --git a/tests/iii/template-class-errors/program.cps b/tests/iii/template-class-errors/program.cps new file mode 100644 index 0000000..67f8db4 --- /dev/null +++ b/tests/iii/template-class-errors/program.cps @@ -0,0 +1,13 @@ +TEMPLATE <T> +CLASS Box + Value : T +ENDCLASS +TEMPLATE <T> +FUNCTION Make() RETURNS T + DECLARE Value : T + RETURN Value +ENDFUNCTION +OUTPUT Make<Box<Nope>>() +DECLARE TooMany : Box<INTEGER, REAL> +DECLARE TooFew : Box +DECLARE Unknown : Box<Nope> diff --git a/tests/iii/template-class-pack-field/case.json b/tests/iii/template-class-pack-field/case.json new file mode 100644 index 0000000..2ad0199 --- /dev/null +++ b/tests/iii/template-class-pack-field/case.json @@ -0,0 +1,5 @@ +{ + "errors": [ + {"reason": "pack.*field|field.*pack", "line": 3} + ] +} diff --git a/tests/iii/template-class-pack-field/program.cps b/tests/iii/template-class-pack-field/program.cps new file mode 100644 index 0000000..83f30bb --- /dev/null +++ b/tests/iii/template-class-pack-field/program.cps @@ -0,0 +1,5 @@ +TEMPLATE <Rest...> +CLASS Invalid + Values : Rest... +ENDCLASS +OUTPUT 0 |
