Stabel

Check-in [c4e54eba3a]
Login
Overview
Comment:Qualifier now understands aliases on module and function level.
Timelines: family | ancestors | descendants | both | module-definition
Files: files | file ages | folders
SHA3-256: c4e54eba3a859347638656501fb178b75bd50ef6235fb1df224eb9aab80c0dea
User & Date: robin.hansen on 2021-05-06 13:42:45
Other Links: branch diff | manifest | tags
Context
2021-05-06
15:37
Qualifier now understands imports. check-in: 7514c3ede5 user: robin.hansen tags: module-definition
13:42
Qualifier now understands aliases on module and function level. check-in: c4e54eba3a user: robin.hansen tags: module-definition
08:20
Qualifier now fails when it finds an internal or external word that does not exist in the wip ast. check-in: 96fdd81e67 user: robin.hansen tags: module-definition
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Play/Qualifier.elm from [f9bb79bf19] to [5feef22612].

50
51
52
53
54
55
56






57
58
59
60
61
62
63
...
244
245
246
247
248
249
250







251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
...
363
364
365
366
367
368
369

370
371
372
373
374
375
376
377
378
379
380
381
382
383
...
480
481
482
483
484
485
486
487
488


489
490
491
492


493

494
495
496
497
498
499
500
...
505
506
507
508
509
510
511





512

513
514
515
516
517
518
519
520
...
529
530
531
532
533
534
535
536
537
538
















539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
...
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
    | Word SourceLocationRange String
    | WordRef SourceLocationRange String
    | ConstructType String
    | GetMember String String
    | SetMember String String
    | Builtin SourceLocationRange Builtin








builtinDict : Dict String Builtin
builtinDict =
    Dict.fromList
        [ ( "+", Builtin.Plus )
        , ( "-", Builtin.Minus )
        , ( "*", Builtin.Multiply )
................................................................................
        ( whens, impl ) =
            case unqualifiedWord.implementation of
                Parser.SoloImpl defImpl ->
                    ( [], defImpl )

                Parser.MultiImpl whenImpl defImpl ->
                    ( whenImpl, defImpl )








        ( newWordsAfterWhens, qualifiedWhensResult ) =
            whens
                |> List.foldr (qualifyWhen config qualifiedTypes unqualifiedWord.name) ( acc, [] )
                |> Tuple.mapSecond Result.combine

        ( newWordsAfterImpl, qualifiedImplementationResult ) =
            initQualifyNode unqualifiedWord.name config newWordsAfterWhens impl

        qualifiedMetadataResult =
            qualifyMetadata config qualifiedTypes unqualifiedWord.metadata

        qualifiedName =
            qualifyName config unqualifiedWord.name
    in
................................................................................
    }


qualifyWhen :
    RunConfig
    -> Dict String TypeDefinition
    -> String

    -> ( Parser.TypeMatch, List Parser.AstNode )
    -> ( Dict String WordDefinition, List (Result Problem ( TypeMatch, List Node )) )
    -> ( Dict String WordDefinition, List (Result Problem ( TypeMatch, List Node )) )
