Tidying, especially the parser
[advent-of-code-16.git] / adventofcode16 / app / advent23.hs
index 42cd15f2c24ca07570df285045b35368821776ed..dfb98cdab1e70e4c81fb3e360fa4fbb7a875e2f8 100644 (file)
@@ -160,32 +160,16 @@ writeValue m (Register r) v =
 
 
 instructionFile = instructionLine `sepEndBy` newline 
--- instructionLine = choice [cpyL, incL, decL, jnzL]
 instructionLine = incL <|> decL <|> cpyL <|> jnzL <|> tglL
 
--- incL = incify <$> (string "inc" *> spaces *> (oneOf "abcd"))
---         where incify r = Inc (Register r)
-incL = (Inc . Register) <$> (string "inc" *> spaces *> (oneOf "abcd"))
--- decL = decify <$> (string "dec" *> spaces *> (oneOf "abcd"))
---         where decify r = Dec (Register r)
-decL = (Dec . Register) <$> (string "dec" *> spaces *> (oneOf "abcd"))
-cpyL = cpyify <$> (string "cpy" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter))))
-              <*> (spaces *> (oneOf "abcd"))
-        where cpyify s r = Cpy s (Register r)
-jnzL = jnzify <$> (string "jnz" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter))))
-              <*> (spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter))))
-        where jnzify r o = Jnz r o
-tglL = Tgl <$> (string "tgl" *> spaces *> ((Literal <$> int) <|> ((Register . head) <$> (many1 letter)))) 
-        -- where tglify r = Tgl r
-
-
--- readLocation :: Int -> Location
--- readLocation l = Literal l
-
--- readLocation :: String -> Location
--- readLocation l
---     | all (isDigit) l = Literal (read l)
---     | otherwise = Register (head l)
+incL = Inc <$> (string "inc" *> spaces *> register)
+decL = Dec <$> (string "dec" *> spaces *> register)
+cpyL = Cpy <$> (string "cpy" *> spaces *> location) <*> (spaces *> register)
+jnzL = Jnz <$> (string "jnz" *> spaces *> location) <*> (spaces *> location)
+tglL = Tgl <$> (string "tgl" *> spaces *> location)
+
+location = (Literal <$> int) <|> register
+register = Register <$> (oneOf "abcd")
 
 parseIfile :: String -> Either ParseError [Instruction]
 parseIfile input = parse instructionFile "(unknown)" input