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

@ -14,10 +14,11 @@ base = M.fromList [
]
eval :: Env -> Expr -> (Env, Expr)
eval env i@(IntE x) = (env, i)
eval env i@(IntE x) = (env, i)
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 (QuotedE e) = (env, e)
eval env c@(ConsE _ _) = (env, c)
eval env NilE = (env, NilE)

View File

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

View File

@ -12,7 +12,7 @@ parseTokens = do
return tokns
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 = char '(' >> return LeftParenT
apostropheT :: GenParser Char st Token
apostropheT = char '\'' >> return ApostropheT
rightParenT :: GenParser Char st Token
rightParenT = char ')' >> return RightParenT

View File

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

View File

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