Play

Check-in [1b2a7e4296]
Login
Overview
Comment:When defining multiwords, : now replaces when:, else: replaces :.
Timelines: family | ancestors | descendants | both | alpha2-syntax-changes
Files: files | file ages | folders
SHA3-256: 1b2a7e4296973083782dcda8a1c048e3d71be113d00b5f47d9635799c17fdce3
User & Date: robin.hansen on 2021-01-24 07:12:18
Other Links: branch diff | manifest | tags
Context
2021-01-24
07:19
Add unit test to make sure defining functions without bodies is legal. Closed-Leaf check-in: 12f2f38909 user: robin.hansen tags: alpha2-syntax-changes
07:12
When defining multiwords, : now replaces when:, else: replaces :. check-in: 1b2a7e4296 user: robin.hansen tags: alpha2-syntax-changes
2021-01-23
17:41
Re-enable and correct source location test. check-in: a897d19fce user: robin.hansen tags: alpha2-syntax-changes
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Parser.elm from [cc94b41119] to [0674f5af9e].

559
560
561
562
563
564
565
566
567
568
569
570
571
572
573

574
575
576
577
578
579
580
                    MultiImpl [] impl
    in
    Parser.oneOf
        [ Parser.succeed (\typeSign -> Parser.Loop { def | metadata = { metadata | type_ = TypeSignature.UserProvided typeSign } })
            |. Parser.keyword (Token "type:" NoProblem)
            |. noiseParser
            |= typeSignatureParser
        , Parser.succeed (\type_ impl -> Parser.Loop { def | implementation = addWhenImpl ( type_, impl ) })
            |. Parser.keyword (Token "when:" NoProblem)
            |. noiseParser
            |= typeMatchParser
            |= implementationParser
        , Parser.succeed (\impl -> Parser.Loop { def | implementation = setDefaultImpl impl })
            |. Parser.keyword (Token ":" NoProblem)
            |. noiseParser

            |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
        , Parser.succeed (Parser.Done def)
        ]








|
|

<

|


>







559
560
561
562
563
564
565
566
567
568

569
570
571
572
573
574
575
576
577
578
579
580
                    MultiImpl [] impl
    in
    Parser.oneOf
        [ Parser.succeed (\typeSign -> Parser.Loop { def | metadata = { metadata | type_ = TypeSignature.UserProvided typeSign } })
            |. Parser.keyword (Token "type:" NoProblem)
            |. noiseParser
            |= typeSignatureParser
        , Parser.succeed (\impl -> Parser.Loop { def | implementation = setDefaultImpl impl })
            |. Parser.keyword (Token "else:" NoProblem)
            |. noiseParser

            |= implementationParser
        , Parser.succeed (\type_ impl -> Parser.Loop { def | implementation = addWhenImpl ( type_, impl ) })
            |. Parser.keyword (Token ":" NoProblem)
            |. noiseParser
            |= typeMatchParser
            |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
        , Parser.succeed (Parser.Done def)
        ]

