-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
37 lines (31 loc) · 1.47 KB
/
Main.hs
File metadata and controls
37 lines (31 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import System.Environment
import Data.Char
import Machine (Rotors, Plugboard, Reflector, toReflector, toRotor, toPlugboard, encodeMessage)
parseRotors :: [String] -> IO Rotors
parseRotors ("-rotors" : [a, b, c] : _) = case (toRotor a, toRotor b, toRotor c) of
(Just a', Just b', Just c') -> pure (a', b', c')
_ -> fail $ "invalid rotors " ++ [a, b, c] ++ " specified in args"
parseRotors (_ : xs) = parseRotors xs
parseRotors [] = fail "rotors must be specified in args"
parsePlugboard :: [String] -> IO Plugboard
parsePlugboard ("-plugboard" : plugboard : _) = pure $ toPlugboard plugboard
parsePlugboard (_ : xs) = parsePlugboard xs
parsePlugboard [] = pure []
parseReflector :: [String] -> IO Reflector
parseReflector ("-reflector" : [reflector] : _) = case (toReflector . toUpper) reflector of
Just reflector' -> pure reflector'
Nothing -> fail $ "invalid reflector " ++ [reflector] ++ " specified in args"
parseReflector (_ : xs) = parseReflector xs
parseReflector [] = fail "reflector must be specified in args"
parseMessage :: [String] -> IO String
parseMessage ("-message" : message : _) = pure $ map toUpper message
parseMessage (_ : xs) = parseMessage xs
parseMessage [] = fail "message must be specified in args"
main :: IO ()
main = do
args <- System.Environment.getArgs
rotors <- parseRotors args
reflector <- parseReflector args
plugboard <- parsePlugboard args
message <- parseMessage args
print $ encodeMessage rotors reflector plugboard message