Skip to content

Instantly share code, notes, and snippets.

@sshine
Created October 29, 2014 13:39
Show Gist options
  • Save sshine/9df86625b3b09fbe0798 to your computer and use it in GitHub Desktop.
Save sshine/9df86625b3b09fbe0798 to your computer and use it in GitHub Desktop.
--
-- Skeleton for Salsa interpreter
-- To be used at the exam for Advanced Programming, B1-2013
--
module SalsaInterp
-- (Position, interpolate, runProg)
where
import SalsaAst
import Gpx
--
-- The function interpolate
--
type Position = (Integer, Integer)
interpolate :: Integer -> Position -> Position -> [Position]
interpolate = undefined
--
-- Define the types Context and SalsaCommand
--
type View = (Ident, Int, Int)
data Context = Context { env :: Env
, state :: State }
deriving Show
data Env = Env { views :: [(Ident, View)]
, groups :: [(Ident, [Ident])]
, shapes :: [(Ident, Int)]
} deriving Show
data State = State { positions :: [(Int, Position)] }
deriving Show
initialContext = Context env st
where env = Env [] [] []
st = State []
newtype SalsaCommand a = SalsaCommand { runSC :: Context -> (a, Context) }
instance Monad SalsaCommand where
return x = SalsaCommand $ \ context -> (x, context)
(SalsaCommand ma) >>= f = SalsaCommand $ \ context ->
let (a, context') = ma context
SalsaCommand mb = f a
(b, context'') = mb context' { env = env context }
in (b, context'' { env = env context })
-- functions for manipulating the context
move :: Int -> Position -> SalsaCommand ()
move i pos = SalsaCommand $ \c -> ((), let c' = c { state = s' }
s' = (state c) { positions = (i, pos) : positions (state c) }
in c')
moveTwice i pos = do
messWithEnv
move i pos
move i pos
messWithEnv :: SalsaCommand ()
messWithEnv = SalsaCommand $ \c -> ((), c { env = Env [] [] [("foo", 1)] })
local :: Context -> SalsaCommand a -> SalsaCommand ()
local con cmd = SalsaCommand $ \_ -> ((), con) ...
-- getEnv :: SalsaCommand Env
-- getEnv = SalsaCommand $ \ context -> (env context, context)
-- getState = SalsaCommand $ \ context -> (state context, context)
-- getShapes = do
-- env <- getEnv
-- return (shapes env)
-- getActiveViews = do
--
-- Define the function command
--
-- command :: Command -> SalsaCommand ()
-- command (Move ids pos) = do
-- env <- getEnv
-- let sh = shapes env
--
-- Define the type Salsa
--
-- data Salsa a = ...
--
-- Define the functions liftC, definition, and defCom
--
-- liftC :: SalsaCommand a -> Salsa a
-- definition :: Definition -> Salsa ()
-- definition (Def def) = ...
-- definition (Com cmd) = command cmd
defCom :: DefCom -> Salsa ()
defCom (Def def) =
program :: Program -> Salsa ()
program dcs = mapM_ defCom dcs
--
-- Define the function runProg
--
-- runProg :: Integer -> Program -> Animation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment