Compare commits

..

No commits in common. "d355a5db8a95bc9fc79a801327bb45c1ed885783" and "c15a736236b76a8fc58a223bf50c61fb682ae0c6" have entirely different histories.

7 changed files with 30 additions and 41 deletions

View File

@ -22,7 +22,8 @@ executable cherry
Parser, Parser,
TokenType, TokenType,
Lexer, Lexer,
Evaluator Evaluator,
ParserUtils
build-depends: base >= 4.7 && < 5, build-depends: base >= 4.7 && < 5,
containers, containers,

View File

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

View File

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

View File

@ -1,6 +1,7 @@
module Lexer where module Lexer where
import Text.ParserCombinators.Parsec import Text.ParserCombinators.Parsec
import ParserUtils
import Data.Char import Data.Char
import TokenType import TokenType
@ -12,7 +13,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 <|> try nilT <|> try setT <|> varT <|> intT
------------------------------------------------------------ ------------------------------------------------------------
@ -27,7 +28,13 @@ leftParenT = char '(' >> return LeftParenT
rightParenT :: GenParser Char st Token rightParenT :: GenParser Char st Token
rightParenT = char ')' >> return RightParenT rightParenT = char ')' >> return RightParenT
symbolT :: GenParser Char st Token setT :: GenParser Char st Token
symbolT = do setT = caseInsensitiveString "set!" >> return SetT
var <- map toLower <$> many1 (letter <|> oneOf "+-*/\\!|@#$~%&/=<>")
return $ SymbolT var nilT :: GenParser Char st Token
nilT = caseInsensitiveString "nil" >> return NilT
varT :: GenParser Char st Token
varT = do
var <- map toLower <$> many1 (letter <|> oneOf "+-*/!|@#$~%&/=<>")
return $ VarT var

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 <|> try defineE <|> try nilE <|> varE <|> procedureE
return expr return expr
intE :: GenParser Token st Expr intE :: GenParser Token st Expr
@ -31,23 +31,14 @@ intE = do
procedureE :: GenParser Token st Expr procedureE :: GenParser Token st Expr
procedureE = do procedureE = do
_ <- parseLeftParenT _ <- parseLeftParenT
(SymbolT p) <- parseSymbolT (VarT p) <- parseVarT
args <- many expressionFromTokens args <- many expressionFromTokens
_ <- parseRightParenT _ <- parseRightParenT
return $ ProcedureE p args 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 :: GenParser Token st Expr
varE = do varE = do
(SymbolT var) <- parseSymbolT (VarT var) <- parseVarT
return $ VarE var return $ VarE var
nilE :: GenParser Token st Expr nilE :: GenParser Token st Expr
@ -55,11 +46,11 @@ nilE = do
_ <- parseNilT _ <- parseNilT
return NilE return NilE
setE :: GenParser Token st Expr defineE :: GenParser Token st Expr
setE = do defineE = do
_ <- parseLeftParenT _ <- parseLeftParenT
_ <- parseSetT _ <- parseSetT
(SymbolT var) <- parseSymbolT (VarT var) <- parseVarT
expr <- expressionFromTokens expr <- expressionFromTokens
_ <- parseRightParenT _ <- parseRightParenT
return $ SetE var expr return $ SetE var expr
@ -78,24 +69,15 @@ parseRightParenT :: GenParser Token st Token
parseRightParenT = satisfyT (== RightParenT) parseRightParenT = satisfyT (== RightParenT)
parseSetT :: GenParser Token st Token parseSetT :: GenParser Token st Token
parseSetT = satisfyT isSetT parseSetT = satisfyT (== SetT)
where isSetT (SymbolT "set!") = True
isSetT _ = False
parseNilT :: GenParser Token st Token parseNilT :: GenParser Token st Token
parseNilT = satisfyT isNilT parseNilT = satisfyT (== NilT)
where isNilT (SymbolT "nil") = True
isNilT _ = False
parseConsT :: GenParser Token st Token parseVarT :: GenParser Token st Token
parseConsT = satisfyT isConsT parseVarT = satisfyT isVarT
where isConsT (SymbolT "cons") = True where isVarT (VarT _) = True
isConsT _ = False isVarT _ = False
parseSymbolT :: GenParser Token st Token
parseSymbolT = satisfyT isSymbolT
where isSymbolT (SymbolT _) = True
isSymbolT _ = False
parseIntT :: GenParser Token st Token parseIntT :: GenParser Token st Token
parseIntT = satisfyT isIntT parseIntT = satisfyT isIntT

View File

@ -1,4 +1,3 @@
-- UNUSED
module ParserUtils where module ParserUtils where
import Text.ParserCombinators.Parsec import Text.ParserCombinators.Parsec

View File

@ -2,6 +2,8 @@ module TokenType where
data Token = LeftParenT data Token = LeftParenT
| RightParenT | RightParenT
| SymbolT String | SetT
| VarT String
| IntT Integer | IntT Integer
| NilT
deriving (Show, Eq) deriving (Show, Eq)