git @ Cat's Eye Technologies ZOWIE / 0600b42
Exec-yoo-ting the machine. We are exec-yoo-ting the machine. Chris Pressey 6 months ago
3 changed file(s) with 46 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
2323 state' <- writeAddr state addr' value
2424 return state'
2525
26 run s = s
26 applyInstr :: State -> Instruction -> IO State
27 applyInstr state (Mov dest src) = do
28 value <- getValue state src
29 state' <- setValue state dest value
30 return state'
31
32 nth :: [a] -> Integer -> Maybe a
33 nth [] _ = Nothing
34 nth (x:xs) 0 = Just x
35 nth (x:xs) n = nth xs (n-1)
36
37 step :: State -> IO (Maybe State)
38 step state@State{ pc=pc, prog=prog } =
39 case nth prog pc of
40 Just instr -> do
41 state' <- applyInstr state instr
42 return $ Just state'{ pc=pc+1 }
43 Nothing ->
44 return Nothing
45
46 run :: State -> IO State
47 run state = do
48 result <- step state
49 case result of
50 Just state' ->
51 run state'
52 Nothing ->
53 return state
54
55 loadAndRun s =
56 let
57 prog = [Mov (Direct 0) (Immediate 0)] -- TODO parse s
58 state = initState prog
59 in
60 run state
2323 } deriving (Show, Ord, Eq)
2424
2525
26 initState :: [Instruction] -> State
27 initState prog =
28 State{
29 pc=0,
30 mem=Map.empty,
31 prog=prog,
32 saved=Nothing
33 }
34
2635 readMem mem addr = Map.findWithDefault 0 addr mem
2736 writeMem mem addr value = Map.insert addr value mem
28
2937
3038 beginTransaction :: State -> State
3139 beginTransaction state@State{} =
1111 case args of
1212 ["run", fileName] -> do
1313 text <- readFile fileName
14 putStrLn $ show $ reverse $ Machine.run text
14 result <- Machine.loadAndRun text
15 putStrLn $ show $ result
1516 return ()
1617 _ -> do
1718 abortWith "Usage: zowie run <carriage-program-text-filename>"