From b6f4a21e6544ffb8b31951a8c2802f9b618d2f9f Mon Sep 17 00:00:00 2001 From: Kelong Cong Date: Wed, 13 Nov 2013 20:44:32 +0000 Subject: [PATCH] refactored some code, added makefile --- .gitignore | 1 + Enigma.hs | 43 +++++++++++++++++++++---------------------- main.hs => Main.hs | 4 ++++ Makefile | 4 ++++ tests.hs => Tests.hs | 12 ++++++------ 5 files changed, 36 insertions(+), 28 deletions(-) rename main.hs => Main.hs (72%) create mode 100644 Makefile rename tests.hs => Tests.hs (71%) diff --git a/.gitignore b/.gitignore index f5bd353..830780e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ dist cabal-dev main +Main *.o *.hi *.chi diff --git a/Enigma.hs b/Enigma.hs index fd8479f..eccf6de 100644 --- a/Enigma.hs +++ b/Enigma.hs @@ -17,14 +17,15 @@ type State = [Rot_Loc] data Direction = Fwd | Bwd deriving (Show, Eq) data Conf = Conf - { pb :: PB_Conf - , refl :: Ref_Conf - , rtype :: [Rot_Type] - , ring :: [Rot_Ring]} + { get_pb :: PB_Conf + , get_refl :: Ref_Conf + , get_type :: [Rot_Type] + , get_ring :: [Rot_Ring]} deriving (Show) -- helper functions +alphs :: [Char] alphs = ['A'..'Z'] rem26 :: Int -> Int @@ -64,7 +65,7 @@ rotate_rotor x conf state | otherwise = state where (part1,_:part2) = splitAt x state - pawl_loc = snd (rtype conf !! (x+1)) + pawl_loc = snd (get_type conf !! (x+1)) right_loc = state !! (x+1) current_loc = state !! x @@ -74,31 +75,29 @@ reflector conf c = plugboard Bwd conf c rotor :: Direction -> Rot_Type -> Rot_Ring -> Rot_Loc -> Char -> Char -rotor dir (rt,_) ring loc c -- where rt is rotor type - | dir == Fwd = - intToChar (rem26 $ (charToInt fwd_f) - iloc + iring) - | dir == Bwd = - intToChar (rem26 $ (charToInt bwd_f) - iloc + iring) - | otherwise = error "Unknown direction" +rotor dir (tpe,_) ring loc c = -- where tpe is rotor type + intToChar (rem26 $ (charToInt code) - iloc + iring) where ic = charToInt c iloc = charToInt loc iring = charToInt ring - fwd_f = rt!! rem26 (ic + iloc - iring) - bwd_f = alphs !! getIndex (intToChar (rem26 (ic + iloc - iring))) rt + code = if (dir == Fwd) -- for intermediate calculation + then tpe !! rem26 (ic + iloc - iring) + else alphs !! getIndex ((intToChar.rem26) $ ic + iloc - iring) tpe + enigma_char :: Conf -> State -> Char -> Char enigma_char conf state c = - ( plugboard Bwd (pb conf) - . rotor Bwd ((rtype conf) !! 2) ((ring conf) !! 2) (state !! 2) - . rotor Bwd ((rtype conf) !! 1) ((ring conf) !! 1) (state !! 1) - . rotor Bwd ((rtype conf) !! 0) ((ring conf) !! 0) (state !! 0) - . reflector (refl conf) - . rotor Fwd ((rtype conf) !! 0) ((ring conf) !! 0) (state !! 0) - . rotor Fwd ((rtype conf) !! 1) ((ring conf) !! 1) (state !! 1) - . rotor Fwd ((rtype conf) !! 2) ((ring conf) !! 2) (state !! 2) - . plugboard Fwd (pb conf) + ( plugboard Bwd (get_pb conf) + . rotor Bwd ((get_type conf) !! 2) ((get_ring conf) !! 2) (state !! 2) + . rotor Bwd ((get_type conf) !! 1) ((get_ring conf) !! 1) (state !! 1) + . rotor Bwd ((get_type conf) !! 0) ((get_ring conf) !! 0) (state !! 0) + . reflector (get_refl conf) + . rotor Fwd ((get_type conf) !! 0) ((get_ring conf) !! 0) (state !! 0) + . rotor Fwd ((get_type conf) !! 1) ((get_ring conf) !! 1) (state !! 1) + . rotor Fwd ((get_type conf) !! 2) ((get_ring conf) !! 2) (state !! 2) + . plugboard Fwd (get_pb conf) ) c diff --git a/main.hs b/Main.hs similarity index 72% rename from main.hs rename to Main.hs index b74d74f..92367f8 100644 --- a/main.hs +++ b/Main.hs @@ -1,11 +1,15 @@ import Enigma +myconf :: Conf myconf = Conf plugs ref_b [rtypeI,rtypeII,rtypeIII] ['A','A','A'] +mystate :: State mystate = ['A','A','A'] +run :: String -> String run = enigma myconf mystate +main :: IO () main = do msg <- getLine putStrLn (run msg) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..735edd2 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +all: + ghc -Wall Main.hs +clean: + rm -rf *.o *.hi Main diff --git a/tests.hs b/Tests.hs similarity index 71% rename from tests.hs rename to Tests.hs index 72860b6..a28b492 100644 --- a/tests.hs +++ b/Tests.hs @@ -27,19 +27,19 @@ rotor_test = ( -- detailed tests: encode A at rotor pos AAV using default settings ------------------------------------------------------------------------------- rotor2_fwd_test = - (rotor Fwd ((rtype aaa_conf) !! 2) ((ring aaa_conf) !! 2) 'V' 'A') + (rotor Fwd ((get_type aaa_conf) !! 2) ((get_ring aaa_conf) !! 2) 'V' 'A') == 'R' -- carried forward from rotor2_fwd_test rotor1_fwd_test = - (rotor Fwd ((rtype aaa_conf) !! 1) ((ring aaa_conf) !! 1) 'A' 'R') + (rotor Fwd ((get_type aaa_conf) !! 1) ((get_ring aaa_conf) !! 1) 'A' 'R') == 'G' -- carried forward from rotor1_fwd_test rotor0_fwd_test = - (rotor Fwd ((rtype aaa_conf) !! 0) ((ring aaa_conf) !! 0) 'A' 'G') + (rotor Fwd ((get_type aaa_conf) !! 0) ((get_ring aaa_conf) !! 0) 'A' 'G') == 'D' @@ -51,17 +51,17 @@ reflector_test = -- backwards now.. rotor0_bwd_test = - (rotor Bwd ((rtype aaa_conf) !! 0) ((ring aaa_conf) !! 0) 'A' 'H') + (rotor Bwd ((get_type aaa_conf) !! 0) ((get_ring aaa_conf) !! 0) 'A' 'H') == 'P' rotor1_bwd_test = - (rotor Bwd ((rtype aaa_conf) !! 1) ((ring aaa_conf) !! 1) 'A' 'P') + (rotor Bwd ((get_type aaa_conf) !! 1) ((get_ring aaa_conf) !! 1) 'A' 'P') == 'U' rotor2_bwd_test = - (rotor Bwd ((rtype aaa_conf) !! 2) ((ring aaa_conf) !! 2) 'V' 'U') + (rotor Bwd ((get_type aaa_conf) !! 2) ((get_ring aaa_conf) !! 2) 'V' 'U') == 'M'