aboutsummaryrefslogtreecommitdiff
path: root/src/parser.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-12 16:19:55 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-13 12:30:49 +0100
commite0ade28ab642c043501493fe7192b626a6a68115 (patch)
tree7bfa065b5f80f6c245137244cb37d8691c02f488 /src/parser.hs
parent37e52bff39a1fe4d442cb253773252030c1cab8a (diff)
downloadcomputorv2-e0ade28ab642c043501493fe7192b626a6a68115.tar.gz
computorv2-e0ade28ab642c043501493fe7192b626a6a68115.tar.bz2
computorv2-e0ade28ab642c043501493fe7192b626a6a68115.zip
Caveman parsing working
(brain hurty)
Diffstat (limited to 'src/parser.hs')
-rw-r--r--src/parser.hs41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/parser.hs b/src/parser.hs
index f402b80..d708a6c 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -1,15 +1,12 @@
module Parser where
--- ( parse
--- , equationP
--- ) where
import Control.Applicative
import Control.Monad
import Data.Char
import Expr
--- import Equation
--- import Complex
+import Imag
+import Matrix
newtype Parser a = Parser (String -> Maybe (a, String))
@@ -58,11 +55,10 @@ satisfyChar f = Parser p
else Nothing
sepBy :: Parser b -> Parser a -> Parser [a]
-sepBy sep x = many (sep *> x)
+sepBy sep x = (:) <$> x <*> (many (sep *> x))
sepByMap :: (b -> a -> a) -> Parser b -> Parser a -> Parser [a]
-sepByMap f sep x = many (f <$> sep <*> x)
-
+sepByMap f sep x = (:) <$> x <*> (many (f <$> sep <*> x))
signed :: Num a => Parser a -> Parser a
signed p = do charP '-'
x <- p
@@ -101,15 +97,21 @@ unsignedFloatP = readParser p
floatP :: Parser Float
floatP = signed unsignedFloatP
--- imaginaryP :: Parser Imaginary
--- imaginaryP = floatP <* charP 'i'
+imagP :: Parser Imag
+imagP = Imag <$> (floatP <* charP 'i')
+
+matrixP :: Parser (Matrix AExpr)
+matrixP = Matrix <$> (charP '[' *> (sepBy (charP ';') matrixRowP) <* charP ']')
-exprP :: Parser Expr
-exprP = do x <- termP
- charP '+'
- y <- exprP
- return (Expr x y)
- <|> (ExprSingle <$> termP)
+matrixRowP :: Parser (MatrixRow AExpr)
+matrixRowP = charP '[' *> (sepBy (charP ',') aExprP) <* charP ']'
+
+aExprP :: Parser AExpr
+aExprP = do x <- termP
+ charP '+'
+ y <- aExprP
+ return (AExpr x y)
+ <|> (AExprSingle <$> termP)
termP :: Parser Term
termP = do f <- factorP
@@ -127,5 +129,8 @@ factorP = do b <- baseP
<|> (FactorSingle <$> baseP)
baseP :: Parser Base
-baseP = (charP '(' *> (Base <$> exprP) <* charP ')')
- <|> (BaseSingle <$> floatP)
+baseP = (charP '(' *> (Base <$> aExprP) <* charP ')')
+ <|> (BaseSingle <$> ExprI <$> imagP)
+ <|> (BaseSingle <$> ExprF <$> floatP)
+ <|> (BaseSingle <$> ExprM <$> matrixP)
+