aboutsummaryrefslogtreecommitdiff
path: root/src/Parser.hs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-10 06:45:42 +0100
committerAkshay <[email protected]>2020-10-10 06:45:42 +0100
commitf74d9c9bb3722fd20cea000b4f0c2a74be289a9c (patch)
treeceec2de1fdc82f6f2e9693449b969c24925fac8e /src/Parser.hs
parent5097a20545bbeeafec191f97e9d0ae3d215ada90 (diff)
add quasiquote, unquote modifiers, basic boolean operations
add more info to readme
Diffstat (limited to 'src/Parser.hs')
-rw-r--r--src/Parser.hs31
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 ((<$>))
13import Control.Monad (liftM) 13import Control.Monad (liftM)
14import Text.ParserCombinators.Parsec 14import Text.ParserCombinators.Parsec
15 15
16 16-- TODO: use LispNumber (src/Operators.hs) here instead of IntLiteral and FloatLiteral
17type Ident = String
18
19data Expr = List [Expr] 17data 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
28parseString :: Parser Expr 26parseString :: Parser Expr
@@ -68,12 +66,17 @@ parseDottedList = do
68 whiteSpace 66 whiteSpace
69 DottedList head <$> parseLispValue 67 DottedList head <$> parseLispValue
70 68
71parseQuote :: Parser Expr 69type Alias = String
72parseQuote = do 70parseModifier :: Char -> Alias -> Parser Expr
73 char '\'' 71parseModifier 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
76parseQuote = parseModifier '\'' "quote"
77parseQuasiquote = parseModifier '`' "quasiquote"
78parseUnquote = parseModifier ',' "unquote"
79-- TODO: add modifier for unquote splicing
77 80
78parseLispValue :: Parser Expr 81parseLispValue :: Parser Expr
79parseLispValue = 82parseLispValue =
@@ -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
97showLispList :: [Expr] -> String
98showLispList = unwords . map show
99
93instance Show Expr where 100instance 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