From c16cfb33a091df12667b8a86fc58033569e5e598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Tue, 2 Nov 2021 16:17:13 +0100 Subject: [PATCH] Model instructions and op codes --- MVM.cabal | 1 + app/Instruction.hs | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/Instruction.hs diff --git a/MVM.cabal b/MVM.cabal index 84599cd..6e94d69 100644 --- a/MVM.cabal +++ b/MVM.cabal @@ -26,6 +26,7 @@ executable MVM -- Modules included in this executable, other than Main. other-modules: VirtualMachine + Instruction -- LANGUAGE extensions used by modules in this package. -- other-extensions: diff --git a/app/Instruction.hs b/app/Instruction.hs new file mode 100644 index 0000000..0b36ada --- /dev/null +++ b/app/Instruction.hs @@ -0,0 +1,65 @@ +module Instruction ( + Op(..), + Instruction(..), + Command(..), + instructions, + instructionByOp, + toOp +) where + +import qualified Data.Char as Char +import qualified Data.Map as Map +import qualified VirtualMachine as VM + +data Op = Nop -- 0x00 + | Halt -- 0x01 + | Push -- 0x02 + | Pop -- 0x03 + | Dup -- 0x04 + | Swap -- 0x05 + | Add -- 0x06 + | Sub -- 0x07 + | Mul -- 0x08 + | Div -- 0x09 + | Neg -- 0x0a + | Not -- 0x0b + | Call -- 0x0c + | Ret -- 0x0d + | Jmp -- 0x0e + | Je -- 0x0f + | Jne -- 0x10 + | Jg -- 0x11 + | Jl -- 0x12 + | Jge -- 0x13 + | Jle -- 0x14 + | Ld -- 0x15 + deriving (Eq, Ord, Enum, Show, Read, Bounded) + +data Instruction = Simple { op :: Op + , noParams :: Int + , noPops :: Int } + | Complex { op :: Op + , vm :: VM.VM + } + 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 } + , Simple { op = Push, noParams = 1, noPops = 0 } + , Simple { op = Pop, noParams = 0, noPops = 1 } + ] + +instructionByOp :: Map.Map Op Instruction +instructionByOp = Map.fromList $ map (\i -> (op i, i)) instructions + + +toOp :: String -> Op +toOp = read . capitalize + where capitalize :: String -> String + capitalize [] = [] + capitalize (x:xs) = Char.toUpper x : map Char.toLower xs \ No newline at end of file