-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvolutionParser.hs
More file actions
65 lines (51 loc) · 1.9 KB
/
Copy pathEvolutionParser.hs
File metadata and controls
65 lines (51 loc) · 1.9 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module EvolutionParser (module EvolutionParser) where
import Data.Char
import Control.Monad
import Data.Map
import Data.List
import Text.ParserCombinators.Parsec
import Data.Either
import Data.Maybe
import EvolutionTypes
-- this file contains logic responsible for parsing the content of an input file
type Input = [[(String, String)]] -- list of list of string pairs, each list correspond to one solution, expects definitions of variables and "res"
ident :: Parser String
ident = do c <- letter <|> char '_'
cs <- many (letter <|> digit <|> char '_')
return (c:cs)
<?> "identifier"
eos :: Parser ()
eos = do oneOf " ,"
return ()
<?> "end of statement"
eol :: Parser ()
eol = do oneOf "\r\n"
return ()
<?> "end of line"
emptyChars = oneOf " \t"
item :: Parser (String, String)
item = do skipMany emptyChars
key <- ident
skipMany emptyChars
char '='
skipMany emptyChars
value <- manyTill anyChar (lookAhead $ eol <|> eos <|> eof)
skipMany emptyChars
skipMany (char ',')
return (key, value)
line :: Parser [(String, String)]
line = manyTill item (eol <|> lookAhead eof)
file :: Parser [[(String, String)]]
file = manyTill line eof
getSolutions :: IO [Solution]
getSolutions = do
inputEithers <- readInput "input.txt"
solutions <- case inputEithers of
Left err -> return [(Solution (fromList []) 0)]
Right input -> return $ Data.List.map toSolution input
return solutions
readInput :: String -> IO (Either ParseError Input)
readInput = parseFromFile file
-- takes the string inputs and transforms them into the solution data type for use in program
toSolution i = Solution (Data.Map.delete "res" mapped) (mapped ! "res")
where mapped = fromList $ Data.List.map (\(a,b) -> (a, read b :: Double)) i