Improve code
This commit is contained in:
@@ -13,14 +13,13 @@ import qualified Data.ByteString as B
|
||||
|
||||
import VirtualMachine.VM (VM(..), Op, Computation, get, pop, pushS, forward, getPc, isHalted, isDebug)
|
||||
import VirtualMachine.Instruction (Instruction(..), Unit(..), instructionByOp)
|
||||
import Util (maybeToEither)
|
||||
|
||||
|
||||
parseInstr :: [Word8] -> Either String (Instruction, [Word8])
|
||||
parseInstr (opCode:rest) = do
|
||||
let op = toEnum . fromIntegral $ opCode :: Op
|
||||
instr <- case M.lookup op instructionByOp of
|
||||
(Just i) -> Right i
|
||||
Nothing -> Left "Unknown instruction"
|
||||
instr <- maybeToEither (M.lookup op instructionByOp) "Unknown instruction"
|
||||
let noParams = _noParams instr
|
||||
let params = map fromIntegral $ take noParams rest :: [Word8]
|
||||
unless (length params == noParams) (Left $ "Expected " ++ show noParams ++ " parameter(s), got " ++ show (length params) ++ " for operator '" ++ show op ++ "'")
|
||||
|
||||
@@ -10,6 +10,7 @@ import qualified Data.Sequence as S
|
||||
import qualified Control.Monad.State as ST (get, put)
|
||||
import Data.Functor ((<&>))
|
||||
import Control.Monad (unless)
|
||||
import Util (maybeToExcept)
|
||||
|
||||
|
||||
data VM = VM { _pc :: Int
|
||||
@@ -85,9 +86,7 @@ isDebug :: Computation Bool
|
||||
isDebug = get <&> _debug
|
||||
|
||||
stackAt :: Int -> String -> Computation Int
|
||||
stackAt index err = get >>= \vm -> case _stack vm S.!? index of
|
||||
(Just i) -> return i
|
||||
Nothing -> throwError err
|
||||
stackAt index err = get >>= \vm -> maybeToExcept (_stack vm S.!? index) err
|
||||
|
||||
frameAt :: Int -> (Int -> Int) -> String -> Computation Int
|
||||
frameAt index t name = do
|
||||
@@ -95,9 +94,7 @@ frameAt index t name = do
|
||||
fp <- getFp
|
||||
unless (fp > -1) (throwError "No active stack frame")
|
||||
stackSize <- getStackSize
|
||||
case _stack vm S.!? (stackSize - fp - 1 - t index) of
|
||||
(Just i) -> return i
|
||||
Nothing -> throwError $ "Cannot determine " ++ name ++ " - index " ++ show index ++ " out of frame bounds"
|
||||
maybeToExcept (_stack vm S.!? (stackSize - fp - 1 - t index)) $ "Cannot determine " ++ name ++ " - index " ++ show index ++ " out of frame bounds"
|
||||
|
||||
updateFrameAt :: Int -> Int -> Computation ()
|
||||
updateFrameAt index value = do
|
||||
|
||||
Reference in New Issue
Block a user