Modified tests/Test/Parser.elm from [afb57faec7] to [d53c1bfa79].

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
...
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
                            : True 
                            : False 

                            defstruct: True
                            defstruct: False

                            defmulti: to-int
                            when: True
                              drop 1
                            when: False
                              drop 0
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                            defunion: Maybe a
                            : a
                            : Nil

                            defstruct: Nil

                            defmulti: if-present
                            when: a
                              !
                            when: Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................

                            defstruct: Box a
                            : element a

                            defstruct: Nil

                            defmulti: if-present
                            when: (Box a)
                              !
                            when: Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                            defstruct: Box a
                            : element a

                            defstruct: Nil

                            defmulti: if-present
                            type: (MaybeBox a) a -- a
                            when: (Box a)
                              !
                            when: Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
        , describe "Pattern matching"
            [ test "Single match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: zero?
                            when: Int( value 0 )
                              True
                            : False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
................................................................................
                            Expect.equal expectedAst ast
            , test "Recursive match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: pair?
                            when: List( tail List( tail Nil ) )
                              True
                            : False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "pair?"
................................................................................
                            Expect.equal expectedAst ast
            , test "Multiple match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: origo?
                            when: Pair( first 0 second 0 )
                              True
                            : False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "origo?"
................................................................................
                       : False

                       defstruct: True
                       defstruct: False

                       defmulti: from-int
                       type: Int -- Int
                       when: Int( value 0 )
                         False
                       when: Int
                         True

                       def: equal
                       : - from-int not

                       defmulti: not
                       when: True
                         False
                       : True
                       """

                    -- The ending source location for most definitions now ends where the next definition beings
                    -- This is not what we want (it includes too much white space), but it'll do for now.
                    expectedAst =
                        { types =
                            Dict.fromListBy AST.typeDefinitionName
................................................................................
                                , { name = "from-int"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int ] [ Type.Int ]
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 9 1 66)
                                                    (SourceLocation 16 1 149)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 11 7 108)
                                                        (SourceLocation 11 21 122)
                                                    )
                                                    Type.Int
                                                    [ ( "value", LiteralInt 0 ) ]
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 12 3 125)
                                                        (SourceLocation 13 1 131)
                                                    )
                                                    "False"
                                                ]
                                              )
                                            , ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 13 7 137)
                                                        (SourceLocation 14 3 143)
                                                    )
                                                    Type.Int
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 14 3 143)
                                                        (SourceLocation 16 1 149)
                                                    )
                                                    "True"
                                                ]
                                              )
                                            ]
                                            []
                                  }
                                , { name = "equal"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 16 1 149)
                                                    (SourceLocation 19 1 178)
                                                )
                                  , implementation =
                                        SoloImpl
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 3 162)
                                                    (SourceLocation 17 5 164)
                                                )
                                                "-"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 5 164)
                                                    (SourceLocation 17 14 173)
                                                )
                                                "from-int"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 14 173)
                                                    (SourceLocation 19 1 178)
                                                )
                                                "not"
                                            ]
                                  }
                                , { name = "not"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 19 1 178)
                                                    (SourceLocation 23 1 218)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 20 7 198)
                                                        (SourceLocation 21 3 205)
                                                    )
                                                    (Type.Custom "True")
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 21 3 205)
                                                        (SourceLocation 22 1 211)
                                                    )
                                                    "False"
                                                ]
                                              )
                                            ]
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 22 3 213)
                                                    (SourceLocation 23 1 218)
                                                )
                                                "True"
                                            ]
                                  }
                                ]
                        }
                in







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|






|

|







 







|





|
|





|
|






|
|





|
|












|
|





|
|




|
|




|
|









|
|





|
|





|
|







|
|







262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
...
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
...
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
                            : True 
                            : False 

                            defstruct: True
                            defstruct: False

                            defmulti: to-int
                            : True
                              drop 1
                            : False
                              drop 0
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                            defunion: Maybe a
                            : a
                            : Nil

                            defstruct: Nil

                            defmulti: if-present
                            : a
                              !
                            : Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................

                            defstruct: Box a
                            : element a

                            defstruct: Nil

                            defmulti: if-present
                            : (Box a)
                              !
                            : Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                            defstruct: Box a
                            : element a

                            defstruct: Nil

                            defmulti: if-present
                            type: (MaybeBox a) a -- a
                            : (Box a)
                              !
                            : Nil
                              drop
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
        , describe "Pattern matching"
            [ test "Single match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: zero?
                            : Int( value 0 )
                              True
                            else: False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
................................................................................
                            Expect.equal expectedAst ast
            , test "Recursive match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: pair?
                            : List( tail List( tail Nil ) )
                              True
                            else: False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "pair?"
................................................................................
                            Expect.equal expectedAst ast
            , test "Multiple match" <|
                \_ ->
                    let
                        source =
                            """
                            defmulti: origo?
                            : Pair( first 0 second 0 )
                              True
                            else: False
                            """

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "origo?"
................................................................................
                       : False

                       defstruct: True
                       defstruct: False

                       defmulti: from-int
                       type: Int -- Int
                       : Int( value 0 )
                         False
                       : Int
                         True

                       def: equal
                       : - from-int not

                       defmulti: not
                       : True
                         False
                       else: True
                       """

                    -- The ending source location for most definitions now ends where the next definition beings
                    -- This is not what we want (it includes too much white space), but it'll do for now.
                    expectedAst =
                        { types =
                            Dict.fromListBy AST.typeDefinitionName
................................................................................
                                , { name = "from-int"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int ] [ Type.Int ]
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 9 1 66)
                                                    (SourceLocation 16 1 141)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 11 3 104)
                                                        (SourceLocation 11 17 118)
                                                    )
                                                    Type.Int
                                                    [ ( "value", LiteralInt 0 ) ]
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 12 3 121)
                                                        (SourceLocation 13 1 127)
                                                    )
                                                    "False"
                                                ]
                                              )
                                            , ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 13 3 129)
                                                        (SourceLocation 14 3 135)
                                                    )
                                                    Type.Int
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 14 3 135)
                                                        (SourceLocation 16 1 141)
                                                    )
                                                    "True"
                                                ]
                                              )
                                            ]
                                            []
                                  }
                                , { name = "equal"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 16 1 141)
                                                    (SourceLocation 19 1 170)
                                                )
                                  , implementation =
                                        SoloImpl
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 3 154)
                                                    (SourceLocation 17 5 156)
                                                )
                                                "-"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 5 156)
                                                    (SourceLocation 17 14 165)
                                                )
                                                "from-int"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 14 165)
                                                    (SourceLocation 19 1 170)
                                                )
                                                "not"
                                            ]
                                  }
                                , { name = "not"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 19 1 170)
                                                    (SourceLocation 23 1 210)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 20 3 186)
                                                        (SourceLocation 21 3 193)
                                                    )
                                                    (Type.Custom "True")
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 21 3 193)
                                                        (SourceLocation 22 1 199)
                                                    )
                                                    "False"
                                                ]
                                              )
                                            ]
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 22 7 205)
                                                    (SourceLocation 23 1 210)
                                                )
                                                "True"
                                            ]
                                  }
                                ]
                        }
                in

