Skip to content

Commit

Permalink
Added resolution for core SDK types. finos#37
Browse files Browse the repository at this point in the history
  • Loading branch information
AttilaMihaly committed Mar 26, 2020
1 parent 146f34f commit c424c50
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/Morphir/Elm/Frontend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Morphir.IR.Advanced.Value as Value exposing (Value)
import Morphir.IR.FQName as FQName exposing (FQName, fQName)
import Morphir.IR.Name as Name exposing (Name)
import Morphir.IR.Path as Path exposing (Path)
import Morphir.IR.SDK as SDK
import Morphir.JsonExtra as JsonExtra
import Morphir.ResultList as ResultList
import Morphir.Rewrite as Rewrite
Expand Down Expand Up @@ -276,12 +277,18 @@ mapProcessedFile currentPackagePath processedFile modulesSoFar =
|> Dict.map
(\path def ->
Module.definitionToDeclaration def
|> Module.eraseDeclarationExtra
)

dependencies =
Dict.fromList
[ ( [ [ "morphir" ], [ "s", "d", "k" ] ], SDK.packageDeclaration )
]

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

typesResult : Result Errors (Dict Name (AccessControlled (Type.Definition SourceLocation)))
Expand Down
33 changes: 32 additions & 1 deletion src/Morphir/Elm/Frontend/Resolve.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Dict exposing (Dict)
import Elm.Syntax.Exposing exposing (Exposing(..), TopLevelExpose(..))
import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Range exposing (emptyRange)
import Json.Encode as Encode
import Morphir.IR.Advanced.Module as Module
import Morphir.IR.Advanced.Package as Package
Expand Down Expand Up @@ -92,6 +93,32 @@ type alias PackageResolver =
}


defaultImports : List Import
defaultImports =
let
importExplicit : ModuleName -> Maybe ModuleName -> List TopLevelExpose -> Import
importExplicit moduleName maybeAlias exposingList =
Import
(Node emptyRange moduleName)
(maybeAlias
|> Maybe.map (Node emptyRange)
)
(exposingList
|> List.map (Node emptyRange)
|> Explicit
|> Node emptyRange
|> Just
)
in
[ importExplicit [ "Morphir", "SDK", "Bool" ] Nothing [ TypeOrAliasExpose "Bool" ]
, importExplicit [ "Morphir", "SDK", "Int" ] Nothing [ TypeOrAliasExpose "Int" ]
, importExplicit [ "Morphir", "SDK", "Float" ] Nothing [ TypeOrAliasExpose "Float" ]
, importExplicit [ "Morphir", "SDK", "String" ] Nothing [ TypeOrAliasExpose "String" ]
, importExplicit [ "Morphir", "SDK", "Maybe" ] Nothing [ TypeOrAliasExpose "Maybe" ]
, importExplicit [ "Morphir", "SDK", "List" ] Nothing [ TypeOrAliasExpose "List" ]
]


createPackageResolver : Dict Path (Package.Declaration a) -> Path -> Dict Path (Module.Declaration a) -> PackageResolver
createPackageResolver dependencies currentPackagePath currentPackageModules =
let
Expand Down Expand Up @@ -215,8 +242,12 @@ createPackageResolver dependencies currentPackagePath currentPackageModules =


createModuleResolver : PackageResolver -> List Import -> ModuleResolver
createModuleResolver packageResolver imports =
createModuleResolver packageResolver explicitImports =
let
imports : List Import
imports =
defaultImports ++ explicitImports

explicitNames : (ModuleName -> TopLevelExpose -> List LocalName) -> Dict LocalName ModuleName
explicitNames matchExpose =
imports
Expand Down
18 changes: 17 additions & 1 deletion src/Morphir/IR/Advanced/Module.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Morphir.IR.Advanced.Module exposing
( Declaration, Definition
, encodeDeclaration, encodeDefinition
, definitionToDeclaration, mapDeclaration, mapDefinition
, definitionToDeclaration, eraseDeclarationExtra, mapDeclaration, mapDefinition
)

