-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
64 lines (48 loc) · 1.42 KB
/
main.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
module Main where
import System.Environment
import Monad
import Text.ParserCombinators.Parsec hiding (spaces)
-- parsing symbols
symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=>?@^_~"
--parsing space
spaces :: Parser ()
spaces = skipMany1 space
--parser function
readExpr :: String -> String
readExpr input = case parse parseExpr "lisp" input of
Left err -> "No match: " ++ show err
Right _ -> "Found value"
--data type
data LispVal = Atom String
| List [LispVal]
| DottedList [LispVal] LispVal
| Number Integer
| String String
| Bool Bool
--parseString
parseString :: Parser LispVal
parseString = do char '"'
x <- many (noneOf "\"")
char '"'
return $ String x
--parseAtom
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
otherwise -> Atom atom
--parseNumber
parseNumber :: Parser LispVal
parseNumber = liftM (Number . read) $ many1 digit
--Expression parsing
parseExpr :: Parser LispVal
parseExpr = parseAtom
<|> parseString
<|> parseNumber
main :: IO ()
main = do args <- getArgs
putStrLn (readExpr (args !! 0))