-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathex9.hs
291 lines (242 loc) · 5.05 KB
/
ex9.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import Control.Applicative
import Data.Char
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
-- fmap :: (a -> b) -> Parser a -> Parser b
where
fmap g p =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
instance Applicative Parser
-- pure :: a -> Parser a
where
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 = pure g <*> item <*> item <*> item
where
g x y z = (x, z)
instance Monad Parser
-- (>>=) :: Parser a -> (a -> Parser b) -> Parser b
where
p >>= f =
P
(\inp ->
case parse p inp of
[] -> []
[(v, out)] -> parse (f v) out)
three' :: Parser (Char, Char)
three' = do
x <- item
item
z <- item
return (x, z)
instance Alternative Parser
-- empty :: Parser a
where
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)])
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
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)
nats :: Parser [Int]
nats = do
symbol "["
n <- natural
ns <-
many
(do symbol ","
natural)
symbol "]"
return (n : ns)
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
type Pos = (Int, Int)
cls :: IO ()
cls = putStr "\ESC[2J"
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 do
writeat (1, 14) (replicate 17 ' ')
process c xs
else do
writeat (1, 14) (c:" is not a button")
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, [])] -> do
writeat (2, 1) (replicate 15 '-')
calc (show n)
[(_, rs)] -> do
writeat (x, 1) "v"
calc xs
where x = length xs - length rs + 3
_ -> clear
clear :: IO ()
clear = do
writeat (2, 1) (replicate 15 '-')
writeat (1, 14) (replicate 17 ' ')
calc []
press :: Char -> String -> IO ()
press c xs = calc (xs ++ [c])
run :: IO ()
run = do
cls
showbox
clear
main :: IO ()
main = run