Added quoted expressions

This commit is contained in:
Ivy 2021-01-30 15:28:30 +01:00
parent d355a5db8a
commit e96cddff9b
5 changed files with 19 additions and 4 deletions

View File

@ -18,6 +18,7 @@ eval env i@(IntE x) = (env, i)
eval env (VarE v) = (M.insert v nExpr nEnv, nExpr) eval env (VarE v) = (M.insert v nExpr nEnv, nExpr)
where (nEnv, nExpr) = eval env $ env M.! v where (nEnv, nExpr) = eval env $ env M.! v
eval env (SetE v expr) = (M.insert v expr env, NilE) eval env (SetE v expr) = (M.insert v expr env, NilE)
eval env (QuotedE e) = (env, e)
eval env c@(ConsE _ _) = (env, c) eval env c@(ConsE _ _) = (env, c)
eval env NilE = (env, NilE) eval env NilE = (env, NilE)

View File

@ -5,6 +5,7 @@ data Expr = IntE Integer
| ProcedureE String [Expr] | ProcedureE String [Expr]
| SetE String Expr | SetE String Expr
| ConsE Expr Expr | ConsE Expr Expr
| QuotedE Expr
| NilE deriving (Show) | NilE deriving (Show)
-- instance Show Expr where -- instance Show Expr where

View File

@ -12,7 +12,7 @@ parseTokens = do
return tokns return tokns
anyLispToken :: GenParser Char st Token anyLispToken :: GenParser Char st Token
anyLispToken = leftParenT <|> rightParenT <|> symbolT <|> intT anyLispToken = leftParenT <|> rightParenT <|> apostropheT <|> symbolT <|> intT
------------------------------------------------------------ ------------------------------------------------------------
@ -24,6 +24,9 @@ intT = do
leftParenT :: GenParser Char st Token leftParenT :: GenParser Char st Token
leftParenT = char '(' >> return LeftParenT leftParenT = char '(' >> return LeftParenT
apostropheT :: GenParser Char st Token
apostropheT = char '\'' >> return ApostropheT
rightParenT :: GenParser Char st Token rightParenT :: GenParser Char st Token
rightParenT = char ')' >> return RightParenT rightParenT = char ')' >> return RightParenT

View File

@ -20,7 +20,7 @@ expressionFromTokensEOF = do
expressionFromTokens :: GenParser Token st Expr expressionFromTokens :: GenParser Token st Expr
expressionFromTokens = do expressionFromTokens = do
expr <- intE <|> try setE <|> try nilE <|> try consE <|> varE <|> procedureE expr <- intE <|> quotedE <|> try setE <|> try nilE <|> try consE <|> varE <|> procedureE
return expr return expr
intE :: GenParser Token st Expr intE :: GenParser Token st Expr
@ -28,6 +28,12 @@ intE = do
(IntT num) <- parseIntT (IntT num) <- parseIntT
return $ IntE num return $ IntE num
quotedE :: GenParser Token st Expr
quotedE = do
_ <- parseApostropheT
expr <- expressionFromTokens
return $ QuotedE expr
procedureE :: GenParser Token st Expr procedureE :: GenParser Token st Expr
procedureE = do procedureE = do
_ <- parseLeftParenT _ <- parseLeftParenT
@ -77,6 +83,9 @@ parseLeftParenT = satisfyT (== LeftParenT)
parseRightParenT :: GenParser Token st Token parseRightParenT :: GenParser Token st Token
parseRightParenT = satisfyT (== RightParenT) parseRightParenT = satisfyT (== RightParenT)
parseApostropheT :: GenParser Token st Token
parseApostropheT = satisfyT (== ApostropheT)
parseSetT :: GenParser Token st Token parseSetT :: GenParser Token st Token
parseSetT = satisfyT isSetT parseSetT = satisfyT isSetT
where isSetT (SymbolT "set!") = True where isSetT (SymbolT "set!") = True

View File

@ -3,5 +3,6 @@ module TokenType where
data Token = LeftParenT data Token = LeftParenT
| RightParenT | RightParenT
| SymbolT String | SymbolT String
| ApostropheT
| IntT Integer | IntT Integer
deriving (Show, Eq) deriving (Show, Eq)