diff --git a/src/Morphir/Snowpark/Backend.elm b/src/Morphir/Snowpark/Backend.elm index 8ba93dfb6..b77118853 100644 --- a/src/Morphir/Snowpark/Backend.elm +++ b/src/Morphir/Snowpark/Backend.elm @@ -18,6 +18,8 @@ import Morphir.Scala.Common exposing (mapValueName) import Morphir.IR.Value as Value exposing (Pattern(..), Value(..)) import Morphir.Snowpark.MappingContext as MappingContext import Morphir.IR.FQName as FQName +import Morphir.IR.Literal exposing (Literal(..)) +import Morphir.Snowpark.Constants as Constants type alias Options = {} @@ -126,5 +128,25 @@ mapFunctionBody value = Maybe.Just (mapValue value.body) mapValue : Value ta (Type ()) -> Scala.Value -mapValue _ = - Scala.Literal (Scala.StringLit "To Do") \ No newline at end of file +mapValue value = + case value of + Literal tpe literal -> + mapLiteral tpe literal + _ -> + Scala.Literal (Scala.StringLit "To Do") + +mapLiteral : ta -> Literal -> Scala.Value +mapLiteral tpe literal = + case literal of + CharLiteral val -> + Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.CharacterLit val))] + StringLiteral val -> + Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.StringLit val))] + BoolLiteral val -> + Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.BooleanLit val))] + WholeNumberLiteral val -> + Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.IntegerLit val))] + FloatLiteral val -> + Constants.applySnowparkFunc "lit" [(Scala.Literal (Scala.FloatLit val))] + _ -> + Debug.todo "The type '_' is not implemented" \ No newline at end of file diff --git a/src/Morphir/Snowpark/Constants.elm b/src/Morphir/Snowpark/Constants.elm new file mode 100644 index 000000000..e555644f8 --- /dev/null +++ b/src/Morphir/Snowpark/Constants.elm @@ -0,0 +1,11 @@ +module Morphir.Snowpark.Constants exposing (..) +import Morphir.Scala.AST as Scala + +functionsNamespace : List String +functionsNamespace = ["com", "snowpark", "functions"] + +applySnowparkFunc : String -> List Scala.Value -> Scala.Value +applySnowparkFunc name args = + Scala.Apply + (Scala.Ref functionsNamespace name) + (args |> List.map (\v -> Scala.ArgValue Nothing v)) \ No newline at end of file diff --git a/tests/Morphir/Snowpark/MapValueLiteralTests.elm b/tests/Morphir/Snowpark/MapValueLiteralTests.elm new file mode 100644 index 000000000..e6828edab --- /dev/null +++ b/tests/Morphir/Snowpark/MapValueLiteralTests.elm @@ -0,0 +1,85 @@ +module Morphir.Snowpark.MapValueLiteralTests exposing (mapValueLiteralTests) +import Expect +import Test exposing (Test, describe, test) +import Morphir.IR.Literal as Literal +import Morphir.Snowpark.Backend exposing (mapValue) +import Morphir.Scala.AST as Scala +import Morphir.IR.Value as Value +import Morphir.IR.Type as Type + +functionNamespace : List String +functionNamespace = ["com", "snowpark", "functions"] + +booleanReference : Type.Type () +booleanReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "boolean" ] ) [] +booleanTest : Scala.Value +booleanTest = Scala.Apply + (Scala.Ref functionNamespace "lit") + ([Scala.ArgValue + Nothing (Scala.Literal (Scala.BooleanLit True))]) + +stringReference : Type.Type () +stringReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "boolean" ] ) [] +stringTest : Scala.Value +stringTest = Scala.Apply + (Scala.Ref functionNamespace "lit") + ([Scala.ArgValue + Nothing (Scala.Literal (Scala.StringLit "Hello world"))]) + +characterReference : Type.Type () +characterReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "character" ] ) [] +characterTest : Scala.Value +characterTest = Scala.Apply + (Scala.Ref functionNamespace "lit") + ([Scala.ArgValue + Nothing (Scala.Literal (Scala.CharacterLit 'C'))]) + +floatReference : Type.Type () +floatReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "float" ] ) [] +floatTest : Scala.Value +floatTest = Scala.Apply + (Scala.Ref functionNamespace "lit") + ([Scala.ArgValue + Nothing (Scala.Literal (Scala.FloatLit 3.24))]) + + +integerReference : Type.Type () +integerReference = Type.Reference () ( [ [ "morphir" ], [ "s", "d", "k" ] ], [ [ "basics" ] ], [ "integer" ] ) [] +integerTest : Scala.Value +integerTest = Scala.Apply + (Scala.Ref functionNamespace "lit") + ([Scala.ArgValue + Nothing (Scala.Literal (Scala.IntegerLit 5))]) + +mapValueLiteralTests: Test +mapValueLiteralTests = + let + assertBooleanLiteral = + test ("Convert boolean") <| + \_ -> + Expect.equal booleanTest (mapValue (Value.Literal booleanReference (Literal.BoolLiteral True))) + assertStringLiteral = + test ("Convert string") <| + \_ -> + Expect.equal stringTest (mapValue (Value.Literal stringReference (Literal.StringLiteral "Hello world"))) + assertCharacterLiteral = + test ("Convert character") <| + \_ -> + Expect.equal characterTest (mapValue (Value.Literal characterReference (Literal.CharLiteral 'C'))) + assertFloatLiteral = + test ("Convert float") <| + \_ -> + Expect.equal floatTest (mapValue (Value.Literal floatReference (Literal.FloatLiteral 3.24))) + assertIntegerLiteral = + test ("Convert integer") <| + \_ -> + Expect.equal integerTest (mapValue (Value.Literal integerReference (Literal.WholeNumberLiteral 5))) + in + describe "literalMapTransform" + [ + assertBooleanLiteral, + assertStringLiteral, + assertCharacterLiteral, + assertFloatLiteral, + assertIntegerLiteral + ] \ No newline at end of file diff --git a/tests/Morphir/Snowpark/MappingContextTests.elm b/tests/Morphir/Snowpark/MappingContextTests.elm index cc57a0938..7a4be39c6 100644 --- a/tests/Morphir/Snowpark/MappingContextTests.elm +++ b/tests/Morphir/Snowpark/MappingContextTests.elm @@ -1,11 +1,8 @@ module Morphir.Snowpark.MappingContextTests exposing (typeClassificationTests) -import Dict exposing (Dict) -import Set exposing (Set) +import Dict import Test exposing (Test, describe, test) import Expect -import Morphir.IR.Distribution exposing (Distribution) -import Morphir.IR.Distribution as Distribution import Morphir.IR.Path as Path import Morphir.IR.Module exposing (emptyDefinition) import Morphir.IR.AccessControlled exposing (public) @@ -64,15 +61,7 @@ testDistributionPackage = { name = Name.fromString "head", tpe = (Reference () (FQName.fromString "UTest:MyMod:Emp1" ":") []) } ]) }) ] } ) - ]}) - -testDistribution : Distribution -testDistribution = - Distribution.Library - testDistributionName - Dict.empty - testDistributionPackage - + ]}) typeClassificationTests : Test typeClassificationTests =