-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchemeParser.hs
209 lines (180 loc) · 5.52 KB
/
SchemeParser.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
module SchemeParser (LispVal(..), ThrowsError(..), Env(..), IOThrowsError(..), LispError(..), parseExpr, spaces) where
import Text.ParserCombinators.Parsec hiding (spaces)
import qualified Text.ParserCombinators.Parsec as P
import Numeric (readFloat, readOct, readHex)
import Data.Ratio
import Data.Complex
import Data.Array
import Control.Monad
import Control.Monad.Error
import Data.IORef
import System.IO (Handle (..))
data LispVal = Atom String
| List [LispVal]
| DottedList [LispVal] LispVal
| Vector (Array Int LispVal)
| Number Integer
| Float Double
| Ratio Rational
| Complex (Complex Double)
| String String
| Bool Bool
| Character Char
| PrimitiveFunc ([LispVal] -> ThrowsError LispVal)
| IOFunc ([LispVal] -> IOThrowsError LispVal)
| Func { params :: [String], vararg :: (Maybe String),
body :: [LispVal], closure :: Env }
| Port Handle
data LispError = NumArgs Integer [LispVal]
| TypeMismatch String LispVal
| Parser ParseError
| BadSpecialForm String LispVal
| NotFunction String String
| UnboundVar String String
| Default String
type ThrowsError = Either LispError
type Env = IORef [(String, IORef LispVal)]
type IOThrowsError = ErrorT LispError IO
symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=>?@^_~"
spaces :: Parser ()
spaces = skipMany1 space
escapedChars :: Parser Char
escapedChars = do char '\\'
x <- oneOf "\\\"nrt"
return $ case x of
'\\' -> x
'"' -> x
'n' -> '\n'
'r' -> '\r'
't' -> '\t'
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 <|> digit <|> symbol)
let atom = first:rest
return $ case atom of
"#t" -> Bool True
"#f" -> Bool False
_ -> Atom atom
parseDecimal1 :: Parser LispVal
parseDecimal1 = many1 digit >>= (return . Number . read)
parseDecimal2 :: Parser LispVal
parseDecimal2 = do
try $ string "#d"
x <- many1 digit
(return . Number . read) x
parseDecimal :: Parser LispVal
parseDecimal = parseDecimal1 <|> parseDecimal2
parseHex :: Parser LispVal
parseHex = do
try $ string "#x"
x <- many1 hexDigit
return $ Number (hex2dig x)
parseOct :: Parser LispVal
parseOct = do
try $ string "#o"
x <- many1 octDigit
return $ Number (oct2dig x)
parseBin :: Parser LispVal
parseBin = do
try $ string "#b"
x <- many1 (oneOf "10")
return $ Number (bin2dig x)
oct2dig x = fst $ readOct x !! 0
hex2dig x = fst $ readHex x !! 0
bin2dig = bin2dig' 0
bin2dig' digint "" = digint
bin2dig' digint (x:xs) = let old = 2 * digint + (if x == '0' then 0 else 1) in
bin2dig' old xs
parseNumber :: Parser LispVal
parseNumber = parseDecimal <|> parseHex
<|> parseOct <|> parseBin
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))
parseComplex :: Parser LispVal
parseComplex = do
x <- (try parseFloat <|> parseDecimal)
char '+'
y <- (try parseFloat <|> parseDecimal)
char 'i'
return $ Complex (toDouble x :+ toDouble y)
where
toDouble (Float f) = realToFrac f
toDouble (Number n) = fromIntegral n
parseQuoted :: Parser LispVal
parseQuoted = do
char '\''
x <- parseExpr
return $ List [Atom "quote", x]
parseQuasiQuoted :: Parser LispVal
parseQuasiQuoted = do
char '`'
x <- parseExpr
return $ List [Atom "quasiquoted", x]
parseUnQuote :: Parser LispVal
parseUnQuote = do
char ','
x <- parseExpr
return $ List [Atom "unqoute", x]
parseBool :: Parser LispVal
parseBool = do
char '#'
(char 't' >> return (Bool True)) <|> (char 'f' >> return (Bool False))
parseCharacter :: Parser LispVal
parseCharacter = do
try $ string "#\\"
value <- try (string "newline" <|> string "space")
<|> do { x <- anyChar; notFollowedBy alphaNum ; return [x] }
return $ Character $ case value of
"space" -> ' '
"newline" -> '\n'
otherwise -> (value !! 0)
parseList :: Parser LispVal
parseList = liftM List $ sepBy parseExpr spaces
parseDottedList :: Parser LispVal
parseDottedList = do
head <- endBy parseExpr spaces
tail <- char '.' >> spaces >> parseExpr
return $ DottedList head tail
parseVector :: Parser LispVal
parseVector = do
arrayValues <- sepBy parseExpr spaces
return $ Vector (listArray (0,(length arrayValues - 1)) arrayValues)
parseExpr :: Parser LispVal
parseExpr = parseAtom
<|> parseString
<|> try parseComplex
<|> try parseFloat
<|> try parseRatio
<|> try parseNumber
<|> try parseBool
<|> try parseCharacter
<|> parseQuoted
<|> parseQuasiQuoted
<|> parseUnQuote
<|> try (do
string "#("
x <- parseVector
char ')'
return x)
<|> do char '('
x <- try parseList <|> parseDottedList
char ')'
return x