Play

Check-in [5de766c3d5]
Login
Overview
Comment:Make syntax changes based on language proposal [49d490f836]. Fixes tickets [05dc3c652b], [2ed5dbbb6d] and [426b0366e4].
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5de766c3d5e7118c84d6de68639b45b3262673926de3f86bc92d6954eaed0e86
User & Date: robin.hansen on 2021-01-24 07:22:34
Other Links: manifest | tags
Context
2021-01-24
09:52
Remove warning about being compiled in debug mode from elm bundle, which cleans up the output from t... check-in: fd2d78e20e user: robin.hansen tags: trunk
07:22
Make syntax changes based on language proposal [49d490f836]. Fixes tickets [05dc3c652b], [2ed5dbbb6d... check-in: 5de766c3d5 user: robin.hansen tags: trunk
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
2021-01-17
10:32
Simplify tests by automating ctor and getter/setter boilerplate. check-in: e9d8932285 user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

156
157
158
159
160
161
162


















163
164
165
166
167
168
169
170
171
172
173
174
175
176
...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
...
389
390
391
392
393
394
395





396
397
398
399
400
401
402
403
...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550

551
552
553
554
555
556
557
...
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
                |. Parser.symbol (Token ":" NotMetadata)
                |> Parser.andThen (\_ -> Parser.problem FoundMetadata)
            , Parser.succeed identity
            ]
        |. noiseParser
        |> Parser.backtrackable




















definitionMetadataParser : Parser String
definitionMetadataParser =
    Parser.variable
        { start = \c -> not (Char.isDigit c || Char.isUpper c || Set.member c invalidSymbolChars)
        , inner = validSymbolChar
        , reserved = Set.fromList [ "def", "defmulti", "deftype", "defunion" ]
        , expecting = NotSymbol
        }
        |. Parser.symbol (Token ":" NotMetadata)


genericParser : Parser String
genericParser =
................................................................................
            |> Parser.andThen insertWord
        , sourceLocationParser
            |. Parser.keyword (Token "defmulti:" NoProblem)
            |. noiseParser
            |> Parser.andThen multiWordDefinitionParser
            |> Parser.andThen insertWord
        , sourceLocationParser
            |. Parser.keyword (Token "deftype:" NoProblem)
            |. noiseParser
            |> Parser.andThen typeDefinitionParser
            |> Parser.andThen insertType
        , sourceLocationParser
            |. Parser.keyword (Token "defunion:" NoProblem)
            |. noiseParser
            |> Parser.andThen unionTypeDefinitionParser
................................................................................
                        [] ->
                            Type.Custom typeName

                        _ ->
                            Type.CustomGeneric typeName (List.map Type.Generic binds)

                ctorDef =





                    { name = ">" ++ typeName
                    , metadata =
                        Metadata.default
                            |> Metadata.withVerifiedType (List.map Tuple.second typeMembers) [ typeOfType ]
                    , implementation =
                        SoloImpl [ ConstructType typeName ]
                    }

................................................................................
                    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)
        ]

................................................................................
    Parser.oneOf
        [ Parser.succeed (\startLoc value endLoc -> Integer (SourceLocationRange startLoc endLoc) value)
            |= sourceLocationParser
            |= intParser
            |= sourceLocationParser
        , Parser.succeed (\startLoc value endLoc -> Word (SourceLocationRange startLoc endLoc) value)
            |= sourceLocationParser
            |= symbolParser
            |= sourceLocationParser
        ]


typeDefinitionName : TypeDefinition -> String
typeDefinitionName typeDef =
    case typeDef of







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






|







 







|







 







>
>
>
>
>
|







 







|
|

<

|


>







 







|







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
...
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
...
559
560
561
562
563
564
565
566
567
568

569
570
571
572
573
574
575
576
577
578
579
580
...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
                |. Parser.symbol (Token ":" NotMetadata)
                |> Parser.andThen (\_ -> Parser.problem FoundMetadata)
            , Parser.succeed identity
            ]
        |. noiseParser
        |> Parser.backtrackable


symbolImplParser : Parser String
symbolImplParser =
    Parser.variable
        { start = \c -> not (Char.isDigit c || Set.member c invalidSymbolChars)
        , inner = validSymbolChar
        , reserved = Set.empty
        , expecting = NotSymbol
        }
        |. Parser.oneOf
            [ Parser.succeed identity
                |. Parser.symbol (Token ":" NotMetadata)
                |> Parser.andThen (\_ -> Parser.problem FoundMetadata)
            , Parser.succeed identity
            ]
        |. noiseParser
        |> Parser.backtrackable


definitionMetadataParser : Parser String
definitionMetadataParser =
    Parser.variable
        { start = \c -> not (Char.isDigit c || Char.isUpper c || Set.member c invalidSymbolChars)
        , inner = validSymbolChar
        , reserved = Set.fromList [ "def", "defmulti", "defstruct", "defunion" ]
        , expecting = NotSymbol
        }
        |. Parser.symbol (Token ":" NotMetadata)


genericParser : Parser String
genericParser =
................................................................................
            |> Parser.andThen insertWord
        , sourceLocationParser
            |. Parser.keyword (Token "defmulti:" NoProblem)
            |. noiseParser
            |> Parser.andThen multiWordDefinitionParser
            |> Parser.andThen insertWord
        , sourceLocationParser
            |. Parser.keyword (Token "defstruct:" NoProblem)
            |. noiseParser
            |> Parser.andThen typeDefinitionParser
            |> Parser.andThen insertType
        , sourceLocationParser
            |. Parser.keyword (Token "defunion:" NoProblem)
            |. noiseParser
            |> Parser.andThen unionTypeDefinitionParser
