Play

Check-in [563df19e0c]
Login
Overview
Comment:Parser now understands union type definitions.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 563df19e0c81a89daa5d5e24d34bce7dddbdae9ec00e6291624a554bbe91fb05
User & Date: robin.hansen on 2020-04-14 17:09:39
Other Links: manifest | tags
Context
2020-04-15
15:07
Revert trunk to the point of the last feature merge. check-in: bb4bf1194a user: robin.hansen tags: trunk
14:48
Parser now understands multifunctions. check-in: 5064f61334 user: robin.hansen tags: trunk
2020-04-14
17:09
Parser now understands union type definitions. check-in: 563df19e0c user: robin.hansen tags: trunk
2020-04-10
12:21
Add support for stack manipulation functions and generic function types. check-in: 32f58d1269 user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Parser.elm from [0f29d96909] to [319a52a171].

12
13
14
15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
..
82
83
84
85
86
87
88
89




90
91
92
93
94
95
96
...
156
157
158
159
160
161
162

























163
164
165
166
167
168
169
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
...
318
319
320
321
322
323
324




















type alias AST =
    { types : Dict String TypeDefinition
    , words : Dict String WordDefinition
    }


type alias TypeDefinition =
    { name : String
    , members : List ( String, Type )
    }



type alias WordDefinition =
    { name : String
    , metadata : Metadata
    , implementation : List AstNode
    }
................................................................................
                    List.drop (List.length tilNextDefinition) tokens
            in
            gatherHelp pred remainingTokens (tilNextDefinition :: acc)


definitionKeywords : Set String
definitionKeywords =
    Set.fromList [ "def", "deftype" ]






isDefinition : Token -> Bool
isDefinition token =
    case token of
        Token.Metadata value ->
            Set.member value definitionKeywords
................................................................................
                            )

                        Ok members ->
                            ( errors
                            , parseTypeDefinition typeName members ast
                            )


























                _ ->
                    ( () :: errors
                    , ast
                    )

        _ ->
            ( () :: errors
................................................................................
            Err ()


parseTypeDefinition : String -> List ( String, Type ) -> AST -> AST
parseTypeDefinition typeName members ast =
    let
        typeDef =
            { name = typeName
            , members = members
            }

        metadata =
            Metadata.default
                |> Metadata.withType (List.map Tuple.second members) [ Type.Custom typeName ]

        ctorDef =
            { name = ">" ++ typeName
................................................................................
                    Err ()

                Ok typeValue ->
                    parseTypeMembers rest (( name, typeValue ) :: acc)

        _ ->
            Err ()


























|
<
|
<
>







 







|
>
>
>
>







 







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







 







|
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
12
13
14
15
16
17
18
19

20

21
22
23
24
25
26
27
28
..
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
273
274
275
276
277
278
279
280


281
282
283
284
285
286
287
...
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369

type alias AST =
    { types : Dict String TypeDefinition
    , words : Dict String WordDefinition
    }


type TypeDefinition

    = CustomTypeDef String (List ( String, Type ))

    | UnionTypeDef String (List Type)


type alias WordDefinition =
    { name : String
    , metadata : Metadata
    , implementation : List AstNode
    }
................................................................................
                    List.drop (List.length tilNextDefinition) tokens
            in
            gatherHelp pred remainingTokens (tilNextDefinition :: acc)


definitionKeywords : Set String
definitionKeywords =
    Set.fromList
        [ "def"
        , "deftype"
        , "defunion"
        ]


isDefinition : Token -> Bool
isDefinition token =
    case token of
        Token.Metadata value ->
            Set.member value definitionKeywords
................................................................................
                            )

                        Ok members ->
                            ( errors
                            , parseTypeDefinition typeName members ast
                            )

                _ ->
                    ( () :: errors
                    , ast
                    )

        (Token.Metadata "defunion") :: (Token.Type typeName) :: (Token.Metadata "") :: Token.ListStart :: rest ->
            case List.splitWhen (\t -> t == Token.ListEnd) rest of
                Just ( types, [ Token.ListEnd ] ) ->
                    let
                        possibleMemberTypes =
                            types
                                |> List.map parseType
                                |> Result.combine
                    in
                    case possibleMemberTypes of
                        Err () ->
                            ( () :: errors
                            , ast
                            )

                        Ok members ->
                            ( errors
                            , parseUnionTypeDefinition typeName members ast
                            )

                _ ->
                    ( () :: errors
                    , ast
                    )

        _ ->
            ( () :: errors
................................................................................
            Err ()


parseTypeDefinition : String -> List ( String, Type ) -> AST -> AST
parseTypeDefinition typeName members ast =
    let
        typeDef =
            CustomTypeDef typeName members



        metadata =
            Metadata.default
                |> Metadata.withType (List.map Tuple.second members) [ Type.Custom typeName ]

        ctorDef =
            { name = ">" ++ typeName
................................................................................
                    Err ()

                Ok typeValue ->
                    parseTypeMembers rest (( name, typeValue ) :: acc)

        _ ->
            Err ()


typeDefinitionName : TypeDefinition -> String
typeDefinitionName typeDef =
    case typeDef of
        CustomTypeDef name _ ->
            name

        UnionTypeDef name _ ->
            name


parseUnionTypeDefinition : String -> List Type -> AST -> AST
parseUnionTypeDefinition typeName members ast =
    let
        typeDef =
            UnionTypeDef typeName members
    in
    { ast | types = Dict.insert typeName typeDef ast.types }

Modified src/Play/Qualifier.elm from [a4d7e116e4] to [b95e559676].

78
79
80
81
82
83
84
85
86
87





88
89
90
91
92
93
94


qualifyType :
    Parser.AST
    -> Parser.TypeDefinition
    -> ( List (), Dict String TypeDefinition )
    -> ( List (), Dict String TypeDefinition )
qualifyType ast unqualifiedWord ( errors, acc ) =
    ( errors
    , Dict.insert unqualifiedWord.name unqualifiedWord acc





    )


qualifyDefinition :
    Parser.AST
    -> Parser.WordDefinition
    -> ( List (), Dict String WordDefinition )







|

|
>
>
>
>
>







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99


qualifyType :
    Parser.AST
    -> Parser.TypeDefinition
    -> ( List (), Dict String TypeDefinition )
    -> ( List (), Dict String TypeDefinition )
qualifyType ast typeDef ( errors, acc ) =
    ( errors
    , case typeDef of
        Parser.CustomTypeDef name members ->
            Dict.insert name { name = name, members = members } acc

        _ ->
            acc
    )


qualifyDefinition :
    Parser.AST
    -> Parser.WordDefinition
    -> ( List (), Dict String WordDefinition )

Modified tests/Test/Parser.elm from [b911c1ec4c] to [a378bb1704].

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
...
256
257
258
259
260
261
262


























































263
264
265
266
267
268
269
270
                        , Type "Int"
                        , Metadata ""
                        , Token.Integer 1
                        ]

                    expectedAst =
                        { types =
                            Dict.fromListBy .name
                                [ { name = "True"
                                  , members = []
                                  }
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">True"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [] [ Type.Custom "True" ]
................................................................................
                        , Type "Int"
                        , Metadata ""
                        , Token.Symbol "age>"
                        ]

                    expectedAst =
                        { types =
                            Dict.fromListBy .name
                                [ { name = "Person"
                                  , members =
                                        [ ( "age", Type.Int )
                                        , ( "jobs", Type.Int )
                                        ]
                                  }
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">Person"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int, Type.Int ] [ Type.Custom "Person" ]
................................................................................
                                  , implementation =
                                        [ AST.Word "dup"
                                        , AST.Word "rotate"
                                        ]
                                  }
                                ]
                        }


























































                in
                case parse source of
                    Err () ->
                        Expect.fail "Did not expect parsing to fail"

                    Ok ast ->
                        Expect.equal expectedAst ast
        ]







