Where bindings work.

This commit is contained in:
Robbie Gleichman 2016-02-21 19:34:33 -08:00
parent ef9ad167cd
commit 133bb74e0a
2 changed files with 16 additions and 11 deletions

View File

@ -12,11 +12,11 @@ import Types(Icon(..), Drawing(..), EdgeEnd(..))
import Translate(translateString)
-- TODO Now --
-- Handle where bindings
-- Destructuring pattern binds
-- TODO Later --
-- Eliminate BranchIcon for the identity funciton "y x = x"
-- Use refactor evalLabmbda and evalMatch to use makeEdges
-- Refactor evalLabmbda and evalMatch to use makeEdges
-- otherwise Guard special case
-- Let lines connect to ports in multiple locations (eg. argument for Apply0Dia)
-- Add a small black border to lines to help distinguish line crossings.
@ -262,7 +262,7 @@ main3 = do
]
letTests = [
"y = x where x = f 3",
"y = x where x = f 3 y",
"y x1 = let {x2 = x1; x3 = x2; x4 = f x3} in x4",
"y x1 = let x2 = f x1 in x2 x1",
"y x = let x = 3 in x",

View File

@ -206,18 +206,19 @@ makeEdges (IconGraph icons edges c sinks bindings) = newGraph where
(Left newStr) -> Left (newStr, destPort)
Nothing -> Left orig
-- TODO: This should remove the sinks that have been matched and turned into edges.
evalLet :: EvalContext -> Binds -> Exp -> State IDState (IconGraph, Reference)
evalLet c bs e = do
evalGeneralLet :: (EvalContext -> State IDState (IconGraph, Reference)) -> EvalContext -> Binds -> State IDState (IconGraph, Reference)
evalGeneralLet expOrRhsEvaler c bs = do
(bindGraph, bindContext) <- evalBinds c bs
expVal <- evalExp bindContext e
expVal <- expOrRhsEvaler bindContext
let
(expGraph, expResult) = expVal
newGraph = deleteBindings . makeEdges $ expGraph <> bindGraph
(IconGraph _ _ _ _ bindings) = bindGraph
pure $ printSelf (newGraph, lookupReference bindings expResult)
evalLet :: EvalContext -> Binds -> Exp -> State IDState (IconGraph, Reference)
evalLet context binds e = evalGeneralLet (`evalExp` e) context binds
evalExp :: EvalContext -> Exp -> State IDState (IconGraph, Reference)
evalExp c x = case x of
Var n -> pure $ evalQName n c
@ -246,17 +247,21 @@ evalRhs :: Rhs -> EvalContext -> State IDState (IconGraph, Reference)
evalRhs (UnGuardedRhs e) c = evalExp c e
evalRhs (GuardedRhss rhss) c = fmap Right <$> evalGuardedRhss c rhss
-- Todo: incorporate the binds by using a generalize version of evalLet that takes
-- as argument either evalRhs or evalExp
evalPatBind :: EvalContext -> Decl -> State IDState IconGraph
evalPatBind c (PatBind _ pat rhs whereBinds) = helper <$> evalRhs rhs (patName : c)
evalPatBind c (PatBind _ pat rhs maybeWhereBinds) = helper <$> evaledRhs
where
patName = evalPattern pat
rhsContext = patName : c
evaledRhs = case maybeWhereBinds of
Nothing -> evalRhs rhs rhsContext
Just b -> evalGeneralLet (evalRhs rhs) rhsContext b
helper (rhsGraph, rhsRef) = makeEdges (gr <> rhsGraph)
where
bindings = [(patName, rhsRef)]
gr = IconGraph mempty mempty mempty mempty bindings
iconGraphToDrawing :: IconGraph -> Drawing
iconGraphToDrawing (IconGraph icons edges subDrawings _ _) = Drawing icons edges subDrawings