qualifyWhen config qualifiedTypes wordName ( typeMatch, impl ) ( qualifiedWords, result ) =
    let
        ( newWords, qualifiedImplementationResult ) =
            initQualifyNode wordName config qualifiedWords impl

        qualifiedMatchResult =
            qualifyMatch config qualifiedTypes typeMatch
    in
    case ( qualifiedImplementationResult, qualifiedMatchResult ) of
        ( Err err, _ ) ->
            ( newWords
................................................................................
                        Ok <| ( fieldName, RecursiveMatch match )

    else
        Err <| NoSuchMemberOnType range typeName fieldName


initQualifyNode :
    String
    -> RunConfig


    -> Dict String WordDefinition
    -> List Parser.AstNode
    -> ( Dict String WordDefinition, Result Problem (List Node) )
initQualifyNode currentDefName config qualifiedWords impl =


    List.foldr (qualifyNode config currentDefName) (initQualifyNodeAccumulator qualifiedWords) impl

        |> (\acc -> ( acc.qualifiedWords, Result.combine acc.qualifiedNodes ))


type alias QualifyNodeAccumulator =
    { availableQuoteId : Int
    , qualifiedWords : Dict String WordDefinition
    , qualifiedNodes : List (Result Problem Node)
................................................................................
initQualifyNodeAccumulator qualifiedWords =
    { availableQuoteId = 1
    , qualifiedWords = qualifiedWords
    , qualifiedNodes = []
    }







qualifyNode : RunConfig -> String -> Parser.AstNode -> QualifyNodeAccumulator -> QualifyNodeAccumulator

qualifyNode config currentDefName node acc =
    case node of
        Parser.Integer loc value ->
            { acc | qualifiedNodes = Ok (Integer loc value) :: acc.qualifiedNodes }

        Parser.Word loc value ->
            let
                qualifiedName =
................................................................................
                        { acc | qualifiedNodes = Ok (Builtin loc builtin) :: acc.qualifiedNodes }

                    Nothing ->
                        { acc | qualifiedNodes = Err (UnknownWordRef loc value) :: acc.qualifiedNodes }

        Parser.PackageWord loc path value ->
            let
                normalizedPath =
                    String.join "/" path

















                qualifiedPath =
                    qualifyPackageModule config.packageName normalizedPath

                qualifiedName =
                    String.join "/" [ qualifiedPath, value ]
            in
            case Dict.get qualifiedName config.inProgressAST.words of
                Nothing ->
                    { acc | qualifiedNodes = Err (UnknownWordRef loc qualifiedName) :: acc.qualifiedNodes }

                Just _ ->
                    { acc | qualifiedNodes = Ok (Word loc qualifiedName) :: acc.qualifiedNodes }

        Parser.ExternalWord loc path value ->
            let
                normalizedPath =
                    "/" ++ String.join "/" path
            in
            case Dict.get normalizedPath config.externalModules of
................................................................................
                    if String.startsWith "quote:" currentDefName then
                        currentDefName ++ "/" ++ String.fromInt acc.availableQuoteId

                    else
                        "quote:" ++ qualifyName config currentDefName ++ "/" ++ String.fromInt acc.availableQuoteId

                ( newWordsAfterQuot, qualifiedQuotImplResult ) =
                    initQualifyNode quoteName config acc.qualifiedWords quotImpl
            in
            case qualifiedQuotImplResult of
                Ok qualifiedQuotImpl ->
                    { acc
                        | availableQuoteId = acc.availableQuoteId + 1
                        , qualifiedWords =
                            Dict.insert quoteName







>
>
>
>
>
>







 







>
>
>
>
>
>
>



|



|







 







>



|


|







 







<
|
>
>



|
>
>
|
>







 







>
>
>
>
>
|
>
|







 







|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|

|
|
|
|
|
|

|
|







 







|







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
...
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
...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
...
494
495
496
497
498
499
500

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
...
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
...
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
    | Word SourceLocationRange String
    | WordRef SourceLocationRange String
    | ConstructType String
    | GetMember String String
    | SetMember String String
    | Builtin SourceLocationRange Builtin


type alias ModuleReferences =
    { aliases : Dict String String
    , imports : Dict String (List String)
    }


builtinDict : Dict String Builtin
builtinDict =
    Dict.fromList
        [ ( "+", Builtin.Plus )
        , ( "-", Builtin.Minus )
        , ( "*", Builtin.Multiply )
................................................................................
        ( whens, impl ) =
            case unqualifiedWord.implementation of
                Parser.SoloImpl defImpl ->
                    ( [], defImpl )

                Parser.MultiImpl whenImpl defImpl ->
                    ( whenImpl, defImpl )

        moduleReferences =
            { aliases =
                config.ast.moduleDefinition.aliases
                    |> Dict.union unqualifiedWord.metadata.aliases
            , imports = config.ast.moduleDefinition.imports
            }

        ( newWordsAfterWhens, qualifiedWhensResult ) =
            whens
                |> List.foldr (qualifyWhen config qualifiedTypes unqualifiedWord.name moduleReferences) ( acc, [] )
                |> Tuple.mapSecond Result.combine

        ( newWordsAfterImpl, qualifiedImplementationResult ) =
            initQualifyNode config unqualifiedWord.name moduleReferences newWordsAfterWhens impl

        qualifiedMetadataResult =
            qualifyMetadata config qualifiedTypes unqualifiedWord.metadata

        qualifiedName =
            qualifyName config unqualifiedWord.name
    in
................................................................................
    }


qualifyWhen :
    RunConfig
    -> Dict String TypeDefinition
    -> String
    -> ModuleReferences
    -> ( Parser.TypeMatch, List Parser.AstNode )
    -> ( Dict String WordDefinition, List (Result Problem ( TypeMatch, List Node )) )
    -> ( Dict String WordDefinition, List (Result Problem ( TypeMatch, List Node )) )
qualifyWhen config qualifiedTypes wordName modRefs ( typeMatch, impl ) ( qualifiedWords, result ) =
    let
        ( newWords, qualifiedImplementationResult ) =
            initQualifyNode config wordName modRefs qualifiedWords impl

        qualifiedMatchResult =
            qualifyMatch config qualifiedTypes typeMatch
    in
    case ( qualifiedImplementationResult, qualifiedMatchResult ) of
        ( Err err, _ ) ->
            ( newWords
................................................................................
                        Ok <| ( fieldName, RecursiveMatch match )

    else
        Err <| NoSuchMemberOnType range typeName fieldName


initQualifyNode :

    RunConfig
    -> String
    -> ModuleReferences
    -> Dict String WordDefinition
    -> List Parser.AstNode
    -> ( Dict String WordDefinition, Result Problem (List Node) )
initQualifyNode config currentDefName modRefs qualifiedWords impl =
    List.foldr
        (qualifyNode config currentDefName modRefs)
        (initQualifyNodeAccumulator qualifiedWords)
        impl
        |> (\acc -> ( acc.qualifiedWords, Result.combine acc.qualifiedNodes ))


type alias QualifyNodeAccumulator =
    { availableQuoteId : Int
    , qualifiedWords : Dict String WordDefinition
    , qualifiedNodes : List (Result Problem Node)
................................................................................
initQualifyNodeAccumulator qualifiedWords =
    { availableQuoteId = 1
    , qualifiedWords = qualifiedWords
    , qualifiedNodes = []
    }


qualifyNode :
    RunConfig
    -> String
    -> ModuleReferences
    -> Parser.AstNode
    -> QualifyNodeAccumulator
    -> QualifyNodeAccumulator
qualifyNode config currentDefName modRefs node acc =
    case node of
        Parser.Integer loc value ->
            { acc | qualifiedNodes = Ok (Integer loc value) :: acc.qualifiedNodes }

        Parser.Word loc value ->
            let
                qualifiedName =
................................................................................
                        { acc | qualifiedNodes = Ok (Builtin loc builtin) :: acc.qualifiedNodes }

                    Nothing ->
                        { acc | qualifiedNodes = Err (UnknownWordRef loc value) :: acc.qualifiedNodes }

        Parser.PackageWord loc path value ->
            let
                normalizedPathPreAliasCheck =
                    String.join "/" path

                normalizedPath =
                    Dict.get normalizedPathPreAliasCheck modRefs.aliases
                        |> Maybe.withDefault normalizedPathPreAliasCheck
            in
            if String.startsWith "/" normalizedPath then
                let
                    externalWordNode =
                        Parser.ExternalWord
                            loc
                            (List.drop 1 <| String.split "/" normalizedPath)
                            value
                in
                qualifyNode config currentDefName modRefs externalWordNode acc

            else
                let
                    qualifiedPath =
                        qualifyPackageModule config.packageName normalizedPath

                    qualifiedName =
                        String.join "/" [ qualifiedPath, value ]
                in
                case Dict.get qualifiedName config.inProgressAST.words of
                    Nothing ->
                        { acc | qualifiedNodes = Err (UnknownWordRef loc qualifiedName) :: acc.qualifiedNodes }

                    Just _ ->
                        { acc | qualifiedNodes = Ok (Word loc qualifiedName) :: acc.qualifiedNodes }

        Parser.ExternalWord loc path value ->
            let
                normalizedPath =
                    "/" ++ String.join "/" path
            in
            case Dict.get normalizedPath config.externalModules of
................................................................................
                    if String.startsWith "quote:" currentDefName then
                        currentDefName ++ "/" ++ String.fromInt acc.availableQuoteId

                    else
                        "quote:" ++ qualifyName config currentDefName ++ "/" ++ String.fromInt acc.availableQuoteId

                ( newWordsAfterQuot, qualifiedQuotImplResult ) =
                    initQualifyNode config quoteName modRefs acc.qualifiedWords quotImpl
            in
            case qualifiedQuotImplResult of
                Ok qualifiedQuotImpl ->
                    { acc
                        | availableQuoteId = acc.availableQuoteId + 1
                        , qualifiedWords =
                            Dict.insert quoteName

Modified tests/Test/Qualifier.elm from [c3b30e2564] to [7488df3293].

1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316









1317













1318































































































                        inProgressAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromList
                                    [ ( "/external/package/mod/add", dummyWord ) ]
                            }
                    in
                    QualifierUtil.expectExternalOutput
                        inProgressAst
                        unqualifiedAst
                        expectedAst









            ]













        ]










































































































>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434

                        inProgressAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromList
                                    [ ( "/external/package/mod/add", dummyWord ) ]
                            }
                    in
                    QualifierUtil.expectExternalOutput
                        inProgressAst
                        unqualifiedAst
                        expectedAst
            , test "Module alias" <|
                \_ ->
                    let
                        unqualifiedAst =
                            { moduleDefinition =
                                { aliases =
                                    Dict.fromList
                                        [ ( "ext", "/mod" )
                                        , ( "internal", "internal/mod" )
                                        ]
                                , imports = Dict.empty
                                , exposes = Set.empty
                                }
                            , types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "external-call"
                                      , metadata = Metadata.default
                                      , implementation =
                                            AST.SoloImpl
                                                [ AST.Integer emptyRange 1
                                                , AST.PackageWord emptyRange [ "internal" ] "value"
                                                , AST.PackageWord emptyRange [ "ext" ] "add"
                                                ]
                                      }
                                    ]
                            }

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "external-call"
                                      , metadata = Metadata.default
                                      , implementation =
                                            SoloImpl
                                                [ Integer emptyRange 1
                                                , Word emptyRange "internal/mod/value"
                                                , Word emptyRange "/external/package/mod/add"
                                                ]
                                      }
                                    ]
                            }

                        inProgressAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromList
                                    [ ( "/external/package/mod/add", dummyWord )
                                    , ( "internal/mod/value", dummyWord )
                                    ]
                            }
                    in
                    QualifierUtil.expectExternalOutput
                        inProgressAst
                        unqualifiedAst
                        expectedAst
            , test "Module and function aliases" <|
                \_ ->
                    let
                        unqualifiedAst =
                            { moduleDefinition =
                                { aliases =
                                    Dict.fromList
                                        [ ( "ext", "/mod" ) ]
                                , imports = Dict.empty
                                , exposes = Set.empty
                                }
                            , types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "external-call"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.withAlias "internal" "internal/mod"
                                      , implementation =
                                            AST.SoloImpl
                                                [ AST.Integer emptyRange 1
                                                , AST.PackageWord emptyRange [ "internal" ] "value"
                                                , AST.PackageWord emptyRange [ "ext" ] "add"
                                                ]
                                      }
                                    ]
                            }

                        expectedAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromListBy .name
                                    [ { name = "external-call"
                                      , metadata =
                                            Metadata.default
                                                |> Metadata.withAlias "internal" "internal/mod"
                                      , implementation =
                                            SoloImpl
                                                [ Integer emptyRange 1
                                                , Word emptyRange "internal/mod/value"
                                                , Word emptyRange "/external/package/mod/add"
                                                ]
                                      }
                                    ]
                            }

                        inProgressAst =
                            { types = Dict.empty
                            , words =
                                Dict.fromList
                                    [ ( "/external/package/mod/add", dummyWord )
                                    , ( "internal/mod/value", dummyWord )
                                    ]
                            }
                    in
                    QualifierUtil.expectExternalOutput
                        inProgressAst
                        unqualifiedAst
                        expectedAst
            ]
        ]