Modified tests/Test/Parser/Error.elm from [948634a5ee] to [b45ca2f7fa].

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
                \_ ->
                    checkForError (wordAlreadyDefined "not") <|
                        """
                        def: not
                        : drop 0

                        defmulti: not
                        when: Int ( value 0)
                          drop 1
                        : drop 0
                        """
            , test "Generated double definitions" <|
                \_ ->
                    checkForError (wordAlreadyDefined "age>") <|
                        """
                        def: age>
                        : 1







|

|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
                \_ ->
                    checkForError (wordAlreadyDefined "not") <|
                        """
                        def: not
                        : drop 0

                        defmulti: not
                        : Int ( value 0)
                          drop 1
                        else: drop 0
                        """
            , test "Generated double definitions" <|
                \_ ->
                    checkForError (wordAlreadyDefined "age>") <|
                        """
                        def: age>
                        : 1

Modified wasm_tests/multiword.test.js from [08aaade640] to [aadc36f12d].

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
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
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
...
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
        : True
        : False

        defstruct: True
        defstruct: False

        defmulti: to-int
        when: True
          drop 100
        when: False
          drop 75

        def: main
        entry: true
        : True to-int 
          False to-int 
          -
................................................................................
        : True 
        : False

        defstruct: True
        defstruct: False

        defmulti: to-int
        when: True
          drop 100
        : drop 75

        def: main
        entry: true
        : True to-int 
          False to-int 
          -
    `);
................................................................................
        defstruct: Person
        : age Int

        defstruct: Dog
        : man-years Int

        defmulti: add-to-age
        when: Person
          swap dup age>
          -rotate +
          >age
        when: Dog
          4 * 
          swap dup man-years>
          -rotate +
          >man-years

        defmulti: get-man-age
        when: Person
          age>
        when: Dog
          man-years>

        def: main
        entry: true
        : 18 >Person 10 add-to-age 
          0 >Dog 2 add-to-age 
          get-man-age swap 
................................................................................
        defstruct: NonEmptyList a
        : first a
        : rest List a

        defstruct: EmptyList

        defmulti: first-or-default
        when: NonEmptyList
          drop first>
        when: EmptyList
          swap drop

        def: main
        entry: true
        : 1 EmptyList >NonEmptyList
          0 first-or-default
    `);
................................................................................
        defstruct: EmptyList

        def: sum
        : 0 sum-helper

        defmulti: sum-helper
        type: (List a) Int -- Int
        when: NonEmptyList
          swap dup first> rotate rest> rotate + sum-helper
        when: EmptyList
          swap drop

        def: main
        entry: true
        : 1 2 3 EmptyList >NonEmptyList >NonEmptyList >NonEmptyList
          sum
    `);
................................................................................
        defunion: IntParseResult
        : Int
        : NoInt

        defstruct: NoInt

        defmulti: double
        when: Int
          2 *
        when: NoInt
          drop 0

        def: main
        entry: true
        : 4 double
    `);

................................................................................

    expect(result.stackElement()).toBe(8);
});

