aboutsummaryrefslogtreecommitdiffhomepage
path: root/testexec/mainapp/generics.slul
blob: 04e06ab1688f86c62363d704db3c1e3c2d67cedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

#
# Test of generic types
#
# Copyright © 2023 Samuel Lidén Borell <samuel@kodafritt.se>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

type NonGeneric1 = struct {
    int x
}

type NonGeneric2 = struct {
    bool b
    int y
}

type NonGeneric3 = struct {
    int a
    int x
}

type Generic1<ref T> = struct {
    Generic2<slot T> same
    Generic2<ref NonGeneric3> fixed
    Generic2<slot T> also_same
    slot T elem
}

type Generic2<T> = struct {
    slot T elem
}

type Generic3<ref T, ref U> = struct {
    ref Generic2<slot T> a
    ref Generic2<slot U> b
    [2]Generic1<slot U> arr
}

type Generic4<ref T, ref U, ref V> = struct {
    Generic3<slot T, slot T> tt_ut
    Generic3<slot T, slot U> tt_uu
    Generic3<slot T, slot V> tt_uv
    Generic3<slot U, slot T> tu_ut
    Generic3<slot U, slot U> tu_uu
    Generic3<slot U, slot V> tu_uv
    Generic3<slot V, slot T> tv_ut
    Generic3<slot V, slot U> tv_uu
    Generic3<slot V, slot V> tv_uv
}

func generics_test1()
{
    NonGeneric1 ng1 = (.x=1100)
    NonGeneric2 ng2 = (.b=true, .y=2200)
    NonGeneric3 ng3 = (.a=3300, .x=3400)

    Generic2<int> g2_int = (.elem = 111)
    Generic2<ref NonGeneric1> g2 = (.elem = refto ng1)

    Generic1<ref NonGeneric1> g1 = (
        .same = (.elem = refto ng1),
        # TODO omitting the "," gives "Unbalances parenthesis" (because the parser thinks that the expr ends)
        .fixed = (.elem = refto ng3),
        .also_same = g2,
        .elem = refto ng1
    )

    var Generic2<int> g2_int_b = g2_int
    g2_int_b.elem = 222

    var Generic1<ref NonGeneric1> g1_b
    g1_b = g1
    g1_b.same.elem = refto ng1
    g1_b.fixed.elem = refto ng3
    g1_b.elem = refto ng1

    # FIXME implement runtime-computed literals
    var Generic1<bool> g1_bool = (
        .same = (.elem = true),
        .fixed = (.elem = refto ng3),
        .also_same = (.elem = false),
        .elem = true,
    )
    assert g1_bool.same.elem
    # XXX fails due to runtime-computed literals being unimplemented
    assert g1_bool.fixed.elem ref_is refto ng3
    assert not g1_bool.also_same.elem
    assert g1_bool.elem
    g1_bool.fixed.elem = refto ng3
    g1_bool.also_same.elem = true

    var Generic2<bool> g2_bool = (.elem = true)

    var Generic2<ref Generic2<int>> g2_nested = (
        .elem = refto g2_int
    )
    g2_nested.elem = refto g2_int
    g2_nested.elem.elem = 333

    var Generic2<ref Generic2<bool>> g2_nested_bool = (
        .elem = refto g2_bool
    )

    var Generic2<ref Generic2<ref Generic2<int>>> g2_doublenested = (
        .elem = refto g2_nested
    )
    g2_doublenested.elem = refto g2_nested
    g2_doublenested.elem.elem = refto g2_int
    g2_doublenested.elem.elem.elem = 444

    var Generic3<int,bool> g3_intbool = (
        .a = refto g2_int,
        .b = refto g2_bool,
        .arr = [g1_bool, g1_bool]
    )
    g3_intbool.a = refto g2_int
    g3_intbool.b = refto g2_bool
    g3_intbool.arr = [g1_bool, g1_bool]
    # FIXME gives "Incompatible types"
    #g3_intbool.arr[1] = g1_bool

    # FIXME causes an assertion failure in ir.c:add_result
    #var Generic3<ref Generic2<ref Generic2<int>>, ref Generic2<bool>> g3_nested = (
    #    .a = refto g2_doublenested,
    #    .b = refto g2_nested_bool,
    #    .arr = [
    #        (
    #            .same = (.elem = refto g2_bool),
    #            .fixed = (.elem = refto ng3),
    #            .also_same = (.elem = refto g2_bool),
    #            .elem = refto g2_bool,
    #        ),
    #        (
    #            .same = (.elem = refto g2_bool),
    #            .fixed = (.elem = refto ng3),
    #            .also_same = (.elem = refto g2_bool),
    #            .elem = refto g2_bool,
    #        ),
    #    ]
    #)

    var int i = 123
    var ref var int intref = refto i
    var Generic4<int,bool,ref int> g4_bound = get_g4_bound()
    # FIXME .62: error: Unexpected symbol after operator
    # XXX this could be solved by parsing < as a type parameter IF it is followed by a type
    # XXX but that will prevent value parameters / dependent types from working on non-methods. E.g. work<10,int>
    #var Generic4<int,bool,ref int> g4_params = get_g4_params<int,ref int,int,ref NonGeneric1,bool>(i, intref)

    g2_int_b = deref g3_intbool.get_a()
    i = g3_intbool.get_a_elem()
    g3_intbool.set_b(refto g2_bool)
    g3_intbool.set_inner(true)
}

func get_g4_bound() -> Generic4<int,bool,ref int>
{
    while true { }
}

func get_g4_params<A,B,C,D,E>(slot A x, slot B y) -> var Generic4<slot C, slot E, slot B>
{
    slot B b = y
    NonGeneric3 ng3 = (.a = 123, .x = 456)
    Generic2<slot B> g2_b = (.elem=y)
    Generic2<ref NonGeneric3> g2_ng3 = (.elem = refto ng3)
    Generic3<slot B, slot B> g3_bb = (
        .a   = refto g2_b,
        .b   = refto g2_b,
        .arr = [
            (
                .same = g2_b,
                .fixed = (.elem = refto ng3),
                .also_same = g2_b,
                .elem = y
            ),
            (
                .same = g2_b,
                .fixed = g2_ng3,
                .also_same = g2_b,
                .elem = y
            )],
        )
    var Generic4<slot C, slot E, slot B> thing
    # TODO need to assign "thing" before the variable can be used.
    #thing.tv_uv = g3_bb
    while true {}
}


func Generic3.get_a() -> ref Generic2<slot T>
{
    return this.a
}

func Generic3.get_a_elem() -> slot T
{
    return this.a.elem
}

func Generic3.set_b(ref Generic2<slot U> value)
{
    this.b = value
}

func Generic3.set_inner(slot U inner)
{
    this.arr[1].same.elem = inner
}


func generics_test()
{
    # TODO fails due to various unimplemented stuff
    #generics_test1()
}