aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-14 15:05:12 +0100
committerAkshay <[email protected]>2020-10-14 15:05:12 +0100
commit14ad0667bf25351e522faa7d9fbb3ff31619d92e (patch)
tree651b5f03cb2c9bc232e3582b58a573622931da70
parent82a64ef64602227daefc3ff96908d6c1b2303b61 (diff)
add support for vectors
a
-rw-r--r--src/Evaluator.hs9
-rw-r--r--src/Parser.hs14
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
18eval :: Expr -> LispResult Expr 18eval :: Expr -> LispResult Expr
19eval v@(StringLiteral s) = return v 19eval v@(StringLiteral s) = return v
20eval v@(IntLiteral i) = return v 20eval v@(IntLiteral i) = return v
21eval v@(BoolLiteral b) = return v 21eval v@(BoolLiteral b) = return v
22eval v@(FloatLiteral f) = return v 22eval v@(FloatLiteral f) = return v
23eval v@(Vector xs) = liftM Vector $ mapM eval xs
23-- handle quotes as literals 24-- handle quotes as literals
24eval (List[Id "quote", val]) = return val 25eval (List[Id "quote", val]) = return val
25eval (List[Id "quasiquote", val]) = undefined 26eval (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
17data Expr = List [Expr] 17data 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
59parseVector :: Parser Expr
60parseVector = do
61 string "#(" >> optionalWhiteSpace
62 x <- sepEndBy parseLispValue whiteSpace
63 optionalWhiteSpace >> char ')'
64 return $ Vector x
65
58symbol :: Parser Char 66symbol :: Parser Char
59symbol = oneOf "!#$%&|*+:/-=<?>@^_~" 67symbol = oneOf "!$%&|*+:/-=<?>@^_~"
60 68
61parseId :: Parser Expr 69parseId :: Parser Expr
62parseId = do 70parseId = 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
111instance Show Expr where 120instance 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