test('Int match', async () => {
    const wat = await compiler.toWat(`
        defmulti: double
        when: Int( value 0 )
          drop 2
        when: Int
          2 *

        def: main
        entry: true
        : 0 double
    `);

................................................................................

    expect(result.stackElement()).toBe(2);
});

test('Int match (reverse)', async () => {
    const wat = await compiler.toWat(`
        defmulti: double
        when: Int( value 0 )
          drop 2
        when: Int
          2 *

        def: main
        entry: true
        : 6 double
    `);

................................................................................

    expect(result.stackElement()).toBe(12);
});

test('Correct Int boxing behaviour', async () => {
    const wat = await compiler.toWat(`
        defmulti: add
        when: Int( value 0 )
          swap 
          drop 2
          swap
          +
        when: Int
          +

        def: main
        entry: true
        : 10 6 add
    `);

................................................................................
});

test('Correct Int boxing behaviour when mismatch between word input size and stack size', async () => {
    const wat = await compiler.toWat(`
        defstruct: Nil

        defmulti: inc-zero
        when: Int( value 0 )
          swap 
          drop 1
          swap
        when: Int

        def: main
        entry: true
        : 0 Nil inc-zero
          drop 
    `);

................................................................................
        : a
        : Nil

        defstruct: Nil

        defmulti: map
        type: (Maybe a) [ a -- b ] -- (Maybe b)
        when: a
          !
        when: Nil
          drop

        defmulti: with-default
        type: (Maybe a) a -- a
        when: a
          drop
        when: Nil
          swap drop

        def: main
        entry: true
        : 10
          [ 1 - ] map
          0 with-default
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(9);
});







|

|







 







|

|







 







|



|






|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|




|







 







|



|







 







|

|




|

|













6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
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
...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
...
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
        : True
        : False

        defstruct: True
        defstruct: False

        defmulti: to-int
        : True
          drop 100
        : False
          drop 75

        def: main
        entry: true
        : True to-int 
          False to-int 
          -
................................................................................
        : True 
        : False

        defstruct: True
        defstruct: False

        defmulti: to-int
        : True
          drop 100
        else: drop 75

        def: main
        entry: true
        : True to-int 
          False to-int 
          -
    `);
................................................................................
        defstruct: Person
        : age Int

        defstruct: Dog
        : man-years Int

        defmulti: add-to-age
        : Person
          swap dup age>
          -rotate +
          >age
        : Dog
          4 * 
          swap dup man-years>
          -rotate +
          >man-years

        defmulti: get-man-age
        : Person
          age>
        : Dog
          man-years>

        def: main
        entry: true
        : 18 >Person 10 add-to-age 
          0 >Dog 2 add-to-age 
          get-man-age swap 
................................................................................
        defstruct: NonEmptyList a
        : first a
        : rest List a

        defstruct: EmptyList

        defmulti: first-or-default
        : NonEmptyList
          drop first>
        : EmptyList
          swap drop

        def: main
        entry: true
        : 1 EmptyList >NonEmptyList
          0 first-or-default
    `);
................................................................................
        defstruct: EmptyList

        def: sum
        : 0 sum-helper

        defmulti: sum-helper
        type: (List a) Int -- Int
        : NonEmptyList
          swap dup first> rotate rest> rotate + sum-helper
        : EmptyList
          swap drop

        def: main
        entry: true
        : 1 2 3 EmptyList >NonEmptyList >NonEmptyList >NonEmptyList
          sum
    `);
................................................................................
        defunion: IntParseResult
        : Int
        : NoInt

        defstruct: NoInt

        defmulti: double
        : Int
          2 *
        : NoInt
          drop 0

        def: main
        entry: true
        : 4 double
    `);

................................................................................

    expect(result.stackElement()).toBe(8);
});

test('Int match', async () => {
    const wat = await compiler.toWat(`
        defmulti: double
        : Int( value 0 )
          drop 2
        : Int
          2 *

        def: main
        entry: true
        : 0 double
    `);

................................................................................

    expect(result.stackElement()).toBe(2);
});

test('Int match (reverse)', async () => {
    const wat = await compiler.toWat(`
        defmulti: double
        : Int( value 0 )
          drop 2
        : Int
          2 *

        def: main
        entry: true
        : 6 double
    `);

................................................................................

    expect(result.stackElement()).toBe(12);
});

test('Correct Int boxing behaviour', async () => {
    const wat = await compiler.toWat(`
        defmulti: add
        : Int( value 0 )
          swap 
          drop 2
          swap
          +
        : Int
          +

        def: main
        entry: true
        : 10 6 add
    `);

................................................................................
});

