-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatorWin32.lhs
161 lines (150 loc) · 4.99 KB
/
calculatorWin32.lhs
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
\begin{Verbatim}[numbers=left]
Programming in Haskell 9.6 절 계산기 보기,
Graham Hutton, Cambridge University Press, 2007.
Note by Ahn, Ki Yung: This code uses FFI to define getCh to make
this example work on MS Windows command line using using GHC.
The definition for getCh in this example works with GHC, but may not
work with some Haskell systems, such as Hugs. Moreover, the use of
control characters or getCh won't work non text based env like WinHugs.
> {-# LANGUAGE ForeignFunctionInterface#-}
> import Foreign.C
> import Char
> import Monad
> import Parsing
> import System.IO
\end{Verbatim}
\newpage
\begin{Verbatim}[numbers=left,firstnumber=last]
수식 문법분석기
---------------
> expr :: Parser Int
> expr = do t <- term
> do symbol "+"
> e <- expr
> return (t + e)
> +++ do symbol "-"
> e <- expr
> return (t - e)
> +++ return t
>
> term :: Parser Int
> term = do f <- factor
> do symbol "*"
> t <- term
> return (f * t)
> +++ do symbol "/"
> t <- term
> return (f `div` t)
> +++ return f
>
> factor :: Parser Int
> factor = do symbol "("
> e <- expr
> symbol ")"
> return e
> +++ integer
\end{Verbatim}
\newpage
\begin{Verbatim}[numbers=left,firstnumber=last]
간단한 동작 이끌어내기
----------------------
> getCh :: IO Char
> getCh = liftM (chr . fromEnum) c_getch
> foreign import ccall unsafe "conio.h getch" c_getch :: IO CInt
>
> beep :: IO ()
> beep = putStr "\BEL"
>
> cls :: IO ()
> cls = putStr "\ESC[2J"
>
> type Pos = (Int,Int)
>
> goto :: Pos -> IO ()
> goto (x,y) = putStr ("\ESC[" ++ show y ++ ";" ++ show x ++ "H")
>
> writeat :: Pos -> String -> IO ()
> writeat p xs = do goto p
> putStr xs
>
> seqn :: [IO a] -> IO ()
> seqn [] = return ()
> seqn (a:as) = do a
> seqn as
\end{Verbatim}
\newpage
\begin{Verbatim}[numbers=left,firstnumber=last]
계산기
------
> 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\r"
>
>
> showbox :: IO ()
> showbox = seqn [writeat (1,y) xs | (y,xs) <- zip [1..13] box]
>
> display xs = do writeat (3,2) " "
> 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
>
\end{Verbatim}
\newpage
\begin{Verbatim}[numbers=left,firstnumber=last]
> process :: Char -> String -> IO ()
> process c xs
> | elem c "qQ\ESC" = quit
> | elem c "dD\BS\DEL" = delete xs
> | elem c "=\n\r" = 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
\end{Verbatim}