................................................................................
                        [] ->
                            Type.Custom typeName

                        _ ->
                            Type.CustomGeneric typeName (List.map Type.Generic binds)

                ctorDef =
                    { name =
                        if List.isEmpty typeMembers then
                            typeName

                        else
                            ">" ++ typeName
                    , metadata =
                        Metadata.default
                            |> Metadata.withVerifiedType (List.map Tuple.second typeMembers) [ typeOfType ]
                    , implementation =
                        SoloImpl [ ConstructType typeName ]
                    }

................................................................................
                    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)
        ]

................................................................................
    Parser.oneOf
        [ Parser.succeed (\startLoc value endLoc -> Integer (SourceLocationRange startLoc endLoc) value)
            |= sourceLocationParser
            |= intParser
            |= sourceLocationParser
        , Parser.succeed (\startLoc value endLoc -> Word (SourceLocationRange startLoc endLoc) value)
            |= sourceLocationParser
            |= symbolImplParser
            |= sourceLocationParser
        ]


typeDefinitionName : TypeDefinition -> String
typeDefinitionName typeDef =
    case typeDef of

Modified tests/Test/Parser.elm from [4e42e708d8] to [a3937d228f].

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
...
426
427
428
429
430
431
432
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
...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
...
745
746
747
748
749
750
751






752
753
754
755
756
757
758
759
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
...
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
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
938
939
940
941
942
943
944
import Dict.Extra as Dict
import Expect
import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (SourceLocation, SourceLocationRange, emptyRange)
import Play.Data.Type as Type
import Play.Parser as AST exposing (..)
import Test exposing (Test, describe, test)
import Test.Parser.Util exposing (addFunctionsForStructs, compile, compileRetainLocations)


suite : Test
suite =
    describe "Parser"
        [ test "Sample program" <|
            \_ ->
................................................................................
                        Expect.equal expectedAst ast
        , describe "Custom data structures"
            [ test "Without members" <|
                \_ ->
                    let
                        source =
                            """
                            deftype: True
                            
                            def: as-int
                            type: True -- Int
                            : 1
                            """

                        expectedAst =
................................................................................
                        Ok ast ->
                            Expect.equal expectedAst ast
            , test "With members" <|
                \_ ->
                    let
                        source =
                            """
                            deftype: Person
                            : age Int
                            : jobs Int

                            def: get-age
                            type: Person -- Int
                            : age>
                            """
................................................................................
                        Ok ast ->
                            Expect.equal expectedAst ast
            , test "Generic members" <|
                \_ ->
                    let
                        source =
                            """
                            deftype: Box a
                            : element a
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ CustomTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: Bool
                            : True 
                            : False 

                            deftype: True
                            deftype: False

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

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

                            deftype: Nil

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

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: MaybeBox a
                            : Box a
                            : Nil

                            deftype: Box a
                            : element a

                            deftype: Nil

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

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: MaybeBox a
                            : Box a
                            : Nil

                            deftype: Box a
                            : element a

                            deftype: 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?"
................................................................................
                                      , metadata = Metadata.default
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange
                                                        Type.Int
                                                        [ ( "value", AST.LiteralInt 0 )
                                                        ]
                                                  , [ AST.Word emptyRange ">True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange ">False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"
................................................................................
                            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?"
................................................................................
                                                                (TypeMatch emptyRange
                                                                    (Type.Custom "List")
                                                                    [ ( "tail", AST.LiteralType (Type.Custom "Nil") )
                                                                    ]
                                                                )
                                                          )
                                                        ]
                                                  , [ AST.Word emptyRange ">True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange ">False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"
................................................................................
                            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?"
................................................................................
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange
                                                        (Type.Custom "Pair")
                                                        [ ( "first", AST.LiteralInt 0 )
                                                        , ( "second", AST.LiteralInt 0 )
                                                        ]
                                                  , [ AST.Word emptyRange ">True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange ">False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"

                        Ok ast ->
                            Expect.equal expectedAst ast
            ]
        , test "Support code comments" <|
            \_ ->
                let
                    expectCompiles code =
                        case compile code of
                            Err _ ->
                                Expect.fail "Did not expect compilation to fail."

                            Ok _ ->
                                Expect.pass
                in
                expectCompiles
                    """
                    # Increments the passed in value by one
                    def: inc
                    type: # as you'd expect
                      Int -- Int
                    : # again, pretty basic
................................................................................
                    type: Int -- Int
                    : 1
                      # + wouldnt work here
                      -

                    # And thats it!
                     # wonder what else we should do...






                    """
        , test "Correct line information" <|
            \_ ->
                let
                    source =
                        """
                        defunion: Bool
                        : True
                        : False

                        deftype: True
                        deftype: 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
................................................................................
                                    []
                                    [ Type.Custom "True"
                                    , Type.Custom "False"
                                    ]
                                , CustomTypeDef
                                    (SourceLocationRange
                                        (SourceLocation 6 1 32)
                                        (SourceLocation 7 1 46)
                                    )
                                    "True"
                                    []
                                    []
                                , CustomTypeDef
                                    (SourceLocationRange
                                        (SourceLocation 7 1 46)
                                        (SourceLocation 9 1 62)
                                    )
                                    "False"
                                    []
                                    []
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">True"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withVerifiedType [] [ Type.Custom "True" ]
                                  , implementation = SoloImpl [ ConstructType "True" ]
                                  }
                                , { name = ">False"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withVerifiedType [] [ Type.Custom "False" ]
                                  , implementation = SoloImpl [ ConstructType "False" ]
                                  }
                                , { name = "from-int"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int ] [ Type.Int ]
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 9 1 62)
                                                    (SourceLocation 16 1 147)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 11 7 104)
                                                        (SourceLocation 11 21 118)
                                                    )
                                                    Type.Int
                                                    [ ( "value", LiteralInt 0 ) ]
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 12 3 121)
                                                        (SourceLocation 13 1 128)
                                                    )
                                                    ">False"
                                                ]
                                              )
                                            , ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 13 7 134)
                                                        (SourceLocation 14 3 140)
                                                    )
                                                    Type.Int
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 14 3 140)
                                                        (SourceLocation 16 1 147)
                                                    )
                                                    ">True"
                                                ]
                                              )
                                            ]
                                            []
                                  }
                                , { name = "equal"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 16 1 147)
                                                    (SourceLocation 19 1 176)
                                                )
                                  , implementation =
                                        SoloImpl
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 3 160)
                                                    (SourceLocation 17 5 162)
                                                )
                                                "-"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 5 162)
                                                    (SourceLocation 17 14 171)
                                                )
                                                "from-int"
                                            , Word
                                                (SourceLocationRange
                                                    (SourceLocation 17 14 171)
                                                    (SourceLocation 19 1 176)
                                                )
                                                "not"
                                            ]
                                  }
                                , { name = "not"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withSourceLocationRange
                                                (SourceLocationRange
                                                    (SourceLocation 19 1 176)
                                                    (SourceLocation 23 1 218)
                                                )
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch
                                                    (SourceLocationRange
                                                        (SourceLocation 20 7 196)
                                                        (SourceLocation 21 3 203)
                                                    )
                                                    (Type.Custom "True")
                                                    []
                                              , [ Word
                                                    (SourceLocationRange
                                                        (SourceLocation 21 3 203)
                                                        (SourceLocation 22 1 210)
                                                    )
                                                    ">False"
                                                ]
                                              )
                                            ]
                                            [ Word
                                                (SourceLocationRange
                                                    (SourceLocation 22 3 212)
                                                    (SourceLocation 23 1 218)
                                                )
                                                ">True"
                                            ]
                                  }
                                ]
                        }
                in
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast
        ]







