aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Utils.hs')
-rw-r--r--src/Utils.hs44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/Utils.hs b/src/Utils.hs
index 345b085..475821a 100644
--- a/src/Utils.hs
+++ b/src/Utils.hs
@@ -1,32 +1,46 @@
+-- This module describes utility functions.
module Utils where
-import Data.Word
+import Prelude hiding (Word)
import Data.Bits
-import Machine
-boolToInt :: Bool -> Int
-boolToInt True = 1
-boolToInt False = 0
-toBits :: Word16 -> [Int]
-toBits x = map (boolToInt . testBit x) [0..(finiteBitSize x-1)]
+-------------------------------------------------------------------------------
+-- Bitwork
+
+toBit :: Bool -> Int
+toBit True = 1
+toBit False = 0
+
+toBits :: Bits a => a -> [Int] -> [Int]
+toBits x r = map (toBit . testBit x) r
+
+toBitsWhole :: FiniteBits a => a -> [Int]
+toBitsWhole x = toBits x [0..(finiteBitSize x - 1)]
fromBits :: [Int] -> Int
fromBits = foldl (\a b -> 2 * a + b) 0 . reverse
-args2 :: (Int -> Int -> Machine -> Machine) ->
- [Int] -> [Int] -> Machine -> Machine
+extractBits :: Bits a => a -> [Int] -> Int
+extractBits x r = fromBits $ toBits x r
+
+
+-------------------------------------------------------------------------------
+-- Transformers for commands arguments
+
+args2 :: (Int -> Int -> t) ->
+ [Int] -> [Int] -> t
args2 f a b = f (fromBits a) (fromBits b)
-args3 :: (Int -> Int -> Int -> Machine -> Machine) ->
- [Int] -> [Int] -> [Int] -> Machine -> Machine
+args3 :: (Int -> Int -> Int -> t) ->
+ [Int] -> [Int] -> [Int] -> t
args3 f a b c = f (fromBits a) (fromBits b) (fromBits c)
-args4 :: (Int -> Int -> Int -> Int -> Machine -> Machine) ->
- [Int] -> [Int] -> [Int] -> [Int] -> Machine -> Machine
+args4 :: (Int -> Int -> Int -> Int -> t) ->
+ [Int] -> [Int] -> [Int] -> [Int] -> t
args4 f a b c d = f (fromBits a) (fromBits b) (fromBits c) (fromBits d)
-args5 :: (Int -> Int -> Int -> Int -> Int -> Machine -> Machine) ->
- [Int] -> [Int] -> [Int] -> [Int] -> [Int] -> Machine -> Machine
+args5 :: (Int -> Int -> Int -> Int -> Int -> t) ->
+ [Int] -> [Int] -> [Int] -> [Int] -> [Int] -> t
args5 f a b c d e = f (fromBits a) (fromBits b) (fromBits c)
(fromBits d) (fromBits e)