|
|
<
<







 







|
|
<
|
|
|
<







 







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








107
108
109
110
111
112
113
114
115


116
117
118
119
120
121
122
...
162
163
164
165
166
167
168
169
170

171
172
173

174
175
176
177
178
179
180
...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
                        , Type "Int"
                        , Metadata ""
                        , Token.Integer 1
                        ]

                    expectedAst =
                        { types =
                            Dict.fromListBy AST.typeDefinitionName
                                [ CustomTypeDef "True" []


                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">True"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [] [ Type.Custom "True" ]
................................................................................
                        , Type "Int"
                        , Metadata ""
                        , Token.Symbol "age>"
                        ]

                    expectedAst =
                        { types =
                            Dict.fromListBy AST.typeDefinitionName
                                [ CustomTypeDef "Person"

                                    [ ( "age", Type.Int )
                                    , ( "jobs", Type.Int )
                                    ]

                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">Person"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [ Type.Int, Type.Int ] [ Type.Custom "Person" ]
................................................................................
                                  , implementation =
                                        [ AST.Word "dup"
                                        , AST.Word "rotate"
                                        ]
                                  }
                                ]
                        }
                in
                case parse source of
                    Err () ->
                        Expect.fail "Did not expect parsing to fail"

                    Ok ast ->
                        Expect.equal expectedAst ast
        , test "Parser understands union types" <|
            \_ ->
                let
                    source =
                        [ Metadata "defunion"
                        , Type "Bool"
                        , Metadata ""
                        , ListStart
                        , Type "True"
                        , Type "False"
                        , ListEnd

                        -- True
                        , Metadata "deftype"
                        , Type "True"

                        -- False
                        , Metadata "deftype"
                        , Type "False"
                        ]

                    expectedAst =
                        { types =
                            Dict.fromListBy AST.typeDefinitionName
                                [ UnionTypeDef "Bool"
                                    [ Type.Custom "True"
                                    , Type.Custom "False"
                                    ]
                                , CustomTypeDef "True" []
                                , CustomTypeDef "False" []
                                ]
                        , words =
                            Dict.fromListBy .name
                                [ { name = ">True"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [] [ Type.Custom "True" ]
                                  , implementation =
                                        [ AST.ConstructType "True"
                                        ]
                                  }
                                , { name = ">False"
                                  , metadata =
                                        Metadata.default
                                            |> Metadata.withType [] [ Type.Custom "False" ]
                                  , implementation =
                                        [ AST.ConstructType "False"
                                        ]
                                  }
                                ]
                        }
                in
                case parse source of
                    Err () ->
                        Expect.fail "Did not expect parsing to fail"

                    Ok ast ->
                        Expect.equal expectedAst ast
        ]