diff options
Diffstat (limited to 'src/Parser.hs')
-rw-r--r-- | src/Parser.hs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index dc754d3..69197b8 100644 --- a/src/Parser.hs +++ b/src/Parser.hs | |||
@@ -15,6 +15,7 @@ import Text.ParserCombinators.Parsec | |||
15 | -- TODO: add character literals: \#a \#b \#c \#space \#newline | 15 | -- TODO: add character literals: \#a \#b \#c \#space \#newline |
16 | -- TODO: add support for complex numbers, oct and hex numbers | 16 | -- TODO: add support for complex numbers, oct and hex numbers |
17 | data Expr = List [Expr] | 17 | data Expr = List [Expr] |
18 | | Vector [Expr] | ||
18 | | DottedList [Expr] Expr | 19 | | DottedList [Expr] Expr |
19 | | StringLiteral String | 20 | | StringLiteral String |
20 | | IntLiteral Integer | 21 | | IntLiteral Integer |
@@ -55,8 +56,15 @@ parseFloat = do | |||
55 | let fval = characteristic ++ "." ++ mantissa | 56 | let fval = characteristic ++ "." ++ mantissa |
56 | return $ (FloatLiteral . read) $ maybe fval (:fval) sign | 57 | return $ (FloatLiteral . read) $ maybe fval (:fval) sign |
57 | 58 | ||
59 | parseVector :: Parser Expr | ||
60 | parseVector = do | ||
61 | string "#(" >> optionalWhiteSpace | ||
62 | x <- sepEndBy parseLispValue whiteSpace | ||
63 | optionalWhiteSpace >> char ')' | ||
64 | return $ Vector x | ||
65 | |||
58 | symbol :: Parser Char | 66 | symbol :: Parser Char |
59 | symbol = oneOf "!#$%&|*+:/-=<?>@^_~" | 67 | symbol = oneOf "!$%&|*+:/-=<?>@^_~" |
60 | 68 | ||
61 | parseId :: Parser Expr | 69 | parseId :: Parser Expr |
62 | parseId = do | 70 | parseId = do |
@@ -95,9 +103,10 @@ parseLispValue = | |||
95 | <|> parseQuote | 103 | <|> parseQuote |
96 | <|> parseQuasiquote | 104 | <|> parseQuasiquote |
97 | <|> parseUnquote | 105 | <|> parseUnquote |
106 | <|> parseVector | ||
98 | -- handles lists and dotted lists | 107 | -- handles lists and dotted lists |
99 | <|> do | 108 | <|> do |
100 | char '(' >> spaces | 109 | char '(' >> optionalWhiteSpace |
101 | x <- sepEndBy parseLispValue whiteSpace | 110 | x <- sepEndBy parseLispValue whiteSpace |
102 | spaces | 111 | spaces |
103 | t <- optionMaybe $ char '.' >> space >> parseLispValue | 112 | t <- optionMaybe $ char '.' >> space >> parseLispValue |
@@ -111,6 +120,7 @@ showLispList = unwords . map show | |||
111 | instance Show Expr where | 120 | instance Show Expr where |
112 | show (DottedList xs x) = "(" ++ showLispList xs ++ " . " ++ show x ++ ")" | 121 | show (DottedList xs x) = "(" ++ showLispList xs ++ " . " ++ show x ++ ")" |
113 | show (List xs) = "(" ++ showLispList xs ++ ")" | 122 | show (List xs) = "(" ++ showLispList xs ++ ")" |
123 | show (Vector xs) = "#(" ++ showLispList xs ++ ")" | ||
114 | show (StringLiteral s) = "\"" ++ s ++ "\"" | 124 | show (StringLiteral s) = "\"" ++ s ++ "\"" |
115 | show (IntLiteral n) = show n | 125 | show (IntLiteral n) = show n |
116 | show (FloatLiteral n) = show n | 126 | show (FloatLiteral n) = show n |