aboutsummaryrefslogtreecommitdiff
path: root/src/parser/Expr.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-16 14:15:42 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-16 14:15:42 +0100
commit8c8f6155f1b05230c271059c52a503211aec872b (patch)
tree53626fa32ad3f6178b42a9dc5db588ee01277cd5 /src/parser/Expr.hs
parentd17423cba7c15a26f835a6fa578ecb48b80d8aab (diff)
downloadcomputorv2-8c8f6155f1b05230c271059c52a503211aec872b.tar.gz
computorv2-8c8f6155f1b05230c271059c52a503211aec872b.tar.bz2
computorv2-8c8f6155f1b05230c271059c52a503211aec872b.zip
file Renaming, basic REPL
Diffstat (limited to 'src/parser/Expr.hs')
-rw-r--r--src/parser/Expr.hs42
1 files changed, 42 insertions, 0 deletions
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