-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathch13_00_sandbox.hs
257 lines (193 loc) · 5.78 KB
/
ch13_00_sandbox.hs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
module Chapter_13_Sandbox where
import Control.Applicative
import Data.Char
import Data.List
import System.IO
newtype Parser a = P (String -> [(a, String)])
parse :: Parser a -> String -> [(a, String)]
parse (P p) inp = p inp
item :: Parser Char
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x, xs)])
instance Functor Parser where
-- fmap :: (a -> b) -> Parser a -> Parser b
fmap g p = P (\inp -> case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
instance Applicative Parser where
-- pure :: a -> Parser a
pure v = P (\inp -> [(v, inp)])
-- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
pg <*> px = P (\inp -> case parse pg inp of
[] -> []
[(g,out)] -> parse (fmap g px) out)
three :: Parser (Char, Char)
three = do x <- item
item
z <- item
return (x,z)
instance Monad Parser where
-- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = P (\inp -> case parse p inp of
[] -> []
[(v,out)] -> parse (f v) out)
instance Alternative Parser where
-- empty :: Parser a
empty = P (\inp -> [])
-- (<|>) :: Parser a -> Parser a -> Parser a
p <|> q = P (\inp -> case parse p inp of
[] -> parse q inp
[(v,out)] -> [(v,out)])
-- 13.6 Derived primitives
sat :: (Char -> Bool) -> Parser Char
sat p = do x <- item
if p x then return x else empty
digit :: Parser Char
digit = sat isDigit
lower :: Parser Char
lower = sat isLower
upper :: Parser Char
upper = sat isUpper
letter :: Parser Char
letter = sat isAlpha
alphanum :: Parser Char
alphanum = sat isAlphaNum
char :: Char -> Parser Char
char x = sat (== x)
string :: String -> Parser String
string [] = return []
string (x:xs) = do char x
string xs
return (x:xs)
ident :: Parser String
ident = do x <- lower
xs <- many alphanum
return (x:xs)
nat :: Parser Int
nat = do xs <- some digit
return (read xs)
space :: Parser ()
space = do many (sat isSpace)
return ()
int :: Parser Int
int = do char '-'
n <- nat
return (-n)
<|> nat
token :: Parser a -> Parser a
token p = do space
v <- p
space
return v
-- Using token , we can now define parsers that ignore spacing around identifiers, natural
-- numbers, integers and special symbols:
identifier :: Parser String
identifier = token ident
natural :: Parser Int
natural = token nat
integer :: Parser Int
integer = token int
symbol :: String -> Parser String
symbol xs = token (string xs)
-- For example, using these primitives a parser for a non-empty list of natural numbers that
-- ignores spacing around tokens can be defined as follows:
nats :: Parser [Int]
nats = do symbol "["
n <- natural
ns <- many (do symbol ","; natural)
symbol "]"
return (n:ns)
-- 13.8 Arithmetic expressions
expr :: Parser Int
expr = do t <- term; do symbol "+"
e <- expr
return (t + e)
<|> return t
term :: Parser Int
term = do f <- factor; do symbol "*"
t <- term
return (f * t)
<|> return f
factor :: Parser Int
factor = do symbol "("
e <- expr
symbol ")"
return e
<|> natural
eval :: String -> Int
eval xs = case (parse expr xs) of
[(n, [])] -> n
[(_, out)] -> error ("Unused input " ++ out)
[] -> error ("Invalid input")
-- 13.9 Calculator
cls :: IO ()
cls = putStr "\ESC[2J"
type Pos = (Int,Int)
writeAt :: Pos -> String -> IO ()
writeAt p xs = do goto p
putStr xs
goto :: Pos -> IO ()
goto (x,y) = putStr ("\ESC[" ++ show y ++ ";" ++ show x ++ "H")
getCh :: IO Char
getCh = do hSetEcho stdin False
x <- getChar
hSetEcho stdin True
return x
box :: [String]
box = ["+---------------+",
"| |",
"+---+---+---+---+",
"| q | c | d | = |",
"+---+---+---+---+",
"| 1 | 2 | 3 | + |",
"+---+---+---+---+",
"| 4 | 5 | 6 | - |",
"+---+---+---+---+",
"| 7 | 8 | 9 | * |",
"+---+---+---+---+",
"| 0 | ( | ) | / |",
"+---+---+---+---+"]
buttons :: String
buttons = standard ++ extra
where
standard = "qcd=123+456-789*0()/"
extra = "QCD \ESC\BS\DEL\n"
showbox :: IO ()
showbox = sequence_ [writeAt (1,y) b | (y,b) <- zip [1..] box]
display xs = do writeAt (3,2) (replicate 13 ' ')
writeAt (3,2) (reverse (take 13 (reverse xs)))
calc :: String -> IO ()
calc xs = do display xs
c <- getCh
if elem c buttons then
process c xs
else
do beep
calc xs
beep :: IO ()
beep = putStr "\BEL"
process :: Char -> String -> IO ()
process c xs | elem c "qQ\ESC" = quit
| elem c "dD\BS\DEL" = delete' xs
| elem c "=\n" = eval' xs
| elem c "cC" = clear
| otherwise = press c xs
quit :: IO ()
quit = goto (1,14)
delete' :: String -> IO ()
delete' [] = calc []
delete' xs = calc (init xs)
eval' :: String -> IO ()
eval' xs = case parse expr xs of
[(n,[])] -> calc (show n)
_ -> do beep
calc xs
clear :: IO ()
clear = calc []
press :: Char -> String -> IO ()
press c xs = calc (xs ++ [c])
run :: IO ()
run = do cls
showbox
clear