From 7f59fd16534fc4fe417640130c415107008a638c Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Tue, 16 Feb 2021 13:11:37 +0300 Subject: Now Machine is in Emulator which is a monad. =) --- src/Utils.hs | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'src/Utils.hs') 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) -- cgit v1.2.3