Skip to content

Commit

Permalink
Merge pull request #2 from Morgan-Stanley/master
Browse files Browse the repository at this point in the history
sync from origin
  • Loading branch information
stephengoldbaum authored Aug 16, 2020
2 parents 2788897 + 91480ba commit 600a21f
Show file tree
Hide file tree
Showing 18 changed files with 981 additions and 835 deletions.
9 changes: 5 additions & 4 deletions cli/morphir-elm-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ const program = new commander.Command()
program
.name('morphir-elm gen')
.description('Generate code from Morphir IR')
.option('-i, --input <path>', 'Source location where the Morphir IR will be loaded from. Defaults to STDIN.')
.option('-o, --output <path>', 'Target location where the generated code will be saved. Defaults to ./dist.', './dist')
.parse(process.argv)


gen(path.resolve(program.output), { targetPackage: ["com", "foo"] })
gen(program.input, path.resolve(program.output), {})
.then(() => {
console.log("Done.")
})
Expand All @@ -35,9 +36,9 @@ gen(path.resolve(program.output), { targetPackage: ["com", "foo"] })
process.exit(1)
})

async function gen(outputPath, options) {
const morphirIrJson = await getStdin()
const fileMap = await generate(options, JSON.parse(morphirIrJson))
async function gen(input, outputPath, options) {
const morphirIrJson = input ? await readFile(path.resolve(input)) : await getStdin()
const fileMap = await generate(options, JSON.parse(morphirIrJson.toString()))
const writePromises =
fileMap.map(async ([[dirPath, fileName], content]) => {
const fileDir = dirPath.reduce((accum, next) => path.join(accum, next), outputPath)
Expand Down
35 changes: 18 additions & 17 deletions cli/src/Morphir/Elm/CLI.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{-
Copyright 2020 Morgan Stanley
Copyright 2020 Morgan Stanley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}


Expand Down Expand Up @@ -66,25 +66,26 @@ update msg model =
result =
Frontend.packageDefinitionFromSource packageInfo sourceFiles
|> Result.map Package.eraseDefinitionAttributes
|> Result.map (Package.Library packageInfo.name)
in
( model, result |> encodeResult (Encode.list encodeError) (PackageCodec.encodeDefinition (\_ -> Encode.object [])) |> packageDefinitionFromSourceResult )
( model, result |> encodeResult (Encode.list encodeError) PackageCodec.encodeDistribution |> packageDefinitionFromSourceResult )

Err errorMessage ->
( model, errorMessage |> Decode.errorToString |> decodeError )

Generate ( optionsJson, packageDefJson ) ->
Generate ( optionsJson, packageDistJson ) ->
let
optionsResult =
Decode.decodeValue decodeOptions optionsJson

packageDefResult =
Decode.decodeValue (PackageCodec.decodeDefinition (Decode.succeed ())) packageDefJson
packageDistroResult =
Decode.decodeValue PackageCodec.decodeDistribution packageDistJson
in
case Result.map2 Tuple.pair optionsResult packageDefResult of
Ok ( options, packageDef ) ->
case Result.map2 Tuple.pair optionsResult packageDistroResult of
Ok ( options, packageDist ) ->
let
fileMap =
Backend.mapPackageDefinition options [ [ "morphir" ] ] packageDef
Backend.mapDistribution options packageDist
in
( model, fileMap |> Ok |> encodeResult Encode.string encodeFileMap |> generateResult )

Expand Down
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"elm-community/graph": "6.0.0 <= v < 7.0.0",
"elm-community/list-extra": "8.2.4 <= v < 9.0.0",
"elm-explorations/test": "1.2.2 <= v < 2.0.0",
"stil4m/elm-syntax": "7.1.1 <= v < 8.0.0"
"stil4m/elm-syntax": "7.1.3 <= v < 8.0.0"
},
"test-dependencies": {}
}
74 changes: 48 additions & 26 deletions src/Morphir/Elm/Frontend.elm
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{-
Copyright 2020 Morgan Stanley
Copyright 2020 Morgan Stanley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}


Expand All @@ -25,11 +25,12 @@ import Elm.Syntax.Declaration exposing (Declaration(..))
import Elm.Syntax.Exposing as Exposing exposing (Exposing)
import Elm.Syntax.Expression as Expression exposing (Expression, Function, FunctionImplementation)
import Elm.Syntax.File exposing (File)
import Elm.Syntax.Infix as Infix
import Elm.Syntax.Module as ElmModule
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node exposing (Node(..))
import Elm.Syntax.Pattern as Pattern exposing (Pattern(..))
import Elm.Syntax.Range exposing (Range)
import Elm.Syntax.Range as Range exposing (Range)
import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..))
import Graph exposing (Graph)
import Json.Decode as Decode
Expand Down Expand Up @@ -165,6 +166,7 @@ type Error
| DuplicateNameInPattern Name SourceLocation SourceLocation
| VariableShadowing Name SourceLocation SourceLocation
| MissingTypeSignature SourceLocation
| RecordPatternNotSupported SourceLocation