|







 







|







 







|







 







|







 







|
|


|

|







 







|


|

|







 







|


|


|

|







 







|


|



|

|







 







|
|
|







 







|


|







 







|
|
|







 







|


|







 







|
|
|







 







|


|













<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>










|
|



|
|
|
|





|
|
|







 







|






|
|







|





|











|
|





|
|






|

|




|
|





|
|

|










|
|





|
|




|
|




|
|









|
|





|
|





|
|

|





|
|

|












4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
...
426
427
428
429
430
431
432
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
...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
...
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722









723
724
725
726
727
728
729
...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
...
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
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
938
939
940
941
import Dict.Extra as Dict
import Expect
import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (SourceLocation, SourceLocationRange, emptyRange)
import Play.Data.Type as Type
import Play.Parser as AST exposing (..)
import Test exposing (Test, describe, test)
import Test.Parser.Util exposing (addFunctionsForStructs, compile, compileRetainLocations, expectCompiles)


suite : Test
suite =
    describe "Parser"
        [ test "Sample program" <|
            \_ ->
................................................................................
                        Expect.equal expectedAst ast
        , describe "Custom data structures"
            [ test "Without members" <|
                \_ ->
                    let
                        source =
                            """
                            defstruct: True
                            
                            def: as-int
                            type: True -- Int
                            : 1
                            """

                        expectedAst =
................................................................................
                        Ok ast ->
                            Expect.equal expectedAst ast
            , test "With members" <|
                \_ ->
                    let
                        source =
                            """
                            defstruct: Person
                            : age Int
                            : jobs Int

                            def: get-age
                            type: Person -- Int
                            : age>
                            """
................................................................................
                        Ok ast ->
                            Expect.equal expectedAst ast
            , test "Generic members" <|
                \_ ->
                    let
                        source =
                            """
                            defstruct: Box a
                            : element a
                            """

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ CustomTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: Bool
                            : True 
                            : False 

                            defstruct: True
                            defstruct: False

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

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

                            defstruct: Nil

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

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: MaybeBox a
                            : Box a
                            : Nil

                            defstruct: Box a
                            : element a

                            defstruct: Nil

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

                        expectedAst =
                            { types =
                                Dict.fromListBy AST.typeDefinitionName
                                    [ UnionTypeDef emptyRange
................................................................................
                    let
                        source =
                            """
                            defunion: MaybeBox a
                            : Box a
                            : Nil

                            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?"
................................................................................
                                      , metadata = Metadata.default
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange
                                                        Type.Int
                                                        [ ( "value", AST.LiteralInt 0 )
                                                        ]
                                                  , [ AST.Word emptyRange "True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange "False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"
................................................................................
                            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?"
................................................................................
                                                                (TypeMatch emptyRange
                                                                    (Type.Custom "List")
                                                                    [ ( "tail", AST.LiteralType (Type.Custom "Nil") )
                                                                    ]
                                                                )
                                                          )
                                                        ]
                                                  , [ AST.Word emptyRange "True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange "False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"
................................................................................
                            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?"
................................................................................
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange
                                                        (Type.Custom "Pair")
                                                        [ ( "first", AST.LiteralInt 0 )
                                                        , ( "second", AST.LiteralInt 0 )
                                                        ]
                                                  , [ AST.Word emptyRange "True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange "False" ]
                                      }
                                    ]
                            }
                    in
                    case compile source of
                        Err _ ->
                            Expect.fail "Did not expect parsing to fail"

                        Ok ast ->
                            Expect.equal expectedAst ast
            ]
        , test "Support code comments" <|
            \_ ->









                expectCompiles
                    """
                    # Increments the passed in value by one
                    def: inc
                    type: # as you'd expect
                      Int -- Int
                    : # again, pretty basic
................................................................................
                    type: Int -- Int
                    : 1
                      # + wouldnt work here
                      -

                    # And thats it!
                     # wonder what else we should do...
                    """
        , test "definition without implementation should be legal" <|
            \_ ->
                expectCompiles
                    """
                    def: someword
                    """
        , test "Correct line information" <|
            \_ ->
                let
                    source =
                        """
                        defunion: Bool
                        : True
                        : 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
................................................................................
                                    []
                                    [ Type.Custom "True"
                                    , Type.Custom "False"
                                    ]
                                , CustomTypeDef
                                    (SourceLocationRange
                                        (SourceLocation 6 1 32)
                                        (SourceLocation 7 1 48)
                                    )
                                    "True"
                                    []
                                    []
                                , CustomTypeDef
                                    (SourceLocationRange
                                        (SourceLocation 7 1 48)
                                        (SourceLocation 9 1 66)
                                    )
                                    "False"
                                    []
                                    []
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = "True"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withVerifiedType [] [ Type.Custom "True" ]
                                  , implementation = SoloImpl [ ConstructType "True" ]
                                  }
                                , { name = "False"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withVerifiedType [] [ Type.Custom "False" ]
                                  , implementation = SoloImpl [ ConstructType "False" ]
                                  }
                                , { 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
                case compileRetainLocations source of
                    Err _ ->
                        Expect.fail "Did not expect compilation to fail."

                    Ok ast ->
                        Expect.equal expectedAst ast
        ]

Modified tests/Test/Parser/Error.elm from [6b5a489c73] to [b45ca2f7fa].

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
..
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
                \_ ->
                    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

                        deftype: Person
                        : age Int
                        """
            , test "Type definition" <|
                \_ ->
                    let
                        source =
                            """
                            deftype: Person
                            : age Int

                            defunion: Person
                            : Person
                            """

                        typeAlreadyDefined name problem =
................................................................................
                          0
                        : 2 *
                        """
            , test "type" <|
                \_ ->
                    checkForError (expectedError "age") <|
                        """
                        deftype: Person
                        # wrong syntax
                        age: Int
                        """
            , test "union" <|
                \_ ->
                    checkForError (expectedError "member") <|
                        """
                        defunion: Gender
                        member: Male
                        member: Female
                        """
            ]







        ]


checkForError : (Problem -> Bool) -> String -> Expectation
checkForError fn source =
    case compile source of
        Err errors ->







|

|








|







|







 







|












>
>
>
>
>
>
>







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
..
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
                \_ ->
                    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

                        defstruct: Person
                        : age Int
                        """
            , test "Type definition" <|
                \_ ->
                    let
                        source =
                            """
                            defstruct: Person
                            : age Int

                            defunion: Person
                            : Person
                            """

                        typeAlreadyDefined name problem =
................................................................................
                          0
                        : 2 *
                        """
            , test "type" <|
                \_ ->
                    checkForError (expectedError "age") <|
                        """
                        defstruct: Person
                        # wrong syntax
                        age: Int
                        """
            , test "union" <|
                \_ ->
                    checkForError (expectedError "member") <|
                        """
                        defunion: Gender
                        member: Male
                        member: Female
                        """
            ]
        , test "Only compiler can create functions which names begins with an upper case letter" <|
            \_ ->
                checkForError ((==) NotSymbol) <|
                    """
                    def: Person
                    : 1
                    """
        ]


