Skip to content

Commit 0f9dd15

Browse files
committed
[Tolk] Implement generic structs and aliases
Now structures can be generic: Container<T> and similar. The same goes for type aliases: type A<T> = Container<T>. Like generic functions, every struct is instantiated, "Container<int>" and "Container<slice>" become different symbols, and on instantiation they walk though the pipeline. Type inferring and checking are done only upon instantiation.
1 parent 09019fe commit 0f9dd15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2227
-401
lines changed

tolk-tester/tests/generics-1.tolk

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ fun getTwo<X>(): X { return 2 as X; }
1717

1818
fun takeInt(a: int) { return a; }
1919

20+
struct Wrapper<T> {
21+
value: T;
22+
}
23+
struct Err<T> {
24+
errPayload: T
25+
}
26+
2027
@method_id(102)
21-
fun test102(): (int, int, int, [int, int]) {
28+
fun test102(): (int, int, int, [int, int], Wrapper< Wrapper<int8> >) {
2229
var a: int = getTwo();
2330
var _: int = getTwo();
2431
var b = getTwo() as int;
@@ -32,7 +39,10 @@ fun test102(): (int, int, int, [int, int]) {
3239
ab_tup.0 = getTwo();
3340
ab_tup.1.1 = getTwo();
3441
var ab_tens_al: Tensor2Int = (getTwo(), getTwo());
35-
return (eq1<int>(a), eq2<int>(b), takeInt(getTwo()), [getTwo(), ab_tens.1.1]);
42+
var cint: Wrapper<int> = { value: getTwo() };
43+
var cuni: Wrapper<int> | Err<int> = Wrapper { value: getTwo() };
44+
cuni = Err { errPayload: getTwo() };
45+
return (eq1<int>(a), eq2<int>(b), takeInt(getTwo()), [getTwo(), ab_tens.1.1], eq1({ value: { value: getTwo() } } as Wrapper< Wrapper<int8> >));
3646
}
3747

3848
@method_id(103)
@@ -204,7 +214,7 @@ fun main(x: int): (int, [Tup2Int]) {
204214
/**
205215
@testcase | 0 | 1 | 1 [ [ 1 2 ] ]
206216
@testcase | 101 | 0 | 0 0 0 [ 0 0 ] 0 0 0 [ 0 0 ] 0 0 0 []
207-
@testcase | 102 | | 2 2 2 [ 2 2 ]
217+
@testcase | 102 | | 2 2 2 [ 2 2 ] 2
208218
@testcase | 103 | 0 | 0 100 100
209219
@testcase | 104 | 0 | [ 1 (null) 2 ] [ 2 -1 0 ]
210220
@testcase | 105 | | 3
@@ -224,6 +234,7 @@ fun main(x: int): (int, [Tup2Int]) {
224234
@fif_codegen DECLPROC eq1<[int,int]>
225235
@fif_codegen DECLPROC eq1<MInt?>
226236
@fif_codegen DECLPROC eq1<int|slice|null>
237+
@fif_codegen DECLPROC eq1<Wrapper<Wrapper<int8>>>
227238
@fif_codegen DECLPROC getTwo<int>
228239

229240
@fif_codegen_avoid DECLPROC eq1

0 commit comments

Comments
 (0)