From d355a5db8a95bc9fc79a801327bb45c1ed885783 Mon Sep 17 00:00:00 2001 From: Ivy Date: Sat, 30 Jan 2021 14:55:22 +0100 Subject: [PATCH] Added cons expression --- src/Evaluator.hs | 3 ++- src/ExprType.hs | 1 + src/Lexer.hs | 2 +- src/Parser.hs | 16 +++++++++++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Evaluator.hs b/src/Evaluator.hs index 22a31a1..7bb0da5 100644 --- a/src/Evaluator.hs +++ b/src/Evaluator.hs @@ -14,10 +14,11 @@ base = M.fromList [ ] eval :: Env -> Expr -> (Env, Expr) -eval env (IntE x) = (env, IntE x) +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 eval env (SetE v expr) = (M.insert v expr env, NilE) +eval env c@(ConsE _ _) = (env, c) eval env NilE = (env, NilE) --apply :: Proc -> Args -> Expr diff --git a/src/ExprType.hs b/src/ExprType.hs index 704c06d..06be2a6 100644 --- a/src/ExprType.hs +++ b/src/ExprType.hs @@ -4,6 +4,7 @@ data Expr = IntE Integer | VarE String | ProcedureE String [Expr] | SetE String Expr + | ConsE Expr Expr | NilE deriving (Show) -- instance Show Expr where diff --git a/src/Lexer.hs b/src/Lexer.hs index 5a60f82..beff3b5 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -29,5 +29,5 @@ rightParenT = char ')' >> return RightParenT symbolT :: GenParser Char st Token symbolT = do - var <- map toLower <$> many1 (letter <|> oneOf "+-*/!|@#$~%&/=<>") + var <- map toLower <$> many1 (letter <|> oneOf "+-*/\\!|@#$~%&/=<>") return $ SymbolT var diff --git a/src/Parser.hs b/src/Parser.hs index 28a84fc..9eb69dc 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -20,7 +20,7 @@ expressionFromTokensEOF = do expressionFromTokens :: GenParser Token st Expr expressionFromTokens = do - expr <- intE <|> try setE <|> try nilE <|> varE <|> procedureE + expr <- intE <|> try setE <|> try nilE <|> try consE <|> varE <|> procedureE return expr intE :: GenParser Token st Expr @@ -36,6 +36,15 @@ procedureE = do _ <- parseRightParenT return $ ProcedureE p args +consE :: GenParser Token st Expr +consE = do + _ <- parseLeftParenT + _ <- parseConsT + expr1 <- expressionFromTokens + expr2 <- expressionFromTokens + _ <- parseRightParenT + return $ ConsE expr1 expr2 + varE :: GenParser Token st Expr varE = do (SymbolT var) <- parseSymbolT @@ -78,6 +87,11 @@ parseNilT = satisfyT isNilT where isNilT (SymbolT "nil") = True isNilT _ = False +parseConsT :: GenParser Token st Token +parseConsT = satisfyT isConsT + where isConsT (SymbolT "cons") = True + isConsT _ = False + parseSymbolT :: GenParser Token st Token parseSymbolT = satisfyT isSymbolT where isSymbolT (SymbolT _) = True