diff options
author | Marvin Borner | 2024-04-15 17:12:46 +0200 |
---|---|---|
committer | Marvin Borner | 2024-04-15 17:13:13 +0200 |
commit | bfdd0ef6897e42e03cefa4d43c8757ef09d96de2 (patch) | |
tree | 253509bc1a1ed34afc4a128952401921197fae7a /src/Parser.hs | |
parent | 32f0891053cb9efe7b2987638a96395d2b9f9722 (diff) |
Added parsing and printing of complex numbers
Diffstat (limited to 'src/Parser.hs')
-rw-r--r-- | src/Parser.hs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index a63b4a2..a24db6f 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -149,13 +149,12 @@ parseFloat :: Parser Expression parseFloat = do _ <- string "(" <?> "float start" num <- signedFloat <?> "signed float" - base <- try (oneOf "frc") <|> return 'f' + base <- try (oneOf "fr") <|> return 'f' _ <- string ")" <?> "float end" pure $ f base num where f 'f' = floatToRational f 'r' = floatToReal - f 'c' = floatToComplex -- TODO: imaginary f _ = invalidProgramState sign :: Parser (Rational -> Rational) sign = (char '-' >> return negate) <|> (char '+' >> return id) @@ -168,6 +167,26 @@ parseFloat = do signedFloat :: Parser Rational signedFloat = ap sign float +parseComplex :: Parser Expression +parseComplex = do + _ <- string "(" <?> "complex start" + real <- signedFloat <?> "signed complex" + _ <- char 'i' + imaginary <- signedFloat <?> "signed complex" + _ <- string ")" <?> "complex end" + pure $ floatToComplex real imaginary + where + sign :: Parser (Rational -> Rational) + sign = (char '-' >> return negate) <|> (char '+' >> return id) + float :: Parser Rational + float = do + a <- read <$> some digitChar <?> "digits" + _ <- char '.' <?> "float delimiter" + b <- read <$> some digitChar <?> "digits" + return $ convertToRational a b + signedFloat :: Parser Rational + signedFloat = ap sign float + specialEscape :: Parser Char specialEscape = choice (zipWith (\c r -> r <$ char c) "bnfrt\\\"/" "\b\n\f\r\t\\\"/") @@ -233,6 +252,7 @@ parseSingleton :: Parser Expression parseSingleton = let parseSingletonExpression = parseBruijn + <|> try parseComplex <|> try parseNumeral <|> try parseFloat <|> parseString |