2021-01-29 23:11:03 +00:00
|
|
|
module Parser where
|
2021-01-28 01:16:30 +00:00
|
|
|
|
|
|
|
import Text.Parsec
|
|
|
|
import Text.Parsec.String
|
2021-01-29 23:11:03 +00:00
|
|
|
import Lexer
|
2021-01-28 01:16:30 +00:00
|
|
|
import TokenType
|
|
|
|
import ExprType
|
|
|
|
|
|
|
|
parseExpression :: String -> Either ParseError Expr
|
|
|
|
parseExpression s = do
|
|
|
|
tokns <- parse parseTokens "lexical error" s
|
2021-01-29 00:16:15 +00:00
|
|
|
expr <- parse expressionFromTokensEOF "parsing error" tokns
|
|
|
|
return expr
|
|
|
|
|
|
|
|
expressionFromTokensEOF :: GenParser Token st Expr
|
|
|
|
expressionFromTokensEOF = do
|
|
|
|
expr <- expressionFromTokens
|
|
|
|
_ <- eof
|
2021-01-28 01:16:30 +00:00
|
|
|
return expr
|
|
|
|
|
2021-01-28 23:47:53 +00:00
|
|
|
expressionFromTokens :: GenParser Token st Expr
|
|
|
|
expressionFromTokens = do
|
2021-01-30 13:55:22 +00:00
|
|
|
expr <- intE <|> try setE <|> try nilE <|> try consE <|> varE <|> procedureE
|
2021-01-28 23:47:53 +00:00
|
|
|
return expr
|
2021-01-28 01:16:30 +00:00
|
|
|
|
|
|
|
intE :: GenParser Token st Expr
|
|
|
|
intE = do
|
|
|
|
(IntT num) <- parseIntT
|
|
|
|
return $ IntE num
|
|
|
|
|
|
|
|
procedureE :: GenParser Token st Expr
|
|
|
|
procedureE = do
|
|
|
|
_ <- parseLeftParenT
|
2021-01-30 13:49:21 +00:00
|
|
|
(SymbolT p) <- parseSymbolT
|
2021-01-28 23:47:53 +00:00
|
|
|
args <- many expressionFromTokens
|
2021-01-28 01:16:30 +00:00
|
|
|
_ <- parseRightParenT
|
|
|
|
return $ ProcedureE p args
|
|
|
|
|
2021-01-30 13:55:22 +00:00
|
|
|
consE :: GenParser Token st Expr
|
|
|
|
consE = do
|
|
|
|
_ <- parseLeftParenT
|
|
|
|
_ <- parseConsT
|
|
|
|
expr1 <- expressionFromTokens
|
|
|
|
expr2 <- expressionFromTokens
|
|
|
|
_ <- parseRightParenT
|
|
|
|
return $ ConsE expr1 expr2
|
|
|
|
|
2021-01-28 01:16:30 +00:00
|
|
|
varE :: GenParser Token st Expr
|
|
|
|
varE = do
|
2021-01-30 13:49:21 +00:00
|
|
|
(SymbolT var) <- parseSymbolT
|
2021-01-28 01:16:30 +00:00
|
|
|
return $ VarE var
|
|
|
|
|
|
|
|
nilE :: GenParser Token st Expr
|
|
|
|
nilE = do
|
|
|
|
_ <- parseNilT
|
|
|
|
return NilE
|
|
|
|
|
2021-01-30 13:49:21 +00:00
|
|
|
setE :: GenParser Token st Expr
|
|
|
|
setE = do
|
2021-01-28 01:16:30 +00:00
|
|
|
_ <- parseLeftParenT
|
2021-01-29 22:58:29 +00:00
|
|
|
_ <- parseSetT
|
2021-01-30 13:49:21 +00:00
|
|
|
(SymbolT var) <- parseSymbolT
|
2021-01-28 23:47:53 +00:00
|
|
|
expr <- expressionFromTokens
|
2021-01-28 01:16:30 +00:00
|
|
|
_ <- parseRightParenT
|
2021-01-29 22:58:29 +00:00
|
|
|
return $ SetE var expr
|
2021-01-28 01:16:30 +00:00
|
|
|
|
|
|
|
------------------------------------------------------------
|
|
|
|
|
|
|
|
satisfyT :: (Stream s m a, Show a) => (a -> Bool) -> ParsecT s u m a
|
|
|
|
satisfyT f = tokenPrim show
|
|
|
|
(\pos _ _ -> incSourceColumn pos 1)
|
|
|
|
(\t -> if f t then Just t else Nothing)
|
|
|
|
|
|
|
|
parseLeftParenT :: GenParser Token st Token
|
|
|
|
parseLeftParenT = satisfyT (== LeftParenT)
|
|
|
|
|
|
|
|
parseRightParenT :: GenParser Token st Token
|
|
|
|
parseRightParenT = satisfyT (== RightParenT)
|
|
|
|
|
2021-01-29 22:58:29 +00:00
|
|
|
parseSetT :: GenParser Token st Token
|
2021-01-30 13:49:21 +00:00
|
|
|
parseSetT = satisfyT isSetT
|
|
|
|
where isSetT (SymbolT "set!") = True
|
|
|
|
isSetT _ = False
|
2021-01-28 01:16:30 +00:00
|
|
|
|
|
|
|
parseNilT :: GenParser Token st Token
|
2021-01-30 13:49:21 +00:00
|
|
|
parseNilT = satisfyT isNilT
|
|
|
|
where isNilT (SymbolT "nil") = True
|
|
|
|
isNilT _ = False
|
|
|
|
|
2021-01-30 13:55:22 +00:00
|
|
|
parseConsT :: GenParser Token st Token
|
|
|
|
parseConsT = satisfyT isConsT
|
|
|
|
where isConsT (SymbolT "cons") = True
|
|
|
|
isConsT _ = False
|
|
|
|
|
2021-01-30 13:49:21 +00:00
|
|
|
parseSymbolT :: GenParser Token st Token
|
|
|
|
parseSymbolT = satisfyT isSymbolT
|
|
|
|
where isSymbolT (SymbolT _) = True
|
|
|
|
isSymbolT _ = False
|
2021-01-28 01:16:30 +00:00
|
|
|
|
|
|
|
parseIntT :: GenParser Token st Token
|
|
|
|
parseIntT = satisfyT isIntT
|
|
|
|
where isIntT (IntT _) = True
|
|
|
|
isIntT _ = False
|