-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1.7.hs
138 lines (116 loc) · 3.69 KB
/
2.1.7.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
import Control.Monad
import Text.ParserCombinators.Parsec hiding (spaces)
import System.Environment
import Numeric
import Data.Ratio
import Data.Complex
main :: IO ()
main = do
putStrLn ">>>> "
expr <- getLine
putStrLn $ readExpr expr
readExpr :: String -> String
readExpr input = case parse parseExpr "lisp" input of
Left err -> "No match: " ++ show err
Right v -> "Found value " ++ show v
parseExpr :: Parser LispVal
parseExpr = parseAtom
<|> parseString
<|> try parseComplex
<|> try parseFloat
<|> try parseRatio
<|> try parseNumber
<|> try parseBool
<|> try parseCharacter
-- Terminals --
symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=>?@^_~"
spaces :: Parser ()
spaces = skipMany1 space
-- Data Type --
data LispVal = Atom String
| List [LispVal]
| DottedList [LispVal] LispVal
| Complex (Complex Double)
| Number Integer
| Float Double
| String String
| Bool Bool
| Ratio Rational
| Character Char deriving (Show)
escapedChars :: Parser Char
escapedChars = do
char '\\'
x <- oneOf "\\\"tnr"
return $ case x of
't' -> '\t'
'n' -> '\n'
'r' -> '\r'
_ -> x
parseString :: Parser LispVal
parseString = do
char '\"'
-- 注意一定要优先尝试转义字符 --
x <- many $ escapedChars <|> noneOf "\""
char '\"'
return $ String x
parseAtom :: Parser LispVal
parseAtom = do
first <- letter <|> symbol
rest <- many $ letter <|> symbol <|> digit
let atom = first : rest
return $ case atom of
"#t" -> Bool True
"#f" -> Bool False
_ -> Atom atom
parseBool :: Parser LispVal
parseBool = do
char '#'
(char 't' >> return (Bool True)) <|> (char 'f' >> return (Bool False))
parseCharacter :: Parser LispVal
parseCharacter = do
try $ string "#\\"
val <- try (string "newline" <|> string "space") <|>
do {x <- anyChar; notFollowedBy alphaNum; return [x]}
return . Character $ case val of
"space" -> ' '
"newline" -> '\n'
_ -> head val
parseComplex :: Parser LispVal
parseComplex = do
x <- try parseFloat <|> parseDec1 <|> parseDec2
c <- oneOf "+-"
y <- try parseFloat <|> parseDec1 <|> parseDec2
char 'i'
return . Complex $ toDouble x :+ toDouble y * if c == '+' then 1 else -1
parseFloat :: Parser LispVal
parseFloat = do
x <- many1 digit
char '.'
y <- many1 digit
return . Float . fst . head . readFloat $ x ++ "." ++ y
parseRatio :: Parser LispVal
parseRatio = do
x <- many1 digit
char '/'
y <- many1 digit
return . Ratio $ read x % read y
parseNumber :: Parser LispVal
parseNumber =
parseDec1 <|> parseDec2 <|> parseHex <|> parseOct <|> parseBin
parseDec1 :: Parser LispVal
parseDec1 = liftM (Number . read) $ many1 digit
parseDec2 :: Parser LispVal
parseDec2 = try (string "#d") >> many1 octDigit >>= return . Number . read
parseHex :: Parser LispVal
parseHex = try (string "#x") >> many1 hexDigit >>= return . Number . hex2dig
where hex2dig = fst . head . readHex
parseOct :: Parser LispVal
parseOct = try (string "#o") >> many1 octDigit >>= return . Number . oct2dig
where oct2dig = fst . head . readOct
parseBin :: Parser LispVal
parseBin = try (string "#b") >> many1 (oneOf "01") >>= return . Number . bin2dig
where bin2dig = sum . zipWith (\b c -> if c == '1' then 2 ^ b else 0) [0..] . reverse
toDouble :: LispVal -> Double
toDouble (Float f) = realToFrac f
toDouble (Number n) = fromIntegral n