From 07812f903e0d09b4bb8c2ff9ac4310eb0d3ab8ee Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Mon, 14 Apr 2014 03:36:36 +1000 Subject: Everything done for v2a except the actual character parsing --- src/Thue/Interpreter.hs | 7 ++----- src/Thue/Parser.hs | 55 ++++++++++++++++++++++++++++--------------------- src/Thue/Test.hs | 4 ++-- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/Thue/Interpreter.hs b/src/Thue/Interpreter.hs index 3e82826..c9758a0 100644 --- a/src/Thue/Interpreter.hs +++ b/src/Thue/Interpreter.hs @@ -12,10 +12,6 @@ import Thue.Parser -data ThueVersion = Ver1 | Ver2a - deriving (Eq) - - data Choice = Random StdGen | First | Last @@ -26,9 +22,10 @@ thue :: ThueProgram -> IO ThueState thue program = let rules = thueRules program state = thueInitialState program + version = thueVersion program gen = mkStdGen 4 --chosen by fair dice roll, guaranteed to be random - in interpret Ver1 rules (Random gen) state + in interpret version rules (Random gen) state diff --git a/src/Thue/Parser.hs b/src/Thue/Parser.hs index a9c2d96..9d5f30b 100644 --- a/src/Thue/Parser.hs +++ b/src/Thue/Parser.hs @@ -4,7 +4,10 @@ module Thue.Parser ( ThueState, ThueChar(..), + ThueVersion(..), + parseThue, + parseThue2a, tCh, tLit, @@ -20,7 +23,8 @@ import Text.ParserCombinators.Parsec data ThueProgram = ThueProgram { thueRules :: [ThueRule] - , thueInitialState :: ThueState } + , thueInitialState :: ThueState + , thueVersion :: ThueVersion } deriving (Show, Eq) @@ -37,15 +41,19 @@ data ThueChar = TChar { tChar :: Char } deriving (Show, Eq) +data ThueVersion = Ver1 | Ver2a + deriving (Show, Eq) + + parseThue :: String -> Either ParseError ThueProgram -parseThue = parse thue "error" +parseThue = parse (thue Ver1) "error" ---parseThue2a :: String -> Either ParseError Thue2aProgram ---parseThue2a = parse thue2a "error" +parseThue2a :: String -> Either ParseError ThueProgram +parseThue2a = parse (thue Ver2a) "error" @@ -67,48 +75,47 @@ fromThueState = map tChar -thue = do - rs <- many rule +thue ver = do + rs <- many (rule ver) separatorLine - i <- initialState + i <- initialState ver eof - return (ThueProgram rs i) + return (ThueProgram rs i ver) -rule = do - o <- ruleState +rule ver = do + o <- ruleState ver separator - r <- state + r <- state ver eol return (ThueRule o r) -separatorLine = whiteSpace >> separator >> whiteSpace >> eol +separatorLine = separator >> eol separator = string "::=" "rule separator" -initialState = do - s <- state `sepEndBy` eol +initialState ver = do + s <- (state ver) `sepEndBy` eol return (concat s) -ruleState = some ruleStateChar - - -ruleStateChar = (noneOf "\n\r:" >>= return . TChar) - <|> try (char ':' >> notFollowedBy (string ":=") >> return (TChar ':')) - "state character" +ruleState ver = some (ruleStateChar ver) -state = many stateChar +ruleStateChar ver = + (noneOf "\n\r:" >>= return . TChar) + <|> try (char ':' >> notFollowedBy (string ":=") >> return (TChar ':')) + "state character" -stateChar = (noneOf "\n\r" >>= return . TChar) - "state character" +state ver = many (stateChar ver) -whiteSpace = many (oneOf "\t ") +stateChar ver = + (noneOf "\n\r" >>= return . TChar) + "state character" eol = try (string "\r\n") diff --git a/src/Thue/Test.hs b/src/Thue/Test.hs index fcd8cde..be4e38b 100644 --- a/src/Thue/Test.hs +++ b/src/Thue/Test.hs @@ -16,9 +16,9 @@ instance Eq Text.Parsec.Error.ParseError -parser0 = (Right (ThueProgram [ThueRule (tStr "a") (tStr "b")] (tStr "a")) ) ~=? (parseThue "a::=b\n::=\na") +parser0 = (Right (ThueProgram [ThueRule (tStr "a") (tStr "b")] (tStr "a") Ver1) ) ~=? (parseThue "a::=b\n::=\na") -parser1 = (Right (ThueProgram [] (tStr "b")) ) ~=? (parseThue "::=\nb") +parser1 = (Right (ThueProgram [] (tStr "b") Ver1) ) ~=? (parseThue "::=\nb") -- cgit