Skip to content

Commit

Permalink
Added local name resolution to frontend (#20)
Browse files Browse the repository at this point in the history
* Fix compilation errors.

* Fix isPrefixOf. #4

* Setup for NPM publishing. #1

* Set package version

* Improved CLI. #3, #1

* Added more documentation. #1, #3

* Fix package publish errors. #1

* Do not print done if STDOUT is used.

* Print JSON in compact form.

* Better error handling.

* Added resolution for local names.
  • Loading branch information
AttilaMihaly authored Mar 17, 2020
1 parent 6cbf344 commit 1726587
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
70 changes: 48 additions & 22 deletions src/Morphir/Elm/Frontend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import Json.Decode as Decode
import Json.Encode as Encode
import Morphir.DAG as DAG exposing (DAG)
import Morphir.Elm.Frontend.Resolve as Resolve exposing (ModuleResolver, PackageResolver)
import Morphir.IR.AccessControlled exposing (AccessControlled, private, public)
import Morphir.IR.AccessControlled as AccessControlled exposing (AccessControlled, private, public)
import Morphir.IR.Advanced.Module as Module
import Morphir.IR.Advanced.Package as Package
import Morphir.IR.Advanced.Type as Type exposing (Type)
import Morphir.IR.Advanced.Value as Value exposing (Value)
import Morphir.IR.FQName exposing (fQName)
import Morphir.IR.FQName as FQName exposing (fQName)
import Morphir.IR.Name as Name exposing (Name)
import Morphir.IR.Path as Path exposing (Path)
import Morphir.ResultList as ResultList
import Morphir.Rewrite as Rewrite
import Parser
import Set exposing (Set)

Expand Down Expand Up @@ -238,19 +239,6 @@ mapProcessedFile currentPackagePath processedFile modulesSoFar =
|> Node.value
|> ElmModule.exposingList

moduleDeclsSoFar =
modulesSoFar
|> Dict.map
(\path def ->
Module.definitionToDeclaration def
)

moduleResolver : ModuleResolver
moduleResolver =
Resolve.createModuleResolver
(Resolve.createPackageResolver Dict.empty currentPackagePath moduleDeclsSoFar)
(processedFile.file.imports |> List.map Node.value)

typesResult : Result Errors (Dict Name (AccessControlled (Type.Definition SourceLocation)))
typesResult =
mapDeclarationsToType processedFile.parsedFile.sourceFile moduleExpose (processedFile.file.declarations |> List.map Node.value)
Expand All @@ -259,14 +247,20 @@ mapProcessedFile currentPackagePath processedFile modulesSoFar =
valuesResult : Result Errors (Dict Name (AccessControlled (Value.Definition SourceLocation)))
valuesResult =
Ok Dict.empty

moduleResult : Result Errors (Module.Definition SourceLocation)
moduleResult =
Result.map2 Module.Definition
typesResult
valuesResult
in
Result.map2
(\types values ->
modulesSoFar
|> Dict.insert modulePath (Module.Definition types values)
)
typesResult
valuesResult
moduleResult
|> Result.andThen (resolveLocalTypes currentPackagePath modulePath)
|> Result.map
(\m ->
modulesSoFar
|> Dict.insert modulePath m
)


mapDeclarationsToType : SourceFile -> Exposing -> List Declaration -> Result Errors (List ( Name, AccessControlled (Type.Definition SourceLocation) ))
Expand Down Expand Up @@ -483,6 +477,38 @@ mapTypeAnnotation sourceFile (Node range typeAnnotation) =
(mapTypeAnnotation sourceFile returnTypeNode)


resolveLocalTypes : Path -> Path -> Module.Definition SourceLocation -> Result Errors (Module.Definition SourceLocation)
resolveLocalTypes packagePath modulePath moduleDef =
let
rewriteTypes =
Rewrite.bottomUp Type.rewriteType
(\tpe ->
case tpe of
Type.Reference fullName args sourceLocation ->
let
localName =
fullName
|> FQName.getLocalName
in
moduleDef.types
|> Dict.get localName
|> Maybe.map
(\_ ->
Type.Reference (fQName packagePath modulePath localName) args sourceLocation
)

_ ->
Nothing
)

rewriteValues =
identity
in
moduleDef
|> Module.mapDefinition rewriteTypes rewriteValues
|> Ok


withAccessControl : Bool -> a -> AccessControlled a
withAccessControl isExposed a =
if isExposed then
Expand Down
2 changes: 1 addition & 1 deletion src/Morphir/IR/Advanced/Type.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Morphir.IR.Advanced.Type exposing
, Constructors
, fuzzType
, encodeType, decodeType, encodeDeclaration, encodeDefinition
, Constructor, definitionToDeclaration, mapDeclaration, mapDefinition, mapTypeExtra
, Constructor, definitionToDeclaration, mapDeclaration, mapDefinition, mapTypeExtra, rewriteType
)

{-| This module contains the building blocks of types in the Morphir IR.
Expand Down
14 changes: 10 additions & 4 deletions src/Morphir/Rewrite.elm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ the entire tree regardless of rule matches but only changes the tree if
a rule matches.
-}
bottomUp : Rewrite a -> Rule a -> a -> a
bottomUp rewrite rewriteRule =
rewrite
(bottomUp rewrite rewriteRule)
(Rule.defaultToOriginal rewriteRule)
bottomUp rewrite rewriteRule typeToRewrite =
let
top =
rewrite
(\a -> bottomUp rewrite rewriteRule a)
(Rule.defaultToOriginal rewriteRule)
typeToRewrite
in
rewriteRule top
|> Maybe.withDefault top
30 changes: 18 additions & 12 deletions tests/Morphir/Elm/FrontendTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ module B exposing (..)
"""
}

packageName =
Path.fromString "my/package"

moduleA =
Path.fromString "A"

packageInfo =
{ name =
packageName
, exposedModules =
Set.fromList
[ moduleA
]
}

expected : Package.Definition ()
expected =
{ dependencies = Dict.empty
Expand All @@ -52,7 +67,7 @@ module B exposing (..)
[ ( [ "bar" ]
, public
(Type.typeAliasDefinition []
(Type.reference (fQName [] [] [ "foo" ]) [] ())
(Type.reference (fQName packageName moduleA [ "foo" ]) [] ())
)
)
, ( [ "foo" ]
Expand All @@ -72,9 +87,9 @@ module B exposing (..)
(Type.typeAliasDefinition []
(Type.record
[ Type.field [ "field", "1" ]
(Type.reference (fQName [] [] [ "foo" ]) [] ())
(Type.reference (fQName packageName moduleA [ "foo" ]) [] ())
, Type.field [ "field", "2" ]
(Type.reference (fQName [] [] [ "bar" ]) [] ())
(Type.reference (fQName packageName moduleA [ "bar" ]) [] ())
]
()
)
Expand All @@ -95,15 +110,6 @@ module B exposing (..)
)
]
}

packageInfo =
{ name =
Path.fromString "my/package"
, exposedModules =
Set.fromList
[ Path.fromString "A"
]
}
in
test "first" <|
\_ ->
Expand Down

0 comments on commit 1726587

Please sign in to comment.