checkForError : (Problem -> Bool) -> String -> Expectation
checkForError fn source =
    case compile source of
        Err errors ->

Modified tests/Test/Parser/Util.elm from [240dab60e8] to [34556264c0].

1
2
3
4

5
6
7
8

9
10
11
12
13
14
15
...
123
124
125
126
127
128
129





130
131
132
133
134
135
136
137
...
164
165
166
167
168
169
170










module Test.Parser.Util exposing
    ( addFunctionsForStructs
    , compile
    , compileRetainLocations

    )

import Dict
import Dict.Extra as Dict

import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (emptyRange)
import Play.Data.Type as Type exposing (Type)
import Play.Parser as AST exposing (..)
import Play.Parser.Problem exposing (Problem)
import String.Extra as String

................................................................................
            if List.isEmpty generics then
                Type.Custom name

            else
                Type.CustomGeneric name (List.map Type.Generic generics)

        ctor =





            { name = ">" ++ name
            , metadata =
                Metadata.default
                    |> Metadata.withVerifiedType (List.map Tuple.second members) [ selfType ]
            , implementation = AST.SoloImpl [ AST.ConstructType name ]
            }

        setters =
................................................................................

        allFuncs =
            (ctor :: setters)
                ++ getters
                |> Dict.fromListBy .name
    in
    { ast | words = Dict.union ast.words allFuncs }