encodeDeadEnd : DeadEnd -> Encode.Value
Expand Down Expand Up @@ -223,6 +225,11 @@ encodeError error =
[ encodeSourceLocation sourceLocation
]

RecordPatternNotSupported sourceLocation ->
JsonExtra.encodeConstructor "RecordPatternNotSupported"
[ encodeSourceLocation sourceLocation
]


type alias Imports =
{ lookupByExposedCtor : String -> Maybe Import
Expand Down Expand Up @@ -308,7 +315,7 @@ packageDefinitionFromSource packageInfo sourceFiles =
|> Morphir.Graph.topologicalSort
in
if Morphir.Graph.isEmpty cycles then
Ok sortedModules
Ok (sortedModules |> List.reverse)

else
Err [ CyclicModules cycles ]
Expand Down Expand Up @@ -764,7 +771,7 @@ mapExpression sourceFile (Node range exp) =
sourceLocation =
range |> SourceLocation sourceFile
in
case exp of
case fixAssociativity exp of
Expression.UnitExpr ->
Ok (Value.Unit sourceLocation)

Expand Down Expand Up @@ -988,12 +995,7 @@ mapPattern sourceFile (Node range pattern) =
|> Result.map (Value.TuplePattern sourceLocation)

Pattern.RecordPattern fieldNameNodes ->
Ok
(Value.RecordPattern sourceLocation
(fieldNameNodes
|> List.map (Node.value >> Name.fromString)
)
)
Err [ RecordPatternNotSupported sourceLocation ]

Pattern.UnConsPattern headNode tailNode ->
Result.map2 (Value.HeadTailPattern sourceLocation)
Expand Down Expand Up @@ -1467,13 +1469,6 @@ resolveVariablesAndReferences variables moduleResolver value =
)
(Ok Dict.empty)

Value.RecordPattern sourceLocation fieldNames ->
Ok
(fieldNames
|> List.map (\fieldName -> ( fieldName, sourceLocation ))
|> Dict.fromList
)

Value.ConstructorPattern _ _ args ->
args
|> List.map namesBoundInPattern
Expand Down Expand Up @@ -1514,6 +1509,15 @@ resolveVariablesAndReferences variables moduleResolver value =
(resolveVariablesAndReferences variablesDefNamesAndArgs moduleResolver def.body)
in
case value of
--Value.Constructor sourceLocation (FQName [] modulePath localName) ->
-- moduleResolver.resolveCtor
-- (modulePath |> List.map Name.toTitleCase)
-- (localName |> Name.toTitleCase)
-- |> Result.map
-- (\resolvedFullName ->
-- Value.Constructor sourceLocation resolvedFullName
-- )
-- |> Result.mapError (ResolveError sourceLocation >> List.singleton)
Value.Reference sourceLocation (FQName [] modulePath localName) ->
if variables |> Dict.member localName then
Ok (Value.Variable sourceLocation localName)
Expand Down Expand Up @@ -1683,3 +1687,21 @@ withAccessControl isExposed a =

else
private a


{-| This is an incomplete fis for an associativity issue in elm-syntax.
It only works when the operators are the same instead of relying on precedence equality.
Consequently it also doesn't take mixed associativities into account.
-}
fixAssociativity : Expression -> Expression
fixAssociativity expr =
case expr of
Expression.OperatorApplication o d (Node lr l) (Node _ (Expression.OperatorApplication ro rd (Node rlr rl) (Node rrr rr))) ->
if (o == ro) && d == Infix.Left then
Expression.OperatorApplication o d (Node (Range.combine [ lr, rlr ]) (Expression.OperatorApplication ro rd (Node lr l) (Node rlr rl))) (Node rrr rr)

else
expr

_ ->
expr
Loading

0 comments on commit 600a21f

Please sign in to comment.