Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements on Scala code generation #142

Merged
merged 8 commits into from
Sep 1, 2020
120 changes: 77 additions & 43 deletions src/Morphir/Scala/Backend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,32 @@ mapCustomTypeDefinition : Package.PackagePath -> Path -> Name -> List Name -> Ac
mapCustomTypeDefinition currentPackagePath currentModulePath typeName typeParams accessControlledCtors =
let
caseClass name args extends =
Scala.Class
{ modifiers = [ Scala.Case ]
, name = name |> Name.toTitleCase
, typeArgs = typeParams |> List.map (Name.toTitleCase >> Scala.TypeVar)
, ctorArgs =
args
|> List.map
(\( argName, argType ) ->
{ modifiers = []
, tpe = mapType argType
, name = argName |> Name.toCamelCase
, defaultValue = Nothing
}
)
|> List.singleton
, extends = extends
}
if List.isEmpty args then
Scala.Object
{ modifiers = [ Scala.Case ]
, name = name |> Name.toTitleCase
, extends = extends
, members = []
}

else
Scala.Class
{ modifiers = [ Scala.Case ]
, name = name |> Name.toTitleCase
, typeArgs = typeParams |> List.map (Name.toTitleCase >> Scala.TypeVar)
, ctorArgs =
args
|> List.map
(\( argName, argType ) ->
{ modifiers = []
, tpe = mapType argType
, name = argName |> Name.toCamelCase
, defaultValue = Nothing
}
)
|> List.singleton
, extends = extends
}

parentTraitRef =
mapFQNameToTypeRef (FQName currentPackagePath currentModulePath typeName)
Expand Down Expand Up @@ -442,34 +451,59 @@ mapValue value =
_ ->
Scala.MatchCases [ ( mapPattern argPattern, mapValue bodyValue ) ]

LetDefinition a defName def inValue ->
LetDefinition _ _ _ _ ->
let
flattenLetDef : Value a -> ( List ( Name, Value.Definition a ), Value a )
flattenLetDef v =
case v of
LetDefinition a dName d inV ->
let
( nestedDefs, nestedInValue ) =
flattenLetDef inV
in
( ( dName, d ) :: nestedDefs, nestedInValue )

_ ->
( [], v )

( defs, finalInValue ) =
flattenLetDef value
in
Scala.Block
[ Scala.FunctionDecl
{ modifiers = []
, name = defName |> Name.toCamelCase
, typeArgs = []
, args =
if List.isEmpty def.inputTypes then
[]
(defs
|> List.map
(\( defName, def ) ->
if List.isEmpty def.inputTypes then
Scala.ValueDecl
{ modifiers = []
, pattern = Scala.NamedMatch (defName |> Name.toCamelCase)
, value = mapValue def.body
}

else
[ def.inputTypes
|> List.map
(\( argName, _, argType ) ->
{ modifiers = []
, tpe = mapType argType
, name = argName |> Name.toCamelCase
, defaultValue = Nothing
}
)
]
, returnType =
Just (mapType def.outputType)
, body =
Just (mapValue def.body)
}
]
(mapValue inValue)
else
Scala.FunctionDecl
{ modifiers = []
, name = defName |> Name.toCamelCase
, typeArgs = []
, args =
[ def.inputTypes
|> List.map
(\( argName, _, argType ) ->
{ modifiers = []
, tpe = mapType argType
, name = argName |> Name.toCamelCase
, defaultValue = Nothing
}
)
]
, returnType =
Just (mapType def.outputType)
, body =
Just (mapValue def.body)
}
)
)
(mapValue finalInValue)

LetRecursion a defs inValue ->
Scala.Block
Expand Down