hatt 0.3 → 1.0
raw patch · 3 files changed
+54/−27 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- README.md +8/−0
- hatt.cabal +1/−1
- src/hatt.hs +45/−26
README.md view
@@ -51,6 +51,11 @@ help Print this help text. + pretty+ Pretty-print expressions using Unicode logic symbols. Only employ this+ option if your console is Unicode-aware. If pretty-printing is already+ enabled, using this command will disable it.+ exit Quit the program. @@ -113,6 +118,9 @@ F T F | F F F T | T F F F | F++You can enable pretty-printing while in interactive mode by using the `pretty`+command. Using Hatt in other programs
hatt.cabal view
@@ -1,5 +1,5 @@ Name: hatt-Version: 0.3+Version: 1.0 Stability: experimental Synopsis: A truth table generator for classical propositional logic.
src/hatt.hs view
@@ -10,32 +10,33 @@ import System.Console.CmdArgs import System.IO -data HattOpts = HattOpts- { evaluate :: String- , interactive :: Bool- , pretty :: Bool- } deriving (Show, Data, Typeable)- data Command = Exit | Help+ | Pretty | Eval Expr | Error String -hattOpts :: HattOpts-hattOpts = HattOpts+data ProgramMode = ProgramMode+ { evaluate :: String+ , interactive :: Bool+ , pretty :: Bool+ } deriving (Show, Data, Typeable)++programMode :: ProgramMode+programMode = ProgramMode { evaluate = "" &= typ "EXPRESSION" &= help "Print the truth table for the given expression" , interactive = False &= help "Enter interactive mode" , pretty = False &= help "Use Unicode logic symbols"- } &= summary "Hatt 0.3, (c) Benedict Eastaugh 2011"+ } &= summary "Hatt 1.0, (c) Benedict Eastaugh 2011" &= program "hatt" main :: IO ()-main = do opts <- cmdArgs hattOpts+main = do opts <- cmdArgs programMode let expStr = evaluate opts interMode = interactive opts evalMode = (not . null) expStr- printer = if pretty opts then show else showAscii+ printer = selectPrinter opts -- If the --evaluate flag is passed with an expression, print the -- truth table for that expression.@@ -48,17 +49,26 @@ -- program; otherwise, enter interactive mode. if evalMode && not interMode then return ()- else putStrLn replIntroText >> repl printer+ else putStrLn replIntroText >> repl opts -repl :: (Expr -> String) -> IO ()-repl p = do putStr "> "- hFlush stdout- cmd <- getLine- case parseCommand cmd of- Exit -> return ()- Help -> putStr replHelpText >> repl p- (Eval expr) -> putStr (truthTableP p expr) >> repl p- (Error err) -> putStrLn ("Error: " ++ err) >> repl p+repl :: ProgramMode -> IO ()+repl mode = do putStr "> "+ hFlush stdout+ cmd <- getLine+ case parseCommand cmd of+ Exit -> return ()+ Help -> putStr replHelpText+ >> repl mode+ Pretty -> putStrLn ppMessage+ >> repl (mode {pretty = not isPretty})+ (Eval expr) -> putStr (truthTableP printer expr)+ >> repl mode+ (Error err) -> putStrLn ("Error: " ++ err)+ >> repl mode+ where+ printer = selectPrinter mode+ isPretty = pretty mode+ ppMessage = (if isPretty then "Dis" else "En") ++ "abling pretty-printing." eval :: (Expr -> String) -> String -> String eval p str = case parseExpr "" str of@@ -67,10 +77,11 @@ parseCommand :: String -> Command parseCommand input = case cmd . words . dropWhile isSpace $ input of- "" -> Error "you must enter an expression or a command."- "exit" -> Exit- "help" -> Help- _ -> eval_ input+ "" -> Error "you must enter an expression or a command."+ "exit" -> Exit+ "help" -> Help+ "pretty" -> Pretty+ _ -> eval_ input where cmd [] = "" cmd ws = map toLower . head $ ws@@ -86,11 +97,16 @@ replHelpText :: String replHelpText = unlines- [ "Hatt's interactive mode has a couple of commands."+ [ "Hatt's interactive mode has several commands." , "" , "help" , " Print this help text." , ""+ , "pretty"+ , " Pretty-print expressions using Unicode logic symbols. Only employ this"+ , " option if your console is Unicode-aware. If pretty-printing is already"+ , " enabled, using this command will disable it."+ , "" , "exit" , " Quit the program." , ""@@ -115,3 +131,6 @@ , "" , "If none of this makes any sense, try reading the README file." ]++selectPrinter :: ProgramMode -> (Expr -> String)+selectPrinter m = if pretty m then show else showAscii