Refactor evalRightSection and evalTuple to use applicative instead of monad.

This commit is contained in:
Robbie Gleichman 2016-12-31 22:32:57 -08:00
parent f94265e8f2
commit dc3b0c5875
2 changed files with 25 additions and 12 deletions

View File

@ -545,11 +545,17 @@ evalCase c e alts =
-- END evalCase
evalTuple :: EvalContext -> [Exp] -> State IDState (SyntaxGraph, NameAndPort)
evalTuple c exps = do
argVals <- mapM (evalExp c) exps
funVal <- makeBox $ nTupleString (length exps)
applyIconName <- getUniqueName
pure $ makeApplyGraph (length exps) ApplyNodeFlavor False applyIconName (grNamePortToGrRef funVal) argVals
evalTuple c exps =
let
numExps = length exps
in
makeApplyGraph numExps ApplyNodeFlavor False
<$>
getUniqueName
<*>
(grNamePortToGrRef <$> makeBox (nTupleString numExps))
<*>
mapM (evalExp c) exps
evalTupleSection :: EvalContext -> [Maybe Exp] -> State IDState (SyntaxGraph, NameAndPort)
evalTupleSection c mExps =
@ -573,13 +579,19 @@ evalLeftSection :: EvalContext -> Exp -> QOp -> State IDState GraphAndRef
evalLeftSection c e op = evalExp c $ App (qOpToExp op) e
evalRightSection :: EvalContext -> QOp -> Exp -> State IDState (SyntaxGraph, NameAndPort)
evalRightSection c op e = do
expVal <- evalExp c e
funVal <- evalExp c (qOpToExp op)
applyIconName <- getUniqueName
-- TODO: A better option would be for makeApplyGraph to take the list of expressions as Maybes.
neverUsedPort <- Left <$> getUniqueString "unusedArgument"
pure $ makeApplyGraph 2 ApplyNodeFlavor False applyIconName funVal [GraphAndRef mempty neverUsedPort, expVal]
evalRightSection c op e =
makeApplyGraph 2 ApplyNodeFlavor False
<$>
getUniqueName
<*>
evalExp c (qOpToExp op)
<*>
((\x y -> [x, y]) <$>
-- TODO: A better option would be for makeApplyGraph to take the list of expressions as Maybes.
fmap (GraphAndRef mempty . Left) (getUniqueString "unusedArgument")
<*>
evalExp c e
)
-- evalEnums is only used by evalExp
evalEnums :: EvalContext -> String -> [Exp] -> State IDState GraphAndRef

View File

@ -207,6 +207,7 @@ letTests = [
operatorTests :: [String]
operatorTests = [
-- right section
"y = map (++ 1) 3"
]