module Weave.Section2 where

import Weave.Shared

data Loc = At{curr :: DumbLang, ctx :: Ctx}
               deriving (Show, Eq)

data Ctx =
  Top
  | CondCtx1 Ctx DumbLang DumbLang
  | CondCtx2 DumbLang Ctx DumbLang
  | CondCtx3 DumbLang DumbLang Ctx
  | PlusCtx1 Ctx DumbLang
  | PlusCtx2 DumbLang Ctx
  deriving (Show,Eq)

down :: Loc -> Loc
down t@(At (I _) _) = t
down t@(At (B _) _) = t
down (At (Cond t1 t2 t3) c) = At t1 (CondCtx1 c t2 t3)
down (At (Plus t1 t2) c) = At t1 (PlusCtx1 c t2)

up :: Loc -> Loc
up t@(At _ Top) = t
up (At t1 (CondCtx1 c t2 t3)) = At (Cond t1 t2 t3) c
up (At t2 (CondCtx2 t1 c t3)) = At (Cond t1 t2 t3) c
up (At t3 (CondCtx3 t1 t2 c)) = At (Cond t1 t2 t3) c
up (At t1 (PlusCtx1 c t2)) = At (Plus t1 t2) c
up (At t2 (PlusCtx2 t1 c)) = At (Plus t1 t2) c

--If we've moved all the way left or right, we stay there.
--I wonder what would happen if we looped around instead.
--Doesn't make sense for up/down, but it does for left/right.
left :: Loc -> Loc
left t@(At _ Top) = t
left t@(At _ (CondCtx1 _ _ _)) = t
left (At t2 (CondCtx2 t1 c t3)) = At t1 (CondCtx1 c t2 t3)
left (At t3 (CondCtx3 t1 t2 c)) = At t2 (CondCtx2 t1 c t3)
left t@(At _ (PlusCtx1 _ _)) = t
left (At t2 (PlusCtx2 t1 c)) = At t1 (PlusCtx1 c t2)

right :: Loc -> Loc
right t@(At _ Top) = t
right (At t1 (CondCtx1 c t2 t3)) = At t2 (CondCtx2 t1 c t3)
right (At t2 (CondCtx2 t1 c t3)) = At t3 (CondCtx3 t1 t2 c)
right t@(At _ (CondCtx3 _ _ _)) = t
right (At t1 (PlusCtx1 c t2)) = At t2 (PlusCtx2 t1 c)
right t@(At _ (PlusCtx2 _ _)) = t

top :: DumbLang -> Loc
top t = At t Top

--When we're finished zipping, we do this.
--Need a better name.
reassemble :: Loc -> DumbLang
reassemble (At t Top) = t
reassemble t = reassemble $ up t
