diff options
author | Akshay <[email protected]> | 2020-10-14 15:05:12 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-10-14 15:05:12 +0100 |
commit | 14ad0667bf25351e522faa7d9fbb3ff31619d92e (patch) | |
tree | 651b5f03cb2c9bc232e3582b58a573622931da70 | |
parent | 82a64ef64602227daefc3ff96908d6c1b2303b61 (diff) |
add support for vectors
a
-rw-r--r-- | src/Evaluator.hs | 9 | ||||
-rw-r--r-- | src/Parser.hs | 14 |
2 files changed, 17 insertions, 6 deletions
diff --git a/src/Evaluator.hs b/src/Evaluator.hs index b0171ba..8a5274f 100644 --- a/src/Evaluator.hs +++ b/src/Evaluator.hs | |||
@@ -16,10 +16,11 @@ apply fn args = maybe | |||
16 | (lookup fn primitives) | 16 | (lookup fn primitives) |
17 | 17 | ||
18 | eval :: Expr -> LispResult Expr | 18 | eval :: Expr -> LispResult Expr |
19 | eval v@(StringLiteral s) = return v | 19 | eval v@(StringLiteral s) = return v |
20 | eval v@(IntLiteral i) = return v | 20 | eval v@(IntLiteral i) = return v |
21 | eval v@(BoolLiteral b) = return v | 21 | eval v@(BoolLiteral b) = return v |
22 | eval v@(FloatLiteral f) = return v | 22 | eval v@(FloatLiteral f) = return v |
23 | eval v@(Vector xs) = liftM Vector $ mapM eval xs | ||
23 | -- handle quotes as literals | 24 | -- handle quotes as literals |
24 | eval (List[Id "quote", val]) = return val | 25 | eval (List[Id "quote", val]) = return val |
25 | eval (List[Id "quasiquote", val]) = undefined | 26 | eval (List[Id "quasiquote", val]) = undefined |
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 |