From 8c8f6155f1b05230c271059c52a503211aec872b Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 16 Mar 2020 14:15:42 +0100 Subject: file Renaming, basic REPL --- src/parser/Expr.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/parser/Expr.hs (limited to 'src/parser/Expr.hs') diff --git a/src/parser/Expr.hs b/src/parser/Expr.hs new file mode 100644 index 0000000..5ba4be7 --- /dev/null +++ b/src/parser/Expr.hs @@ -0,0 +1,42 @@ +module Parser.Expr where + +import Control.Applicative + +import Parser.Core +import Expr + + +imaginaryP :: Parser Atom +imaginaryP = Imaginary <$> (floatP <* char 'i') + +rationalP :: Parser Atom +rationalP = Rational <$> floatP + +matrixP :: Parser Atom +matrixP = Matrix <$> (char '[' *> sepBy (char ';') matrixRowP <* char ']') + where matrixRowP = char '[' *> sepBy (char ',') exprP <* char ']' + +exprP :: Parser Expr +exprP = termP `chainl1` termOpP + where termOpP = infixOp "+" Add <|> infixOp "-" Sub + +termP :: Parser Expr +termP = factorP `chainl1` factorOpP + where factorOpP = infixOp "**" Dot <|> infixOp "*" Mul <|> infixOp "/" Div <|> infixOp "%" Mod + +factorP :: Parser Expr +factorP = endpointP `chainl1` expOpP + where expOpP = infixOp "^" Exp + endpointP = parenthesisExprP <|> (EAtom <$> atomP) <|> functionP <|> variableP + +variableP :: Parser Expr +variableP = Variable <$> alphaStringP + +functionP :: Parser Expr +functionP = Function <$> alphaStringP <*> parenthesisExprP + +parenthesisExprP :: Parser Expr +parenthesisExprP = parenthesize exprP + +atomP :: Parser Atom +atomP = imaginaryP <|> rationalP <|> matrixP -- cgit