{-| Modules are groups of types and values that belong together.
Expand Down Expand Up @@ -30,6 +30,13 @@ type alias Declaration extra =
}


emptyDeclaration : Declaration extra
emptyDeclaration =
{ types = Dict.empty
, values = Dict.empty
}


{-| Type that represents a module definition. It includes types and values.
-}
type alias Definition extra =
Expand Down Expand Up @@ -71,6 +78,15 @@ definitionToDeclaration def =
}


eraseDeclarationExtra : Declaration a -> Declaration ()
eraseDeclarationExtra decl =
decl
|> mapDeclaration
(Type.mapTypeExtra (\_ -> ()) >> Ok)
(Value.mapValueExtra (\_ -> ()))
|> Result.withDefault emptyDeclaration


{-| -}
encodeDeclaration : (extra -> Encode.Value) -> Declaration extra -> Encode.Value
encodeDeclaration encodeExtra decl =
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 @@ -3,7 +3,7 @@ module Morphir.IR.Advanced.Type exposing
, variable, reference, tuple, record, extensibleRecord, function, unit
, matchVariable, matchReference, matchTuple, matchRecord, matchExtensibleRecord, matchFunction, matchUnit
, Field, matchField, mapFieldName, mapFieldType
, Declaration, typeAliasDeclaration, opaqueTypeDeclaration, customTypeDeclaration, matchCustomTypeDeclaration
, Declaration(..), typeAliasDeclaration, opaqueTypeDeclaration, customTypeDeclaration, matchCustomTypeDeclaration
, Definition(..), typeAliasDefinition, customTypeDefinition
, Constructors
, fuzzType
Expand Down
14 changes: 14 additions & 0 deletions src/Morphir/IR/SDK.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Morphir.IR.SDK exposing (..)

import Dict
import Morphir.IR.Advanced.Package as Package
import Morphir.IR.SDK.Int as Int


packageDeclaration : Package.Declaration ()
packageDeclaration =
{ modules =
Dict.fromList
[ ( [ [ "int" ] ], Int.moduleDeclaration )
]
}
2 changes: 1 addition & 1 deletion src/Morphir/IR/SDK/Bool.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
Expand Down
8 changes: 8 additions & 0 deletions src/Morphir/IR/SDK/Common.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Morphir.IR.SDK.Common exposing (..)

import Morphir.IR.Path exposing (Path)


packageName : Path
packageName =
[ [ "morphir" ], [ "s", "d", "k" ] ]
2 changes: 1 addition & 1 deletion src/Morphir/IR/SDK/Float.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
Expand Down
17 changes: 15 additions & 2 deletions src/Morphir/IR/SDK/Int.elm
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
module Morphir.IR.SDK.Int exposing (..)

import Morphir.IR.Advanced.Type exposing (Type(..))
import Dict
import Morphir.IR.Advanced.Module as Module
import Morphir.IR.Advanced.Type exposing (Declaration(..), Type(..))
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
moduleName =
[ [ "int" ] ]


moduleDeclaration : Module.Declaration ()
moduleDeclaration =
{ types =
Dict.fromList
[ ( [ "int" ], OpaqueTypeDeclaration [] )
]
, values =
Dict.empty
}


fromLocalName : String -> FQName
fromLocalName name =
name
Expand Down
2 changes: 1 addition & 1 deletion src/Morphir/IR/SDK/List.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
Expand Down
4 changes: 2 additions & 2 deletions src/Morphir/IR/SDK/Maybe.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Morphir.IR.SDK.Bool exposing (..)
module Morphir.IR.SDK.Maybe exposing (..)

import Morphir.IR.Advanced.Type exposing (Type(..))
import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
Expand Down
8 changes: 0 additions & 8 deletions src/Morphir/IR/SDK/Package.elm

This file was deleted.

