Export Command to separate module
This commit is contained in:
@@ -27,6 +27,7 @@ executable MVM
|
||||
other-modules:
|
||||
VirtualMachine
|
||||
Instruction
|
||||
Command
|
||||
Parser
|
||||
Util
|
||||
|
||||
|
||||
9
app/Command.hs
Normal file
9
app/Command.hs
Normal file
@@ -0,0 +1,9 @@
|
||||
module Command (
|
||||
Command(..)
|
||||
) where
|
||||
|
||||
import qualified Instruction as I
|
||||
|
||||
data Command = Command { instr :: I.Instruction
|
||||
, args :: [Int]
|
||||
} deriving (Eq, Show)
|
||||
@@ -1,7 +1,6 @@
|
||||
module Instruction (
|
||||
Op(..),
|
||||
Instruction(..),
|
||||
Command(..),
|
||||
instructions,
|
||||
instructionByOp,
|
||||
toOp
|
||||
@@ -43,10 +42,6 @@ data Instruction = Simple { op :: Op
|
||||
}
|
||||
deriving (Eq, Show)
|
||||
|
||||
data Command = Command { instr :: Instruction
|
||||
, args :: [Int]
|
||||
} deriving (Eq, Show)
|
||||
|
||||
instructions :: [Instruction]
|
||||
instructions = [ Simple { op = Nop, noParams = 0, noPops = 0 }
|
||||
, Simple { op = Halt, noParams = 0, noPops = 0 }
|
||||
|
||||
@@ -6,23 +6,24 @@ import Data.Word
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.Map as Map
|
||||
|
||||
import qualified Command as C
|
||||
import qualified Instruction as I
|
||||
import qualified Util as U
|
||||
|
||||
parse :: B.ByteString -> Either String [I.Command]
|
||||
parse :: B.ByteString -> Either String [C.Command]
|
||||
parse = parseCommands . B.unpack
|
||||
|
||||
parseCommands :: [Word8] -> Either String [I.Command]
|
||||
parseCommands :: [Word8] -> Either String [C.Command]
|
||||
parseCommands [] = Right []
|
||||
parseCommands code@(x:_) = case parseCommand code of
|
||||
Just (cmd, rest) -> parseCommands rest >>= (\r -> return $ cmd : r)
|
||||
Nothing -> Left $ "Unparseable byte: " ++ U.byteStr x ++ "\nIn following sequence:\n" ++ U.bytesStr 16 code
|
||||
|
||||
parseCommand :: [Word8] -> Maybe (I.Command, [Word8])
|
||||
parseCommand :: [Word8] -> Maybe (C.Command, [Word8])
|
||||
parseCommand [] = Nothing
|
||||
parseCommand (opByte:xs) = do
|
||||
let op = toEnum . fromIntegral $ opByte :: I.Op
|
||||
instruction <- Map.lookup op I.instructionByOp
|
||||
let noParams = I.noParams instruction
|
||||
let params = map fromIntegral $ take noParams xs :: [Int]
|
||||
return (I.Command instruction params, drop noParams xs)
|
||||
return (C.Command instruction params, drop noParams xs)
|
||||
Reference in New Issue
Block a user