Skip to content

Commit

Permalink
Added more coverage. finos#46, finos#25, finos#5
Browse files Browse the repository at this point in the history
  • Loading branch information
AttilaMihaly committed Apr 1, 2020
1 parent c841efd commit 9ce6d29
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 6 deletions.
79 changes: 74 additions & 5 deletions src/Morphir/Elm/Frontend.elm
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ mapExpression sourceFile (Node range exp) =
|> Result.mapError List.concat
|> Result.andThen (List.reverse >> toApply)

Expression.OperatorApplication op infixDirection leftNode rightNode ->
Err [ NotSupported sourceLocation "TODO: OperatorApplication" ]

Expression.FunctionOrValue moduleName valueName ->
case ( moduleName, valueName ) of
( [], "True" ) ->
Expand All @@ -703,19 +706,31 @@ mapExpression sourceFile (Node range exp) =
_ ->
Ok (Value.Reference sourceLocation (fQName [] (moduleName |> List.map Name.fromString) (valueName |> Name.fromString)))

Expression.IfBlock condNode thenNode elseNode ->
Result.map3 (Value.IfThenElse sourceLocation)
(mapExpression sourceFile condNode)
(mapExpression sourceFile thenNode)
(mapExpression sourceFile elseNode)

Expression.PrefixOperator op ->
Err [ NotSupported sourceLocation "TODO: PrefixOperator" ]

Expression.Operator op ->
Err [ NotSupported sourceLocation "TODO: Operator" ]

Expression.Integer value ->
Ok (Value.Literal sourceLocation (Value.IntLiteral value))

Expression.Hex value ->
Ok (Value.Literal sourceLocation (Value.IntLiteral value))

Expression.Floatable value ->
Ok (Value.Literal sourceLocation (Value.FloatLiteral value))

Expression.Negation arg ->
mapExpression sourceFile arg
|> Result.map (Number.negate sourceLocation sourceLocation)

Expression.Floatable value ->
Ok (Value.Literal sourceLocation (Value.FloatLiteral value))

Expression.Literal value ->
Ok (Value.Literal sourceLocation (Value.StringLiteral value))

Expand All @@ -729,8 +744,62 @@ mapExpression sourceFile (Node range exp) =
|> Result.mapError List.concat
|> Result.map (Value.Tuple sourceLocation)

other ->
Err [ NotSupported sourceLocation "TODO" ]
Expression.ParenthesizedExpression expNode ->
mapExpression sourceFile expNode

Expression.LetExpression letBlock ->
Err [ NotSupported sourceLocation "TODO: LetExpression" ]

Expression.CaseExpression caseBlock ->
Err [ NotSupported sourceLocation "TODO: CaseExpression" ]

Expression.LambdaExpression lambda ->
Err [ NotSupported sourceLocation "TODO: LambdaExpression" ]

Expression.RecordExpr fieldNodes ->
fieldNodes
|> List.map Node.value
|> List.map
(\( Node _ fieldName, fieldValue ) ->
mapExpression sourceFile fieldValue
|> Result.map (Tuple.pair (fieldName |> Name.fromString))
)
|> ResultList.toResult
|> Result.mapError List.concat
|> Result.map (Value.Record sourceLocation)

Expression.ListExpr itemNodes ->
itemNodes
|> List.map (mapExpression sourceFile)
|> ResultList.toResult
|> Result.mapError List.concat
|> Result.map (Value.List sourceLocation)

Expression.RecordAccess targetNode fieldNameNode ->
mapExpression sourceFile targetNode
|> Result.map
(\subjectValue ->
Value.Field sourceLocation subjectValue (fieldNameNode |> Node.value |> Name.fromString)
)

Expression.RecordAccessFunction fieldName ->
Ok (Value.FieldFunction sourceLocation (fieldName |> Name.fromString))

Expression.RecordUpdateExpression targetVarNameNode fieldNodes ->
fieldNodes
|> List.map Node.value
|> List.map
(\( Node _ fieldName, fieldValue ) ->
mapExpression sourceFile fieldValue
|> Result.map (Tuple.pair (fieldName |> Name.fromString))
)
|> ResultList.toResult
|> Result.mapError List.concat
|> Result.map
(Value.UpdateRecord sourceLocation (targetVarNameNode |> Node.value |> Name.fromString |> Value.Variable sourceLocation))

Expression.GLSLExpression _ ->
Err [ NotSupported sourceLocation "GLSLExpression" ]


mapPattern : SourceFile -> Node Pattern -> Result Errors (Value.Pattern SourceLocation)
Expand Down
32 changes: 31 additions & 1 deletion tests/Morphir/Elm/FrontendTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,29 @@ valueTests =
\_ ->
Frontend.packageDefinitionFromSource packageInfo [ moduleSource valueSource ]
|> Result.map Package.eraseDefinitionAttributes
|> Result.mapError (\error -> "Error while reading model")
|> Result.mapError
(\errors ->
errors
|> List.map
(\error ->
case error of
Frontend.ParseError _ _ ->
"Parse Error"

Frontend.CyclicModules _ ->
"Cyclic Modules"

Frontend.ResolveError _ _ ->
"Resolve Error"

Frontend.EmptyApply _ ->
"Empty Apply"

Frontend.NotSupported _ expType ->
"Not Supported: " ++ expType
)
|> String.join ", "
)
|> Result.andThen
(\packageDef ->
packageDef.modules
Expand Down Expand Up @@ -227,6 +249,14 @@ valueTests =
, checkIR "foo bar" <| Apply () (ref "foo") (ref "bar")
, checkIR "foo bar baz" <| Apply () (Apply () (ref "foo") (ref "bar")) (ref "baz")
, checkIR "-1" <| Number.negate () () (Literal () (IntLiteral 1))
, checkIR "if foo then bar else baz" <| IfThenElse () (ref "foo") (ref "bar") (ref "baz")
, checkIR "( foo, bar, baz )" <| Tuple () [ ref "foo", ref "bar", ref "baz" ]
, checkIR "( foo )" <| ref "foo"
, checkIR "[ foo, bar, baz ]" <| List () [ ref "foo", ref "bar", ref "baz" ]
, checkIR "{ foo = foo, bar = bar, baz = baz }" <| Record () [ ( [ "foo" ], ref "foo" ), ( [ "bar" ], ref "bar" ), ( [ "baz" ], ref "baz" ) ]
, checkIR "foo.bar" <| Field () (ref "foo") [ "bar" ]
, checkIR ".bar" <| FieldFunction () [ "bar" ]
, checkIR "{ a | foo = foo, bar = bar }" <| UpdateRecord () (Variable () [ "a" ]) [ ( [ "foo" ], ref "foo" ), ( [ "bar" ], ref "bar" ) ]
]


Expand Down

0 comments on commit 9ce6d29

Please sign in to comment.