>




>







 







>
>
>
>
>
|







 







>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
module Test.Parser.Util exposing
    ( addFunctionsForStructs
    , compile
    , compileRetainLocations
    , expectCompiles
    )

import Dict
import Dict.Extra as Dict
import Expect
import Play.Data.Metadata as Metadata
import Play.Data.SourceLocation exposing (emptyRange)
import Play.Data.Type as Type exposing (Type)
import Play.Parser as AST exposing (..)
import Play.Parser.Problem exposing (Problem)
import String.Extra as String

................................................................................
            if List.isEmpty generics then
                Type.Custom name

            else
                Type.CustomGeneric name (List.map Type.Generic generics)

        ctor =
            { name =
                if List.isEmpty members then
                    name

                else
                    ">" ++ name
            , metadata =
                Metadata.default
                    |> Metadata.withVerifiedType (List.map Tuple.second members) [ selfType ]
            , implementation = AST.SoloImpl [ AST.ConstructType name ]
            }

        setters =
................................................................................

        allFuncs =
            (ctor :: setters)
                ++ getters
                |> Dict.fromListBy .name
    in
    { ast | words = Dict.union ast.words allFuncs }


expectCompiles : String -> Expect.Expectation
expectCompiles code =
    case compile code of
        Err _ ->
            Expect.fail "Did not expect compilation to fail."

        Ok _ ->
            Expect.pass

Modified tests/Test/Qualifier.elm from [fe9a3dfa3e] to [f2374a327b].

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
...
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
                                      , metadata = Metadata.default
                                      , implementation =
                                            AST.MultiImpl
                                                [ ( AST.TypeMatch emptyRange (Type.Custom "Box") [ ( "value", AST.LiteralInt 0 ) ]
                                                  , [ AST.Word emptyRange ">True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange ">False" ]
                                      }
                                    ]
                            }
                                |> ParserUtil.addFunctionsForStructs

                        expectedAst =
                            { types =
................................................................................
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
                                      , metadata = Metadata.default
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange (Type.Custom "Box") [ ( "value", LiteralInt 0 ) ], [ Word emptyRange ">True" ] )
                                                ]
                                                [ Word emptyRange ">False" ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
                    in
                    case run unqualifiedAst of
                        Err _ ->
................................................................................
                                        Metadata.default
                                            |> Metadata.withType [ Type.Custom "Box" ] [ Type.Custom "Bool" ]
                                  , implementation =
                                        AST.MultiImpl
                                            [ ( AST.TypeMatch emptyRange
                                                    (Type.Custom "Box")
                                                    [ ( "value", AST.LiteralType (Type.Custom "True") ) ]
                                              , [ AST.Word emptyRange ">True" ]
                                              )
                                            ]
                                            [ AST.Word emptyRange ">False" ]
                                  }
                                ]
                        }
                            |> ParserUtil.addFunctionsForStructs

                    expectedAst =
                        { types =
................................................................................
                                        Metadata.default
                                            |> Metadata.withType [ Type.Custom "Box" ] [ boolUnion ]
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch emptyRange
                                                    (Type.Custom "Box")
                                                    [ ( "value", LiteralType (Type.Custom "True") ) ]
                                              , [ Word emptyRange ">True" ]
                                              )
                                            ]
                                            [ Word emptyRange ">False" ]
                                  }
                                ]
                        }
                            |> QualifierUtil.addFunctionsForStructs
                in
                case run unqualifiedAst of
                    Err _ ->
                        Expect.fail "Did not expect qualification to fail"

                    Ok qualifiedAst ->
                        Expect.equal expectedAst qualifiedAst
        ]







|


|







 







|

|







 







|


|







 







|


|












481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
...
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
...
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
                                      , metadata = Metadata.default
                                      , implementation =
                                            AST.MultiImpl
                                                [ ( AST.TypeMatch emptyRange (Type.Custom "Box") [ ( "value", AST.LiteralInt 0 ) ]
                                                  , [ AST.Word emptyRange "True" ]
                                                  )
                                                ]
                                                [ AST.Word emptyRange "False" ]
                                      }
                                    ]
                            }
                                |> ParserUtil.addFunctionsForStructs

                        expectedAst =
                            { types =
................................................................................
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "zero?"
                                      , metadata = Metadata.default
                                      , implementation =
                                            MultiImpl
                                                [ ( TypeMatch emptyRange (Type.Custom "Box") [ ( "value", LiteralInt 0 ) ], [ Word emptyRange "True" ] )
                                                ]
                                                [ Word emptyRange "False" ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
                    in
                    case run unqualifiedAst of
                        Err _ ->
................................................................................
                                        Metadata.default
                                            |> Metadata.withType [ Type.Custom "Box" ] [ Type.Custom "Bool" ]
                                  , implementation =
                                        AST.MultiImpl
                                            [ ( AST.TypeMatch emptyRange
                                                    (Type.Custom "Box")
                                                    [ ( "value", AST.LiteralType (Type.Custom "True") ) ]
                                              , [ AST.Word emptyRange "True" ]
                                              )
                                            ]
                                            [ AST.Word emptyRange "False" ]
                                  }
                                ]
                        }
                            |> ParserUtil.addFunctionsForStructs

                    expectedAst =
                        { types =
................................................................................
                                        Metadata.default
                                            |> Metadata.withType [ Type.Custom "Box" ] [ boolUnion ]
                                  , implementation =
                                        MultiImpl
                                            [ ( TypeMatch emptyRange
                                                    (Type.Custom "Box")
                                                    [ ( "value", LiteralType (Type.Custom "True") ) ]
                                              , [ Word emptyRange "True" ]
                                              )
                                            ]
                                            [ Word emptyRange "False" ]
                                  }
                                ]
                        }
                            |> QualifierUtil.addFunctionsForStructs
                in
                case run unqualifiedAst of
                    Err _ ->
                        Expect.fail "Did not expect qualification to fail"

                    Ok qualifiedAst ->
                        Expect.equal expectedAst qualifiedAst
        ]