2 changes: 1 addition & 1 deletion src/Morphir/IR/SDK/String.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Morphir.IR.FQName as FQName exposing (FQName)
import Morphir.IR.Name as Name
import Morphir.IR.Path exposing (Path)
import Morphir.IR.QName as QName
import Morphir.IR.SDK.Package exposing (packageName)
import Morphir.IR.SDK.Common exposing (packageName)


moduleName : Path
Expand Down
73 changes: 72 additions & 1 deletion tests/Morphir/Elm/FrontendTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ module Morphir.Elm.FrontendTests exposing (..)

import Dict
import Expect
import Morphir.Elm.Frontend as Frontend exposing (SourceLocation)
import Morphir.Elm.Frontend as Frontend exposing (Errors, SourceFile, SourceLocation)
import Morphir.IR.AccessControlled exposing (AccessControlled, private, public)
import Morphir.IR.Advanced.Package as Package
import Morphir.IR.Advanced.Type as Type
import Morphir.IR.Advanced.Value as Value exposing (Definition(..), Literal(..), Value(..))
import Morphir.IR.FQName exposing (fQName)
import Morphir.IR.Path as Path
import Morphir.IR.SDK.Bool as Bool
import Morphir.IR.SDK.Float as Float
import Morphir.IR.SDK.Int as Int
import Morphir.IR.SDK.List as List
import Morphir.IR.SDK.Maybe as Maybe
import Morphir.IR.SDK.String as String
import Set
import Test exposing (..)

Expand All @@ -30,6 +37,12 @@ type alias Bar = Foo
type alias Rec =
{ field1 : Foo
, field2 : Bar
, field3 : Bool
, field4 : Int
, field5 : Float
, field6 : String
, field7 : Maybe Int
, field8 : List Float
}
"""
}
Expand Down Expand Up @@ -97,6 +110,18 @@ type Bee = Bee
(Type.reference (fQName packageName [ [ "a" ] ] [ "foo" ]) [] ())
, Type.Field [ "field", "2" ]
(Type.reference (fQName packageName [ [ "a" ] ] [ "bar" ]) [] ())
, Type.Field [ "field", "3" ]
(Bool.boolType ())
, Type.Field [ "field", "4" ]
(Int.intType ())
, Type.Field [ "field", "5" ]
(Float.floatType ())
, Type.Field [ "field", "6" ]
(String.stringType ())
, Type.Field [ "field", "7" ]
(Maybe.maybeType (Int.intType ()) ())
, Type.Field [ "field", "8" ]
(List.listType (Float.floatType ()) ())
]
()
)
Expand Down Expand Up @@ -132,6 +157,52 @@ type Bee = Bee
|> Expect.equal (Ok expected)


valueTests : Test
valueTests =
let
packageInfo =
{ name = []
, exposedModules = Set.empty
}

moduleSource : String -> SourceFile
moduleSource sourceValue =
{ path = "Test.elm"
, content =
String.join "\n"
[ "module Test exposing (..)"
, ""
, "testValue = " ++ sourceValue
]
}

checkIR : String -> Value () -> Test
checkIR valueSource expectedValueIR =
test valueSource <|
\_ ->
Frontend.packageDefinitionFromSource packageInfo [ moduleSource valueSource ]
|> Result.map Package.eraseDefinitionExtra
|> Result.toMaybe
|> Maybe.andThen
(\packageDef ->
packageDef.modules
|> Dict.get [ [ "test" ] ]
|> Maybe.andThen
(\moduleDef ->
moduleDef.value.values
|> Dict.get [ "test", "value" ]
|> Maybe.map (.value >> Value.getDefinitionBody)
)
)
|> Maybe.map (Expect.equal expectedValueIR)
|> Maybe.withDefault (Expect.fail "Could not find the value in the IR")
in
skip <|
describe "Values are mapped correctly"
[ checkIR "1" <| Literal (IntLiteral 1) ()
]


unindent : String -> String
unindent text =
text

0 comments on commit c424c50

Please sign in to comment.