aboutsummaryrefslogtreecommitdiff
path: root/src/parser/expr.hs
blob: b84362d1b2c94d3aced5639a426e275fc99245a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Parser.Expr where

import Control.Applicative

import Parser.Core
import Atom
import Expr


imaginaryP :: Parser Atom
imaginaryP = AImaginary <$> (floatP <* char 'i')

rationalP :: Parser Atom
rationalP = ARational <$> floatP

termOpP :: Parser (Expr -> Expr -> Expr)
termOpP = infixOp "+" Add <|> infixOp "-" Sub

factorOpP :: Parser (Expr -> Expr -> Expr)
factorOpP = infixOp "*" Mul <|> infixOp "/" Div <|> infixOp "%" Mod

expOpP :: Parser (Expr -> Expr -> Expr)
expOpP = infixOp "^" Exp

exprP :: Parser Expr
exprP = termP `chainl1` termOpP

termP :: Parser Expr
termP =  factorP `chainl1` factorOpP

variableP :: Parser Expr
variableP = Variable <$> alphaStringP

functionP :: Parser Expr
functionP = Function <$> alphaStringP <*> parenthesisExprP

factorP :: Parser Expr
factorP =  endpointP `chainl1` expOpP
    where endpointP = parenthesisExprP <|> (EAtom <$> atomP) <|> functionP <|> variableP

parenthesisExprP :: Parser Expr
parenthesisExprP = parenthesize exprP

atomP :: Parser Atom
atomP = imaginaryP <|> rationalP