Modified tests/Test/Qualifier/Util.elm from [e107564c2e] to [2822ce177a].

28
29
30
31
32
33
34





35
36
37
38
39
40
41
42
            if List.isEmpty generics then
                Type.Custom name

            else
                Type.CustomGeneric name (List.map Type.Generic generics)

        ctor =





            { name = ">" ++ name
            , metadata =
                Metadata.default
                    |> Metadata.withVerifiedType (List.map Tuple.second members) [ selfType ]
            , implementation = AST.SoloImpl [ AST.ConstructType name ]
            }

        setters =







>
>
>
>
>
|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
            if List.isEmpty generics then
                Type.Custom name

            else
                Type.CustomGeneric name (List.map Type.Generic generics)

        ctor =
            { name =
                if List.isEmpty members then
                    name

                else
                    ">" ++ name
            , metadata =
                Metadata.default
                    |> Metadata.withVerifiedType (List.map Tuple.second members) [ selfType ]
            , implementation = AST.SoloImpl [ AST.ConstructType name ]
            }

        setters =

Modified tests/Test/TypeChecker.elm from [ccb93b6437] to [7ab0558be9].

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
...
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
...
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
....
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
....
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
                                  }
                                , { name = "main"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.asEntryPoint
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.Word emptyRange ">True"
                                            , QAST.Word emptyRange "as-int"
                                            ]
                                  }
                                ]
                        }
                            |> QualifierUtil.addFunctionsForStructs
                in
................................................................................
                            [ multiFn
                            , { name = "main"
                              , metadata =
                                    Metadata.default
                                        |> Metadata.asEntryPoint
                              , implementation =
                                    QAST.SoloImpl
                                        [ QAST.Word emptyRange ">True"
                                        , QAST.Word emptyRange "to-int"
                                        , QAST.Word emptyRange ">False"
                                        , QAST.Word emptyRange "to-int"
                                        , QAST.Builtin emptyRange Builtin.Equal
                                        ]
                              }
                            ]
                    }
                        |> QualifierUtil.addFunctionsForStructs
................................................................................
                                Dict.fromListBy .name
                                    [ { name = "not"
                                      , metadata = Metadata.default
                                      , implementation =
                                            QAST.MultiImpl
                                                [ ( QAST.TypeMatch emptyRange (Type.Custom "True") []
                                                  , [ QAST.Builtin emptyRange Builtin.StackDrop
                                                    , QAST.Word emptyRange ">False"
                                                    ]
                                                  )
                                                , ( QAST.TypeMatch emptyRange (Type.Custom "False") []
                                                  , [ QAST.Builtin emptyRange Builtin.StackDrop
                                                    , QAST.Word emptyRange ">True"
                                                    ]
                                                  )
                                                ]
                                                []
                                      }
                                    , { name = "true-to-int"
                                      , metadata =
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange ">True"
                                                , QAST.Word emptyRange "not"
                                                , QAST.Word emptyRange "true-to-int"
                                                ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
................................................................................
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Word emptyRange ">EmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Integer emptyRange 0
                                                , QAST.Word emptyRange "first-or-default"
                                                , QAST.Integer emptyRange 1
                                                , QAST.Builtin emptyRange Builtin.Equal
                                                ]
                                      }
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange ">Nil"
                                                , QAST.Integer emptyRange 1
                                                , QAST.Word emptyRange "with-default"
                                                ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
................................................................................
                                        [ Type.Generic "a"
                                        , Type.Custom "Nil"
                                        ]
                                    , CustomTypeDef "Nil" emptyRange [] []
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = ">Nil"
                                      , type_ = { input = [], output = [ Type.Custom "Nil" ] }
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.withVerifiedType [] [ Type.Custom "Nil" ]
                                      , implementation =
                                            SoloImpl
                                                [ ConstructType "Nil"
................................................................................
                                      , type_ = { input = [], output = [ Type.Int ] }
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            SoloImpl
                                                [ Word emptyRange
                                                    ">Nil"
                                                    { input = []
                                                    , output = [ Type.Custom "Nil" ]
                                                    }
                                                , IntLiteral emptyRange 1
                                                , Word emptyRange
                                                    "with-default"
                                                    { input = [ Type.Union [ Type.Int, Type.Custom "Nil" ], Type.Int ]
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange ">Nil"
                                                , QAST.WordRef emptyRange "main__quot1"
                                                , QAST.Word emptyRange "map"
                                                ]
                                      }
                                    , { name = "main__quot1"
                                      , metadata =
                                            Metadata.default
................................................................................
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Integer emptyRange 2
                                                , QAST.Integer emptyRange 3
                                                , QAST.Word emptyRange ">EmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange "sum"
                                                ]
                                      }
                                    ]







