rev: 7606bb3b9dc06c5c748acde64702578933d0e055 scopes/lib/scopes/enum.sc -rw-r--r-- 23.2 KiB View raw Log this file
7606bb3b9dc0 — Leonard Ritter * `enum`: added support for `tostring` 19 days ago
                                                                                
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.

""""enum
    ====

    Support for defining tagged unions and classical enums through the `enum`
    sugar.

# typechecking-time function
fn... _extract-payload (enum-value, extractT, unpack? = true)
    extractT as:= type
    # don't bother if it's a unit tag
    if (extractT == Nothing)
        return (spice-quote none)

    let qcls = ('qualified-typeof enum-value)
    let cls = ('strip-qualifiers qcls)
    let payload-cls = ('element@ cls 1)
    let raw-payload = `(extractvalue enum-value 1)
    let refer = ('refer? qcls)
    let ptrT = ('refer->pointer-type qcls)

    let extracted =
        if refer
            let ptrET = ('change-element-type ptrT extractT)
            spice-quote
                let ptr = (reftoptr raw-payload)
                let ptr = (bitcast ptr ptrET)
                ptrtoref ptr
        else
            let ptrET = ('change-storage-class
                ('mutable (pointer.type extractT)) 'Function)
            spice-quote
                let ptr = (alloca (qualifiersof raw-payload))
                store raw-payload ptr
                let ptr = (bitcast ptr ptrET)
                load ptr
    let ET = ('strip-qualifiers extractT)
    let extracted =
        if ((ET < tuple) & unpack?)
            `(unpack extracted)
        else extracted

@@ memo
inline gen-dispatch-from-tag (f)
    fn "dispatch-from-tag" (tag handlers self...)
        let first-self = self...
        let qcls = ('qualified-typeof first-self)
        let cls = ('strip-qualifiers qcls)
        let fields = (('@ cls '__fields) as type)
        let field-types = ('@ cls '__fields__)
        let field-type-args = ('args field-types)
        let sw = (sc_switch_new tag)
        for arg in ('args handlers)
            let anchor = ('anchor arg)
            let key arg = ('dekey arg)
            if (key == unnamed)
                sc_switch_append_default sw `(arg)
            else
                let i = (sc_type_field_index fields key)
                let ET = ('key-type ('element@ fields i) unnamed)
                let field = (('getarg field-types i) as type)
                let lit = ('@ field 'Literal)
                let extractT = ('@ field 'Type)
                let payloads... = (f extractT self...)
                sc_switch_append_case sw lit ('tag `(arg payloads...) anchor)
        sw

inline illegal-case ()
    assert false "invalid enum tag; structure possibly corrupted"
    unreachable;

@@ memo
inline gen-apply-from-tag (f)
    fn "apply-from-tag" (tag handler self...)
        let first-self = self...
        let qcls = ('qualified-typeof first-self)
        let cls = ('strip-qualifiers qcls)
        let fields = (('@ cls '__fields) as type)
        let field-types = ('@ cls '__fields__)
        let field-type-args = ('args field-types)
        let sw = (sc_switch_new tag)
        let numfields = ('argcount field-types)
        let anchor = ('anchor handler)
        for i in (range numfields)
            let ET = ('key-type ('element@ fields i) unnamed)
            let FT = ('getarg field-types i)
            let field = (FT as type)
            let lit = ('@ field 'Literal)
            let extractT = ('@ field 'Type)
            let payloads... = (f extractT self...)
            sc_switch_append_case sw lit ('tag `(handler FT payloads...) anchor)
        sc_switch_append_default sw `(illegal-case)
        sw

# tagged union / sum type
typedef Enum
    inline literal (self)
        extractvalue self 0
    spice __unsafe-dispatch2 (self other enum-value handlers...)
        call
            gen-dispatch-from-tag
                inline (T self other)
                    _
                        _extract-payload self T false
                        _extract-payload other T false
            \ enum-value handlers... self other
    spice __dispatch (self handlers...)
        let tag = `(extractvalue self 0)
        call
            gen-dispatch-from-tag
                inline (T self)
                    _extract-payload self T
            \ tag handlers... self
    spice apply (self handler)
        let tag = `(extractvalue self 0)
        call
            gen-apply-from-tag
                inline (T self)
                    _extract-payload self T
            \ tag handler self

fn define-field-runtime (T name field-type index-value)
    let FieldType = (('@ T 'FieldType) as type)
    let fields = ('@ T '__fields__)
    let index = (('@ T '__index__) as u64)
    let index-anchor = ('anchor index-value)
    let index-value =
        if (('typeof index-value) == Nothing) index
        else (extract-integer index-value)
    let next-index = (index-value + 1)
    let FT = (typename.type
        (.. "enum-field<" (name as Symbol as string) ":"
            (tostring (field-type as type)) "=" (tostring index-value) ">")
        FieldType)
    'set-opaque FT
    'set-symbols FT
        Name = name
        Type = field-type
        Index = index-value
    let FT = `FT
    let fields =
        sc_argument_list_join_values fields FT
    sc_type_set_symbol T '__fields__ fields
    sc_type_set_symbol T '__index__ next-index
    index-value

spice define-field (enum-type name opts...)
    if (not ('constant? enum-type))
        error "enum-type must be constant"
    let T = (enum-type as type)
    let argc = ('argcount opts...)
    let field-type index-value =
        if (argc == 0) (_ `Nothing `none)
        elseif (argc == 1) (_ ('getarg opts... 0) `none)
        else (_ ('getarg opts... 0) ('getarglist opts... 1))
    define-field-runtime T name field-type index-value

fn shabbysort (buf sz)
    for i in (range sz)
        for j in (range (i + 1) sz)
            let a = (load (getelementptr buf i))
            let ak = (extractvalue a 0)
            let b = (load (getelementptr buf j))
            let bk = (extractvalue b 0)
            if (ak > bk)
                # swap
                store b (getelementptr buf i)
                store a (getelementptr buf j)

spice static-repr-switch-case (litT self allow-dupes? style? field-types...)
    litT as:= type
    allow-dupes? as:= bool
    style? as:= bool
    let value = (sc_const_int_extract self)
    let numfields = ('argcount field-types...)
    let sorted = (alloca-array (tuple u64 type) numfields)
    for i in (range numfields)
        let field = (('getarg field-types... i) as type)
        let field = (field as type)
        let index = (('@ field 'Index) as u64)
        store (tupleof index field) (getelementptr sorted i)
    # sort array so duplicates are next to each other and merge names
    shabbysort sorted numfields    
    loop (i = 0)
        if (i == numfields)
            break `"?invalid?"        
        let index field = (unpack (load (getelementptr sorted i)))
        if (index == value)
            let name = (('@ field 'Name) as Symbol as string)
            # accumulate all fields with the same index
            let name =
                loop (i name = (i + 1) name)
                    if (i == numfields)
                        break name
                    let index2 field2 = (unpack (load (getelementptr sorted i)))
                    if (index2 == index)
                        if (not allow-dupes?)
                            error "duplicate tags not permitted for tagged unions"
                        let name2 = (('@ field2 'Name) as Symbol as string)
                        repeat (i + 1) (.. name "|" name2)
                    else
                        break name
            let name =
                if style?
                    sc_default_styler style-number name
                else name
            break `name    
        i + 1

fn build-repr-switch-case (litT self allow-dupes? style? field-types)
    let numfields = ('argcount field-types)
    let sorted = (alloca-array (tuple u64 type) numfields)
    for i in (range numfields)
        let field = (('getarg field-types i) as type)
        let field = (field as type)
        let index = (('@ field 'Index) as u64)
        store (tupleof index field) (getelementptr sorted i)
    # sort array so duplicates are next to each other and merge names
    shabbysort sorted numfields
    let sw = (sc_switch_new self)
    loop (i = 0)
        if (i == numfields)
            break;
        let index field = (unpack (load (getelementptr sorted i)))
        let lit = (sc_const_int_new litT index)
        let name = (('@ field 'Name) as Symbol as string)
        # accumulate all fields with the same index
        let i name =
            loop (i name = (i + 1) name)
                if (i == numfields)
                    break i name
                let index2 field2 = (unpack (load (getelementptr sorted i)))
                if (index2 == index)
                    if (not allow-dupes?)
                        error "duplicate tags not permitted for tagged unions"
                    let name2 = (('@ field2 'Name) as Symbol as string)
                    repeat (i + 1) (.. name "|" name2)
                else
                    break i name
        let name =
            if style?
                sc_default_styler style-number name
            else name
        sc_switch_append_case sw lit name
        i
    sc_switch_append_default sw "?invalid?"
    sw

# constructor for unit tags without payload
inline unit-tag-constructor (enum-type index-value payload-type)
    let value = (undef enum-type)
    let value = (insertvalue value index-value 0)
    insertvalue value (nullof payload-type) 1

inline tag-constructor (
    enum-type index-value payload-type field-type field-pointer-type ...)
    let payload = (alloca payload-type)
    let PT = (typeof payload)
    let destptr = (bitcast payload field-pointer-type)
    let payload = (field-type ...)
    store payload destptr
    let payload = (load (bitcast destptr PT))
    let value = (undef enum-type)
    let value = (insertvalue value index-value 0)
    insertvalue value payload 1

fn finalize-enum-runtime (T storage)
    let field-types = ('@ T '__fields__)
    let field-type-args = ('args field-types)
    # figure out integer bit width and signedness for the enumerator
    let width signed =
        fold (width signed = 0 false) for field in field-type-args
            let field = (field as type)
            let index = (('@ field 'Index) as u64 as i64)
            let signed = (signed | (index < 0))
            let iwidth =
                if signed
                    if ((index >= -0x80) and (index < 0x80)) 8
                    elseif ((index >= -0x8000) and (index < 0x8000)) 16
                    elseif ((index >= -0x80000000) and (index < 0x80000000)) 32
                    else 64
                else
                    if (index <= 1) 1
                    if (index <= 0xff) 8
                    elseif (index <= 0xffff) 16
                    elseif (index <= 0xffffffff) 32
                    else 64
            _ (max width iwidth) signed

    inline check-field-redefinition (name finalized-fieldT)
        # if field has already been seen, its "last type" is finalized-fieldT
        # ie. this checks if we defined the same tag twice.
        let previously-definedT =
            try
                'typeof ('@ T (name as Symbol))
            except (ex)
                return;
        if (previously-definedT == finalized-fieldT)
            hide-traceback;
            error (.. "Duplicate enum fields aren't allowed: " (repr (name as Symbol)) ":" (repr T))

    let classic? =
        if (T < CEnum) true
        elseif (T < Enum) false
        else
            error
                .. "type " (repr T) " must have Enum or CEnum supertype"
                    \ " but has supertype " (repr ('superof T))
    local using-scope = (Scope)
    if classic?
        # no storage definition means `plain` was used.
        let storage =
            if (('typeof storage) == Nothing) i32
            else
                if (('typeof storage) == type)
                    storage as type
                else
                    hide-traceback;
                    error (.. "type expected, got " (tostring ('typeof storage)) ".")
        if (not (storage < integer))
            error "enum storage must be of integer type."
        'set-plain-storage T storage
        # enum is integer
        for field in field-type-args
            let field = (field as type)
            let name = (('@ field 'Name) as Symbol)
            check-field-redefinition name T
            let index = (('@ field 'Index) as u64)
            let field-type = (('@ field 'Type) as type)
            if (field-type != Nothing)
                error "plain enums can't have tagged fields"
            let value = (sc_const_int_new T index)
            'set-symbol T name value
            using-scope =
                'bind using-scope name value
        # build repr function
        spice-quote
            fn repr (self)
                spice-unquote
                    build-repr-switch-case T self true true field-types
            fn tostring (self)
                spice-unquote
                    build-repr-switch-case T self true false field-types
            inline __repr (self)
                static-if (constant? self)
                    static-repr-switch-case T self true true field-types
                else
                    repr self
            inline __tostring (self)
                static-if (constant? self)
                    static-repr-switch-case T self true false field-types
                else
                    tostring self
        'set-symbol T '__repr __repr
        'set-symbol T '__tostring __tostring
    else
        let index-type = (sc_integer_type width signed)
        let numfields = ('argcount field-types)
        inline drop-default ()
        inline... drop-any
        case (arg : Nothing,)
        case (args...)
            va-map __drop args...
            ;
        inline cmp-default () false
        inline hash-default () (nullof hash)
        # build repr function
        spice-quote
            fn repr (self)
                let val = (extractvalue self 0)
                spice-unquote
                    build-repr-switch-case index-type val false true field-types
            fn tostring (self)
                let val = (extractvalue self 0)
                spice-unquote
                    build-repr-switch-case index-type val false false field-types
            inline __repr (self)
                let val = (extractvalue self 0)
                static-if (constant? val)
                    static-repr-switch-case index-type val false true field-types
                else
                    repr self
            inline __tostring (self)
                let val = (extractvalue self 0)
                static-if (constant? val)
                    static-repr-switch-case index-type val false false field-types
                else
                    tostring self
            fn __drop (self)
                #print "dropping option" self
                '__dispatch self
                    spice-unquote
                        sc_argument_list_map_new (numfields + 1)
                            inline (i)
                                if (i == numfields)
                                    # default field
                                    `drop-default
                                else
                                    let field = (('getarg field-types i) as type)
                                    let name = (('@ field 'Name) as Symbol)
                                    sc_keyed_new name drop-any
            fn __hash (self)
                let tag = (extractvalue self 0)
                inline hash-with-tag (self)
                    hash tag self
                '__dispatch self
                    spice-unquote
                        sc_argument_list_map_new (numfields + 1)
                            inline (i)
                                if (i == numfields)
                                    # default field
                                    `hash-default
                                else
                                    let field = (('getarg field-types i) as type)
                                    let name = (('@ field 'Name) as Symbol)
                                    sc_keyed_new name hash-with-tag
            inline __== (A B)
                static-if (A == B)
                    fn Enum== (a b)
                        let tag-a = (extractvalue a 0)
                        let tag-b = (extractvalue b 0)
                        if (icmp== tag-a tag-b)
                            '__unsafe-dispatch2 a b tag-a
                                spice-unquote
                                    sc_argument_list_map_new (numfields + 1)
                                        inline (i)
                                            if (i == numfields)
                                                # default field
                                                `cmp-default
                                            else
                                                let field = (('getarg field-types i) as type)
                                                let name = (('@ field 'Name) as Symbol)
                                                sc_keyed_new name `==
                        else false
                elseif (B == type)
                    inline (a b)
                        static-assert (b < A.FieldType)
                        let tag = (extractvalue a 0)
                        let lit = b.Literal
                        tag == lit

        fn set-symbol (T key value)
            # only set symbol when it's not already set
            try ('@ T key)
                return;
            else
                'set-symbol T key value
        set-symbol T '__repr __repr
        set-symbol T '__tostring __tostring
        set-symbol T '__drop __drop
        set-symbol T '__== __==
        set-symbol T '__hash __hash
        let fields = (alloca-array type numfields)
        for i in (range numfields)
            let field = (('getarg field-types i) as type)
            let name = (('@ field 'Name) as Symbol)
            let field-type = (('@ field 'Type) as type)
            let field-type = ('key-type field-type name)
            store field-type (getelementptr fields i)
        'set-symbols T
            __fields = (sc_tuple_type numfields fields)
        let payload-type = (sc_union_storage_type numfields fields)
        'set-storage T
            tuple.type index-type payload-type
        let consts = (alloca-array Value 2)
        # build value constructors
        for i _field in (enumerate field-type-args)
            let field = (_field as type)
            let name = (('@ field 'Name) as Symbol)
            check-field-redefinition name type
            let index = (('@ field 'Index) as u64)
            let field-type = (('@ field 'Type) as type)
            let index-value = (sc_const_int_new index-type index)
            'set-symbol field 'Literal index-value
            let constructor =
                if (field-type == Nothing)
                    spice-quote
                        inline constructor (cls)
                            unit-tag-constructor T index-value payload-type
                    sc_template_set_name constructor name
                    constructor
                else
                    let TT = ('change-storage-class
                            ('mutable (pointer.type field-type)) 'Function)
                    spice-quote
                        inline constructor (cls ...)
                            tag-constructor T index-value payload-type field-type TT ...
                    sc_template_set_name constructor name
                    constructor
            'set-symbol field '__typecall constructor
            'set-symbol T name field
            using-scope =
                'bind using-scope name field
    'set-symbol T '__using (deref using-scope)
    ;

spice finalize-enum (T storage)
    if ('constant? T)
        finalize-enum-runtime (T as type) storage
        `()
    else
        `(finalize-enum-runtime T storage)

sugar enum (name body...)

    let supertype body has-supertype? storage =
        sugar-match body...
        case ('plain rest...)
            _ `CEnum rest... true `none
        case (': storage rest...)
            _ `CEnum rest... true `storage
        case ('< supertype rest...)
            _ supertype rest... true `none
        default
            _ `Enum body... false `none

    let has-fwd-decl =
        if (('typeof name) == Symbol)
            if (empty? body)
                # forward declaration
                return
                    qq [typedef] [name] < [supertype] do

            let symname = (name as Symbol)
            # see if we can find a forward declaration in the local scope
            try ('@ sugar-scope symname) true
            except (err) false
        else false

    # detect and rewrite top level field forms
    let body =
        loop (result body = '() body)
            if (empty? body)
                break ('reverse result)
            let expr next = (decons body)
            let anchor = ('anchor expr)
            let this-type =
                'tag `'this-type anchor
            let exprT = ('typeof expr)
            let expr =
                if (exprT == Symbol)
                    let newexpr =
                        qq [let] [expr] =
                            [define-field] [this-type] '[expr] [Nothing]
                    `newexpr
                elseif (exprT == list)
                    sugar-match (expr as list)
                    case ((name is Symbol) ': T...)
                        let newexpr =
                            qq [let] [name] =
                                [define-field] [this-type] '[name] ([tuple] (unquote-splice T...))
                        `newexpr
                    case ((name is Symbol) '= index...)
                        let newexpr =
                            qq [let] [name] =
                                [define-field] [this-type] '[name] [Nothing]
                                    unquote-splice index...
                        `newexpr
                    default expr
                else expr
            repeat (cons expr result) next

    spice init-fields (enum-type)
        fn init-fields-runtime (T)
            sc_type_set_symbol T '__fields__ (sc_argument_list_new 0 null)
            sc_type_set_symbol T '__index__ (sc_const_int_new u64 0:u64)
            let FT = (typename.type "FieldType" typename)
            'set-opaque FT
            sc_type_set_symbol T 'FieldType FT

        if ('constant? enum-type)
            init-fields-runtime (enum-type as type)
            `()
        else
            `(init-fields-runtime enum-type)
    qq
        unquote-splice
            if has-fwd-decl
                if has-supertype?
                    hide-traceback;
                    error "completing enum declaration must not define a supertype"
                qq [typedef+] [name]
            else
                qq [typedef] [name] < [supertype] do
        [init-fields] this-type
        inline tag (...)
            [define-field] this-type ...
        [do]
            unquote-splice body
            [fold-locals] this-type [append-to-type]
        [finalize-enum] this-type [storage]
        this-type

typedef+ Enum
    # wrap for usage outside spices
    spice unsafe-extract-payload (enum-value extractT)
        _extract-payload enum-value extractT

do
    let enum Enum
    locals;