diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-16 13:11:37 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-16 13:11:37 +0300 |
commit | 7f59fd16534fc4fe417640130c415107008a638c (patch) | |
tree | b1dd2f6954f6590c3cd546c5548b26ab3ec1f105 /src/Utils.hs | |
parent | 97bbb430e4f1460858b2f1baaffc2ef804c63086 (diff) | |
download | suem-7f59fd16534fc4fe417640130c415107008a638c.tar suem-7f59fd16534fc4fe417640130c415107008a638c.tar.xz suem-7f59fd16534fc4fe417640130c415107008a638c.zip |
Now Machine is in Emulator which is a monad. =)
Diffstat (limited to 'src/Utils.hs')
-rw-r--r-- | src/Utils.hs | 44 |
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) |