|







 







|

|







 







|




|







 







|







 







|







 







|







 







|







 







|







 







|







 







|







151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
...
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
...
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
...
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
....
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
....
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
                                  }
                                , { name = "main"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.asEntryPoint
                                  , implementation =
                                        QAST.SoloImpl
                                            [ QAST.Word emptyRange "True"
                                            , QAST.Word emptyRange "as-int"
                                            ]
                                  }
                                ]
                        }
                            |> QualifierUtil.addFunctionsForStructs
                in
................................................................................
                            [ multiFn
                            , { name = "main"
                              , metadata =
                                    Metadata.default
                                        |> Metadata.asEntryPoint
                              , implementation =
                                    QAST.SoloImpl
                                        [ QAST.Word emptyRange "True"
                                        , QAST.Word emptyRange "to-int"
                                        , QAST.Word emptyRange "False"
                                        , QAST.Word emptyRange "to-int"
                                        , QAST.Builtin emptyRange Builtin.Equal
                                        ]
                              }
                            ]
                    }
                        |> QualifierUtil.addFunctionsForStructs
................................................................................
                                Dict.fromListBy .name
                                    [ { name = "not"
                                      , metadata = Metadata.default
                                      , implementation =
                                            QAST.MultiImpl
                                                [ ( QAST.TypeMatch emptyRange (Type.Custom "True") []
                                                  , [ QAST.Builtin emptyRange Builtin.StackDrop
                                                    , QAST.Word emptyRange "False"
                                                    ]
                                                  )
                                                , ( QAST.TypeMatch emptyRange (Type.Custom "False") []
                                                  , [ QAST.Builtin emptyRange Builtin.StackDrop
                                                    , QAST.Word emptyRange "True"
                                                    ]
                                                  )
                                                ]
                                                []
                                      }
                                    , { name = "true-to-int"
                                      , metadata =
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange "True"
                                                , QAST.Word emptyRange "not"
                                                , QAST.Word emptyRange "true-to-int"
                                                ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
................................................................................
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Word emptyRange "EmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Integer emptyRange 0
                                                , QAST.Word emptyRange "first-or-default"
                                                , QAST.Integer emptyRange 1
                                                , QAST.Builtin emptyRange Builtin.Equal
                                                ]
                                      }
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange "Nil"
                                                , QAST.Integer emptyRange 1
                                                , QAST.Word emptyRange "with-default"
                                                ]
                                      }
                                    ]
                            }
                                |> QualifierUtil.addFunctionsForStructs
................................................................................
                                        [ Type.Generic "a"
                                        , Type.Custom "Nil"
                                        ]
                                    , CustomTypeDef "Nil" emptyRange [] []
                                    ]
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "Nil"
                                      , type_ = { input = [], output = [ Type.Custom "Nil" ] }
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.withVerifiedType [] [ Type.Custom "Nil" ]
                                      , implementation =
                                            SoloImpl
                                                [ ConstructType "Nil"
................................................................................
                                      , type_ = { input = [], output = [ Type.Int ] }
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            SoloImpl
                                                [ Word emptyRange
                                                    "Nil"
                                                    { input = []
                                                    , output = [ Type.Custom "Nil" ]
                                                    }
                                                , IntLiteral emptyRange 1
                                                , Word emptyRange
                                                    "with-default"
                                                    { input = [ Type.Union [ Type.Int, Type.Custom "Nil" ], Type.Int ]
................................................................................
                                      }
                                    , { name = "main"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Word emptyRange "Nil"
                                                , QAST.WordRef emptyRange "main__quot1"
                                                , QAST.Word emptyRange "map"
                                                ]
                                      }
                                    , { name = "main__quot1"
                                      , metadata =
                                            Metadata.default
................................................................................
                                            Metadata.default
                                                |> Metadata.asEntryPoint
                                      , implementation =
                                            QAST.SoloImpl
                                                [ QAST.Integer emptyRange 1
                                                , QAST.Integer emptyRange 2
                                                , QAST.Integer emptyRange 3
                                                , QAST.Word emptyRange "EmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange ">NonEmptyList"
                                                , QAST.Word emptyRange "sum"
                                                ]
                                      }
                                    ]

Modified wasm_tests/multiword.test.js from [2d2d1e8504] to [aadc36f12d].

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
..
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
...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
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

