diff --git a/src/Morphir/Snowpark/AccessElementMapping.elm b/src/Morphir/Snowpark/AccessElementMapping.elm index 2d1afc270..bc644e20c 100644 --- a/src/Morphir/Snowpark/AccessElementMapping.elm +++ b/src/Morphir/Snowpark/AccessElementMapping.elm @@ -26,6 +26,7 @@ import Morphir.Snowpark.ReferenceUtils exposing (isValueReferenceToSimpleTypesRe import Morphir.IR.Value as Value import Morphir.Snowpark.MappingContext exposing (isAnonymousRecordWithSimpleTypes) import Morphir.Snowpark.Constants exposing (applySnowparkFunc) +import Morphir.Snowpark.MappingContext exposing (isUnionTypeWithParams) mapFieldAccess : va -> (Value ta (IrType.Type ())) -> Name.Name -> ValueMappingContext -> Scala.Value mapFieldAccess _ value name ctx = @@ -63,7 +64,11 @@ mapConstructorAccess tpe name ctx = (IrType.Reference _ typeName _, _) -> if isUnionTypeWithoutParams typeName ctx.typesContextInfo then scalaReferenceToUnionTypeCase typeName name - else + else if isUnionTypeWithParams typeName ctx.typesContextInfo then + applySnowparkFunc "object_construct" [ + applySnowparkFunc "lit" [Scala.Literal <| Scala.StringLit "__tag"], + applySnowparkFunc "lit" [Scala.Literal <| Scala.StringLit (name |> FQName.getLocalName |> Name.toTitleCase)] ] + else Scala.Literal (Scala.StringLit "Constructor access not converted") _ -> Scala.Literal (Scala.StringLit "Constructor access not converted") diff --git a/src/Morphir/Snowpark/Backend.elm b/src/Morphir/Snowpark/Backend.elm index 379fc3078..718c394fe 100644 --- a/src/Morphir/Snowpark/Backend.elm +++ b/src/Morphir/Snowpark/Backend.elm @@ -2,6 +2,7 @@ module Morphir.Snowpark.Backend exposing (..) import Dict import List +import Set as Set import Morphir.File.FileMap exposing (FileMap) import Morphir.IR.AccessControlled exposing (Access(..), AccessControlled) import Morphir.IR.Distribution as Distribution exposing (..) @@ -33,6 +34,8 @@ import Morphir.Snowpark.MappingContext exposing (ValueMappingContext) import Morphir.Snowpark.MapFunctionsMapping as MapFunctionsMapping import Morphir.Snowpark.PatternMatchMapping exposing (mapPatternMatch) import Morphir.Snowpark.Constants as Constants +import Morphir.Scala.Common exposing (scalaKeywords) +import Morphir.Scala.Common exposing (javaObjectMethods) type alias Options = {} @@ -178,7 +181,18 @@ generateLocalVariableForRowRecord ctx (name, _, tpe) = generateArgumentDeclarationForFunction : MappingContextInfo () -> ( Name.Name, Type (), Type () ) -> List Scala.ArgDecl generateArgumentDeclarationForFunction ctx (name, _, tpe) = - [Scala.ArgDecl [] (mapTypeReference tpe ctx) (name |> Name.toCamelCase) Nothing] + [Scala.ArgDecl [] (mapTypeReference tpe ctx) (name |> generateParameterName) Nothing] + +generateParameterName : Name.Name -> String +generateParameterName name = + let + scalaName = name |> Name.toCamelCase + in + if Set.member scalaName scalaKeywords || Set.member scalaName javaObjectMethods then + "_" ++ scalaName + else + scalaName + processParameters : List ( Name.Name, Type (), Type () ) -> MappingContextInfo () -> (List (List Scala.ArgDecl), List (Scala.MemberDecl) ) processParameters inputTypes ctx = diff --git a/src/Morphir/Snowpark/MapFunctionsMapping.elm b/src/Morphir/Snowpark/MapFunctionsMapping.elm index db04de854..bc10395b8 100644 --- a/src/Morphir/Snowpark/MapFunctionsMapping.elm +++ b/src/Morphir/Snowpark/MapFunctionsMapping.elm @@ -128,10 +128,22 @@ tryToConvertUserFunctionCall (func, args) mapValue ctx = case argsToUse of [] -> funcReference - _ -> - Scala.Apply funcReference argsToUse + (first::rest) -> + List.foldr (\a c -> Scala.Apply c [a]) (Scala.Apply funcReference [first]) rest else Scala.Literal (Scala.StringLit "Call not converted") + ValueIR.Constructor _ constructorName -> + if isLocalFunctionName constructorName ctx && List.length args > 0 then + let + argsToUse = + args + |> List.indexedMap (\i arg -> ("field" ++ (String.fromInt i), mapValue arg ctx)) + |> List.concatMap (\(field, value) -> [Constants.applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit field), value]]) + tag = [ Constants.applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit "__tag")], + Constants.applySnowparkFunc "lit" [ Scala.Literal (Scala.StringLit <| ( constructorName |> FQName.getLocalName |> Name.toTitleCase))]] + in Constants.applySnowparkFunc "object_construct" (tag ++ argsToUse) + else + Scala.Literal (Scala.StringLit "Constructor call not converted") _ -> Scala.Literal (Scala.StringLit "Call not converted") diff --git a/src/Morphir/Snowpark/PatternMatchMapping.elm b/src/Morphir/Snowpark/PatternMatchMapping.elm index 347f7b33f..7778d2b51 100644 --- a/src/Morphir/Snowpark/PatternMatchMapping.elm +++ b/src/Morphir/Snowpark/PatternMatchMapping.elm @@ -71,7 +71,7 @@ mapPatternMatch (tpe, expr, cases) mapValue ctx = referenceExpr = mapValue expr ctx compareWithExpr : Name.Name -> Scala.Value compareWithExpr = \name -> - equalExpr (Scala.Apply referenceExpr [Scala.ArgValue Nothing (applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit "__tag")])]) + equalExpr (Scala.Apply referenceExpr [Scala.ArgValue Nothing (Scala.Literal (Scala.StringLit "__tag"))]) (applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit (Name.toTitleCase name))]) changeCtxt = addBindingReplacementsToContext ctx restPairs = rest |> List.map (\(constr, bindings, val) -> ((compareWithExpr constr), (mapValue val (changeCtxt bindings referenceExpr)))) @@ -81,7 +81,7 @@ mapPatternMatch (tpe, expr, cases) mapValue ctx = let referenceExpr = mapValue expr ctx compareWithExpr = \name -> - equalExpr (Scala.Apply (mapValue expr ctx) [Scala.ArgValue Nothing (applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit "__tag")])]) + equalExpr (Scala.Apply (mapValue expr ctx) [Scala.ArgValue Nothing (Scala.Literal (Scala.StringLit "__tag"))]) (applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit ( Name.toTitleCase name))]) changeCtxt = addBindingReplacementsToContext ctx restPairs = rest |> List.map (\(constr, bindings, val) -> ((compareWithExpr constr), (mapValue val (changeCtxt bindings referenceExpr)))) @@ -98,16 +98,16 @@ mapPatternMatch (tpe, expr, cases) mapValue ctx = _ -> Scala.Variable "NOT_CONVERTED" -generateBindingVariableExpr : Name.Name -> Scala.Value -> Scala.Value +generateBindingVariableExpr : String -> Scala.Value -> Scala.Value generateBindingVariableExpr name expr = - Scala.Apply expr [Scala.ArgValue Nothing (applySnowparkFunc "lit" [Scala.Literal (Scala.StringLit (name |> Name.toCamelCase))])] + Scala.Apply expr [Scala.ArgValue Nothing (Scala.Literal (Scala.StringLit name))] addBindingReplacementsToContext : ValueMappingContext -> List Name.Name -> Scala.Value -> ValueMappingContext addBindingReplacementsToContext ctxt bindingVariables referenceExpr = let newReplacements = bindingVariables - |> List.map (\name -> (name, generateBindingVariableExpr name referenceExpr)) + |> List.indexedMap (\i name -> (name, generateBindingVariableExpr ("field" ++ (String.fromInt i)) referenceExpr)) |> Dict.fromList in { ctxt | inlinedIds = Dict.union ctxt.inlinedIds newReplacements }