diff options
Diffstat (limited to 'src/Parser.hs')
-rw-r--r-- | src/Parser.hs | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index 9813f5c..5053d0a 100644 --- a/src/Parser.hs +++ b/src/Parser.hs | |||
@@ -13,16 +13,14 @@ import Control.Applicative ((<$>)) | |||
13 | import Control.Monad (liftM) | 13 | import Control.Monad (liftM) |
14 | import Text.ParserCombinators.Parsec | 14 | import Text.ParserCombinators.Parsec |
15 | 15 | ||
16 | 16 | -- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral | |
17 | type Ident = String | ||
18 | |||
19 | data Expr = List [Expr] | 17 | data Expr = List [Expr] |
20 | | DottedList [Expr] Expr | 18 | | DottedList [Expr] Expr |
21 | | StringLiteral String | 19 | | StringLiteral String |
22 | | IntLiteral Integer | 20 | | IntLiteral Integer |
23 | | FloatLiteral Double | 21 | | FloatLiteral Double |
24 | | BoolLiteral Bool | 22 | | BoolLiteral Bool |
25 | | Id Ident | 23 | | Id String |
26 | deriving (Eq) | 24 | deriving (Eq) |
27 | 25 | ||
28 | parseString :: Parser Expr | 26 | parseString :: Parser Expr |
@@ -68,12 +66,17 @@ parseDottedList = do | |||
68 | whiteSpace | 66 | whiteSpace |
69 | DottedList head <$> parseLispValue | 67 | DottedList head <$> parseLispValue |
70 | 68 | ||
71 | parseQuote :: Parser Expr | 69 | type Alias = String |
72 | parseQuote = do | 70 | parseModifier :: Char -> Alias -> Parser Expr |
73 | char '\'' | 71 | parseModifier c alias = do |
72 | char c | ||
74 | x <- parseLispValue | 73 | x <- parseLispValue |
75 | return $ List [Id "quote", x] | 74 | return $ List [Id alias, x] |
76 | 75 | ||
76 | parseQuote = parseModifier '\'' "quote" | ||
77 | parseQuasiquote = parseModifier '`' "quasiquote" | ||
78 | parseUnquote = parseModifier ',' "unquote" | ||
79 | -- TODO: add modifier for unquote splicing | ||
77 | 80 | ||
78 | parseLispValue :: Parser Expr | 81 | parseLispValue :: Parser Expr |
79 | parseLispValue = | 82 | parseLispValue = |
@@ -82,7 +85,8 @@ parseLispValue = | |||
82 | <|> try parseFloat | 85 | <|> try parseFloat |
83 | <|> parseInt | 86 | <|> parseInt |
84 | <|> parseQuote | 87 | <|> parseQuote |
85 | -- TODO: figure out a way to have floats and dotted lists | 88 | <|> parseQuasiquote |
89 | <|> parseUnquote | ||
86 | <|> do | 90 | <|> do |
87 | char '(' | 91 | char '(' |
88 | x <- try parseList <|> parseDottedList | 92 | x <- try parseList <|> parseDottedList |
@@ -90,12 +94,15 @@ parseLispValue = | |||
90 | return x | 94 | return x |
91 | <?> "expected lisp value!" | 95 | <?> "expected lisp value!" |
92 | 96 | ||
97 | showLispList :: [Expr] -> String | ||
98 | showLispList = unwords . map show | ||
99 | |||
93 | instance Show Expr where | 100 | instance Show Expr where |
94 | show (DottedList xs x) = "(" ++ unwords (map show xs) ++ " . " ++ show x ++ ")" | 101 | show (DottedList xs x) = "(" ++ showLispList xs ++ " . " ++ show x ++ ")" |
95 | show (List xs) = "(" ++ unwords (map show xs) ++ ")" | 102 | show (List xs) = "(" ++ showLispList xs ++ ")" |
96 | show (StringLiteral s) = "\"" ++ s ++ "\"" | 103 | show (StringLiteral s) = "\"" ++ s ++ "\"" |
97 | show (IntLiteral n) = show n | 104 | show (IntLiteral n) = show n |
98 | show (FloatLiteral n) = show n | 105 | show (FloatLiteral n) = show n |
99 | show (BoolLiteral True) = "#t" | 106 | show (BoolLiteral True) = "#t" |
100 | show (BoolLiteral False) = "#f" | 107 | show (BoolLiteral False) = "#f" |
101 | show (Id i) = i | 108 | show (Id i) = i |