test('Simple case', async () => {
    const wat = await compiler.toWat(`
        defunion: Bool
        : True
        : False

        deftype: True
        deftype: False

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

        def: main
        entry: true

        : >True to-int >False to-int -

    `);

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

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

................................................................................
// Same as above, but without an explicit False branch.
test('Default branch', async () => {
    const wat = await compiler.toWat(`
        defunion: Bool
        : True 
        : False

        deftype: True
        deftype: False

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

        def: main
        entry: true
        : >True to-int 
          >False to-int 
          -
    `);

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

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

test('Multiple arguments', async () => {
    const wat = await compiler.toWat(`
        defunion: Beings
        : Person 
        : Dog

        deftype: Person
        : age Int

        deftype: 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 
................................................................................

test('Generic arguments', async () => {
    const wat = await compiler.toWat(`
        defunion: List a
        : NonEmptyList a 
        : EmptyList

        deftype: NonEmptyList a
        : first a
        : rest List a

        deftype: EmptyList

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

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

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

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

test('Recursive word', async () => {
    const wat = await compiler.toWat(`
        defunion: List a
        : NonEmptyList a 
        : EmptyList

        deftype: NonEmptyList a
        : first a
        : rest List a

        deftype: 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
    `);

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

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

test('Int case', async () => {
    const wat = await compiler.toWat(`
        defunion: Bool
        : Int
        : NoInt

        deftype: 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
    `);

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

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

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

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

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

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

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

test('Generic case', async () => {
    const wat = await compiler.toWat(`
        defunion: Maybe a
        : a
        : Nil

        deftype: 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);
});







|
|


|

|




>
|
>







 







|
|


|

|



|
|







 







|


|



|



|






|

|







 







|



|


|

|




|







 







|



|






|

|




|










|



|


|

|







 







|

|







 







|

|







 







|




|







 







|


|



|



|







 







|



|

|




|

|













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
..
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
...
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
...
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

test('Simple case', async () => {
    const wat = await compiler.toWat(`
        defunion: Bool
        : 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 
          -
    `);

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

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

................................................................................
// Same as above, but without an explicit False branch.
test('Default branch', async () => {
    const wat = await compiler.toWat(`
        defunion: Bool
        : 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 
          -
    `);

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

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

test('Multiple arguments', async () => {
    const wat = await compiler.toWat(`
        defunion: Beings
        : Person 
        : Dog

        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 
................................................................................

test('Generic arguments', async () => {
    const wat = await compiler.toWat(`
        defunion: List a
        : NonEmptyList a 
        : EmptyList

        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
    `);

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

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

test('Recursive word', async () => {
    const wat = await compiler.toWat(`
        defunion: List a
        : NonEmptyList a 
        : EmptyList

        defstruct: NonEmptyList a
        : first a
        : rest List a

        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
    `);

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

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

test('Int case', async () => {
    const wat = await compiler.toWat(`
        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
    `);

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

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

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 
    `);

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

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

test('Generic case', async () => {
    const wat = await compiler.toWat(`
        defunion: Maybe a
        : 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 [20278d37c4] to [72e613569d].

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
const compiler = require('./compiler.wrapper');

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

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

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

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

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

test('Basic pattern match with default implementation', async () => {
    const wat = await compiler.toWat(`
        deftype: 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');

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

test('Basic pattern match reverse case', async () => {
    const wat = await compiler.toWat(`
        deftype: 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');

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

test('Multiple arguments', async () => {
    const wat = await compiler.toWat(`
        deftype: 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');

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

test('Multiple arguments reverse case', async () => {
    const wat = await compiler.toWat(`
        deftype: 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');

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

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

        deftype: 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');

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

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

        deftype: 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);
});




|



|

|







 







|



|

|













|



|

|













|




|

|













|




|

|













|


|



|

|













|


|



|

|










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
const compiler = require('./compiler.wrapper');

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
    `);

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

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

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');

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

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');

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

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');

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

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');

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

test('Recursive match', async () => {
    const wat = await compiler.toWat(`
        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');

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

test('Recursive match reverse case', async () => {
    const wat = await compiler.toWat(`
        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);
});

Modified wasm_tests/quotes.test.js from [1ebafeab8a] to [8233315f50].

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    const result = await compiler.run(wat, 'main');

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

test('Basic quotation', async () => {
    const wat = await compiler.toWat(`
        deftype: Coordinate
        : x Int
        : y Int

        def: update-x
        type: Coordinate [ Int -- Int ] -- Coordinate
        : swap
          dup x>







|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    const result = await compiler.run(wat, 'main');

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

test('Basic quotation', async () => {
    const wat = await compiler.toWat(`
        defstruct: Coordinate
        : x Int
        : y Int

        def: update-x
        type: Coordinate [ Int -- Int ] -- Coordinate
        : swap
          dup x>

Modified wasm_tests/record.test.js from [d4ca0851ff] to [203aebee22].

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
const compiler = require('./compiler.wrapper');

test('Enum type', async () => {
    const wat = await compiler.toWat(`
       deftype: True 
       deftype: False

       def: main
       entry: true
       : >True
    `);

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

    // types are sorted alphabetically, so False will get id 0, and True gets id 1.
    expect(result.typeIdForPointer()).toBe(1);
});

test('Compound type', async () => {
    const wat = await compiler.toWat(`
        deftype: Person
        : age Int

        def: inc-age
        : age> 1 + >Person

        def: main
        entry: true




|
|



|










|







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
const compiler = require('./compiler.wrapper');

test('Enum type', async () => {
    const wat = await compiler.toWat(`
       defstruct: True 
       defstruct: False

       def: main
       entry: true
       : True
    `);

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

    // types are sorted alphabetically, so False will get id 0, and True gets id 1.
    expect(result.typeIdForPointer()).toBe(1);
});

test('Compound type', async () => {
    const wat = await compiler.toWat(`
        defstruct: Person
        : age Int

        def: inc-age
        : age> 1 + >Person

        def: main
        entry: true