, _ssContinue :: Bool
, _ssUnsaved :: Bool
, _ssState :: [ExecutionState]
+ , _ssTracing :: Bool
+ , _ssDumpFile :: String
} deriving (Ord, Eq)
makeLenses ''SynacorState
, "conts " ++ (show $ state ^. ssContinue)
, "unsaved " ++ (show $ state ^. ssUnsaved)
, "states " ++ (show $ state ^. ssState)
+ , "tracing " ++ (show $ state ^. ssTracing)
+ , "dumping to " ++ (state ^. ssDumpFile)
]
main :: IO ()
emptyState =
do mem <- getMemory
let machine = makeMachine mem
- return $ SynacorState [machine] [] [] True False [Runnable]
+ return $ SynacorState
+ { _ssMachines = [machine]
+ , _ssInputs = []
+ , _ssOutputs = []
+ , _ssContinue = True
+ , _ssUnsaved = False
+ , _ssState = [Runnable]
+ , _ssTracing = False
+ , _ssDumpFile = ""
+ }
+ -- [machine] [] [] True False [Runnable]
adventureHarness :: SynacorState -> IO SynacorState
adventureHarness state =
do command <- prompt "> "
state' <- handleCommand command state
- let newOutput = head $ state' ^. ssOutputs
+ let traceAndOutput = head $ state' ^. ssOutputs
+ let (newOutput, tracing) = spliceOut traceAndOutput
putStrLn newOutput
+ when (state' ^. ssTracing)
+ (appendFile (state' ^. ssDumpFile) $ unlines tracing)
if (state' ^. ssContinue)
then (adventureHarness state')
else return state'
+spliceOut :: String -> (String, [String])
+spliceOut s = doingOut s "" []
+ where doingOut s out traces
+ | null s = (reverse out, reverse traces)
+ | ">> " `isPrefixOf` s = doingTrace (drop 3 s) out traces ""
+ | otherwise = doingOut (tail s) (head s : out) traces
+ doingTrace s out traces tr
+ | "<<" `isPrefixOf` s = doingOut (drop 2 s) out (reverse tr : traces)
+ | otherwise = doingTrace (tail s) out traces (head s : tr)
+
+
handleCommand :: String -> SynacorState -> IO SynacorState
handleCommand ":quit" state = return $ state & ssContinue .~ False
handleCommand ":save" state =
machineInput <- readFile filename
let inputs = lines machineInput
initialState <- emptyState
- let nonComments = filter (\i -> head i == '#') inputs
+ let nonComments = filter (\i -> head i /= '#') inputs
let state = foldl' runOneInput initialState ("" : nonComments)
return $ state & ssUnsaved .~ False
handleCommand ":undo" state =
putStrLn o
)
return state
+handleCommand ":trace" state =
+ do filename <- prompt "Dump to? "
+ return $ state & ssTracing .~ True & ssDumpFile .~ filename
+handleCommand ":untrace" state =
+ return $ state & ssTracing .~ False & ssDumpFile .~ ""
+handleCommand ":poke8" state =
+ do let machines = state ^. ssMachines
+ let machine = head machines
+ let machine' = machine & registers . ix 7 .~ 1
+ return $ state & ssMachines .~ (machine' : (tail machines))
handleCommand command state = return $ runOneInput state command
& ssContinue .~ True
& ssUnsaved .~ True
& ssState %~ (exState :)
- where machine = head $ state ^. ssMachines
+ where machine0 = head $ state ^. ssMachines
+ machine = if (state ^. ssTracing)
+ then machine0 & tracing .~ True
+ else machine0 & tracing .~ False
inputW = wordify (input ++ "\n")
(exState, machine', output) = runMachine inputW (machine & inputIndex .~ 0)
-- output' = trace ("runone " ++ (show (machine == machine')) ++ " " ++ (show exState) ++ " " ++ (showOutput output)) output