From 1f9c9ee4864d90ddf6d726f99b5ea179f225d612 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 7 May 2014 02:06:41 +1000 Subject: Exposed rule application order and version 2a parsing at command line --- src/Thue/Interpreter.hs | 9 ++++++--- src/thue.hs | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/Thue/Interpreter.hs b/src/Thue/Interpreter.hs index c3bdde2..2b0461d 100644 --- a/src/Thue/Interpreter.hs +++ b/src/Thue/Interpreter.hs @@ -1,4 +1,6 @@ module Thue.Interpreter ( + Choice(..), + thue, extractInfix, nextInRange @@ -18,14 +20,15 @@ data Choice = Random StdGen -thue :: ThueProgram -> IO ThueState -thue program = +thue :: ThueProgram -> Maybe Choice -> IO ThueState +thue program order = let rules = thueRules program state = thueInitialState program version = thueVersion program gen = mkStdGen 4 --chosen by fair dice roll, guaranteed to be random + choice = if (isJust order) then fromJust order else Random gen - in interpret version rules (Random gen) state + in interpret version rules choice state diff --git a/src/thue.hs b/src/thue.hs index 89afcc3..ac935a4 100644 --- a/src/thue.hs +++ b/src/thue.hs @@ -1,27 +1,51 @@ import System.Environment( getArgs ) +import System.Console.GetOpt import Control.Exception( ErrorCall(..), Handler(..), catches ) import Thue.Parser import Thue.Interpreter +data Flag = LeftInterpret | RightInterpret | RandomInterpret | Version1 | Version2a + deriving (Eq) + + + + +options :: [OptDescr Flag] +options = + [ Option ['l'] ["left"] (NoArg LeftInterpret) "Apply rules in a list from left to right" + , Option ['r'] ["right"] (NoArg RightInterpret) "Apply rules in a list from right to left" + , Option ['a'] ["random"] (NoArg RandomInterpret) "Apply rules randomly" + , Option ['1'] ["ver1"] (NoArg Version1) "Use version 1 parser" + , Option ['2'] ["ver2a"] (NoArg Version2a) "Use version 2a parser, with quoted strings and escaped characters" ] + + usageString :: String -usageString = "Usage: thue " +usageString = "Usage: thue [OPTION..] " program :: IO () program = do - args <- getArgs - fileContents <- if (length args /= 1) - then error usageString - else readFile (head args) - - case (parseThue fileContents) of - Left x -> putStrLn (show x) - Right x -> (thue x) >>= (putStrLn . show) + args <- getArgs + let (actions, nonOptions, errors) = getOpt Permute options args + + parse = if (Version2a `elem` actions) then parseThue2a else parseThue + + order = if (LeftInterpret `elem` actions) + then Just First + else if (RightInterpret `elem` actions) then Just Last else Nothing + + fileContents <- if (length nonOptions /= 1) + then error usageString + else readFile (head nonOptions) + + case (parse fileContents) of + Left x -> putStrLn (show x) + Right x -> (thue x order) >>= (putStrLn . show) -- cgit