blob: 64f452af211523972d09915e3ee24d42f8330827 (
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
module Parser where
import Control.Applicative
import Control.Monad
import Data.Char
import Expr
import Imag
import Matrix
newtype Parser a = Parser (String -> Maybe (a, String))
parse :: Parser a -> String -> Maybe (a, String)
parse (Parser p) input = p input
instance Functor Parser where
-- fmap :: (a -> b) -> Parser a -> Parser b
fmap f (Parser p) = Parser new_p
where new_p s = do
(x, s') <- p s
return (f x, s')
instance Applicative Parser where
-- pure :: a -> Parser a
pure x = Parser (\s -> Just (x, s))
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
(Parser p1) <*> (Parser p2) = Parser new_p
where new_p s = do
(f, s') <- p1 s
(x, s'') <- p2 s'
return (f x, s'')
instance Alternative Parser where
-- empty :: Parser a
empty = Parser (\_ -> Nothing)
-- (<|>) :: Parser a -> Parser a -> Parser a
(Parser p1) <|> (Parser p2) = Parser new_p
where new_p s = p1 s <|> p2 s
instance Monad Parser where
-- return :: a -> Parser a
return x = pure x
-- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
(Parser p1) >>= f = Parser new_p
where new_p s = do
(x, s') <- p1 s
parse (f x) s'
satisfyChar :: (Char -> Bool) -> Parser Char
satisfyChar f = Parser p
where p [] = Nothing
p (c:cs) = if f c then Just (c, cs)
else Nothing
sepBy :: Parser b -> Parser a -> Parser [a]
sepBy sep x = (:) <$> x <*> (many (sep *> x))
sepByMap :: (b -> a -> a) -> Parser b -> Parser a -> Parser [a]
sepByMap f sep x = (:) <$> x <*> (many (f <$> sep <*> x))
signed :: Num a => Parser a -> Parser a
signed p = do charP '-'
x <- p
return (-x)
<|> p
readParser :: Read a => Parser String -> Parser a
readParser p = read <$> p
charP :: Char -> Parser Char
charP c = satisfyChar (c ==)
alphaP :: Parser Char
alphaP = satisfyChar isAlpha
digitsP :: Parser String
digitsP = some (satisfyChar isDigit) -- at least one digit to avoid read exception
spacesP :: Parser String
spacesP = many (satisfyChar isSpace)
unsignedIntP :: Parser Int
unsignedIntP = readParser digitsP
intP :: Parser Int
intP = signed unsignedIntP
unsignedFloatP :: Parser Float
unsignedFloatP = readParser p
where p = do pos <- digitsP
charP '.'
dec <- digitsP
return (pos ++ "." ++ dec)
<|> digitsP
floatP :: Parser Float
floatP = signed unsignedFloatP
imagP :: Parser Imag
imagP = Imag <$> (floatP <* charP 'i')
matrixP :: Parser (Matrix AExpr)
matrixP = Matrix <$> (charP '[' *> (sepBy (charP ';') matrixRowP) <* charP ']')
matrixRowP :: Parser (MatrixRow AExpr)
matrixRowP = charP '[' *> (sepBy (charP ',') aExprP) <* charP ']'
varP :: Parser Var
varP = some alphaP
funcExprP :: Parser FuncExpr
funcExprP = do name <- varP
charP '('
arg <- aExprP
charP ')'
return (FuncExpr name arg)
aExprP :: Parser AExpr
aExprP = do x <- termP
charP '+'
y <- aExprP
return (AExpr x y)
<|> (AExprSingle <$> termP)
termP :: Parser Term
termP = do f <- factorP
charP '*'
t <- termP
return (Term f t)
<|> (TermSingle <$> factorP)
factorP :: Parser Factor
factorP = do b <- baseP
charP '^'
e <- factorP
return (Factor b e)
<|> (FactorSingle <$> baseP)
baseP :: Parser Base
baseP = (charP '(' *> (Base <$> aExprP) <* charP ')')
<|> (BaseSingle <$> ExprI <$> imagP)
<|> (BaseSingle <$> ExprF <$> floatP)
<|> (BaseSingle <$> ExprM <$> matrixP)
<|> (BaseSingle <$> ExprFE <$> funcExprP)
<|> (BaseSingle <$> ExprV <$> varP)
funcDeclP :: Parser FuncDecl
funcDeclP = do name <- varP
charP '('
argName <- varP
charP ')'
return (FuncDecl name argName)
labelP :: Parser Label
labelP = varP <|> funcDeclP
evalP :: Parser Eval
evalP = do labelP
charP '='
(EvalDecl <$> exprP) <|> (EvalTry <$ charP '?')
|