test('Correct Int boxing behaviour when mismatch between word input size and stack size', async () => {
    const wat = await compiler.toWat(`
        defstruct: Nil

        defmulti: inc-zero
        : Int( value 0 )
          swap 
          drop 1
          swap
        : Int

        def: main
        entry: true
        : 0 Nil inc-zero
          drop 
    `);

................................................................................
        : a
        : Nil

        defstruct: Nil

        defmulti: map
        type: (Maybe a) [ a -- b ] -- (Maybe b)
        : a
          !
        : Nil
          drop

        defmulti: with-default
        type: (Maybe a) a -- a
        : a
          drop
        : Nil
          swap drop

        def: main
        entry: true
        : 10
          [ 1 - ] map
          0 with-default
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(9);
});

Modified wasm_tests/pattern_match.test.js from [96320590e3] to [72e613569d].

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

test('Basic pattern match', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        when: Box( value 0 )
          drop 1
        when: Box
          drop 0

        def: main
        entry: true
        : 0 >Box not
    `);

................................................................................

test('Basic pattern match with default implementation', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        when: Box( value 0 )
          drop 1
        : drop 0

        def: main
        entry: true
        : 0 >Box not
    `);

    const result = await compiler.run(wat, 'main');
................................................................................

test('Basic pattern match reverse case', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        when: Box( value 0 )
          drop 1
        : drop 0

        def: main
        entry: true
        : 1 >Box not
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
test('Multiple arguments', async () => {
    const wat = await compiler.toWat(`
        defstruct: Point
        : first Int
        : second Int

        defmulti: origo?
        when: Point( first 0 second 0 )
          drop 1
        : drop 0

        def: main
        entry: true
        : 0 0 >Point origo?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
test('Multiple arguments reverse case', async () => {
    const wat = await compiler.toWat(`
        defstruct: Point
        : first Int
        : second Int

        defmulti: origo?
        when: Point( first 0 second 0 )
          drop 1
        : drop 0

        def: main
        entry: true
        : 0 1 >Point origo?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
        defstruct: Box
        : value Int

        defstruct: BoxOfBox
        : box Box

        defmulti: deep-one?
        when: BoxOfBox( box Box( value 1 ) )
          drop 1
        : drop 0

        def: main
        entry: true
        : 1 >Box >BoxOfBox deep-one?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
        defstruct: Box
        : value Int

        defstruct: BoxOfBox
        : box Box

        defmulti: deep-one?
        when: BoxOfBox( box Box( value 1 ) )
          drop 1
        : drop 0

        def: main
        entry: true
        : 2 >Box >BoxOfBox deep-one?
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(0);
});







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|







 







|

|










2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
..
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
..
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

test('Basic pattern match', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        : Box( value 0 )
          drop 1
        : Box
          drop 0

        def: main
        entry: true
        : 0 >Box not
    `);

................................................................................

test('Basic pattern match with default implementation', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        : Box( value 0 )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 0 >Box not
    `);

    const result = await compiler.run(wat, 'main');
................................................................................

test('Basic pattern match reverse case', async () => {
    const wat = await compiler.toWat(`
        defstruct: Box
        : value Int

        defmulti: not
        : Box( value 0 )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 1 >Box not
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
test('Multiple arguments', async () => {
    const wat = await compiler.toWat(`
        defstruct: Point
        : first Int
        : second Int

        defmulti: origo?
        : Point( first 0 second 0 )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 0 0 >Point origo?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
test('Multiple arguments reverse case', async () => {
    const wat = await compiler.toWat(`
        defstruct: Point
        : first Int
        : second Int

        defmulti: origo?
        : Point( first 0 second 0 )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 0 1 >Point origo?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
        defstruct: Box
        : value Int

        defstruct: BoxOfBox
        : box Box

        defmulti: deep-one?
        : BoxOfBox( box Box( value 1 ) )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 1 >Box >BoxOfBox deep-one?
    `);

    const result = await compiler.run(wat, 'main');
................................................................................
        defstruct: Box
        : value Int

        defstruct: BoxOfBox
        : box Box

        defmulti: deep-one?
        : BoxOfBox( box Box( value 1 ) )
          drop 1
        else: drop 0

        def: main
        entry: true
        : 2 >Box >BoxOfBox deep-one?
    `);

    const result = await compiler.run(wat, 'main');

    expect(result.stackElement()).toBe(0);
});