Added lambda syntax on the language

This commit is contained in:
Ivy 2021-01-30 20:47:05 +01:00
parent 8043186001
commit ee13a75141
3 changed files with 18 additions and 3 deletions

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 <|> apostropheT <|> symbolT <|> intT anyLispToken = leftParenT <|> rightParenT <|> apostropheT <|> backslashT <|> symbolT <|> intT
------------------------------------------------------------ ------------------------------------------------------------
@ -30,7 +30,10 @@ apostropheT = char '\'' >> return ApostropheT
rightParenT :: GenParser Char st Token rightParenT :: GenParser Char st Token
rightParenT = char ')' >> return RightParenT rightParenT = char ')' >> return RightParenT
backslashT :: GenParser Char st Token
backslashT = char '\\' >> return BackslashT
symbolT :: GenParser Char st Token symbolT :: GenParser Char st Token
symbolT = do symbolT = do
var <- map toLower <$> many1 (letter <|> oneOf "+-*/\\!|@#$~%&/=<>") var <- map toLower <$> many1 (letter <|> oneOf "+-*/!|@#$~%&/=<>")
return $ SymbolT var return $ SymbolT 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 <|> quotedE <|> try setE <|> try nilE <|> try consE <|> varE <|> listE expr <- intE <|> quotedE <|> try setE <|> try nilE <|> try consE <|> try lambdaE <|> varE <|> listE
return expr return expr
listE :: GenParser Token st Expr listE :: GenParser Token st Expr
@ -71,6 +71,14 @@ setE = do
_ <- parseRightParenT _ <- parseRightParenT
return $ SetE var expr return $ SetE var expr
lambdaE :: GenParser Token st Expr
lambdaE = do
_ <- parseBackslashT
(SymbolT arg) <- parseSymbolT
body <- expressionFromTokens
return $ LambdaE arg body
------------------------------------------------------------ ------------------------------------------------------------
satisfyT :: (Stream s m a, Show a) => (a -> Bool) -> ParsecT s u m a satisfyT :: (Stream s m a, Show a) => (a -> Bool) -> ParsecT s u m a
@ -78,6 +86,9 @@ satisfyT f = tokenPrim show
(\pos _ _ -> incSourceColumn pos 1) (\pos _ _ -> incSourceColumn pos 1)
(\t -> if f t then Just t else Nothing) (\t -> if f t then Just t else Nothing)
parseBackslashT :: GenParser Token st Token
parseBackslashT = satisfyT (== BackslashT)
parseLeftParenT :: GenParser Token st Token parseLeftParenT :: GenParser Token st Token
parseLeftParenT = satisfyT (== LeftParenT) parseLeftParenT = satisfyT (== LeftParenT)

View File

@ -2,6 +2,7 @@ module TokenType where
data Token = LeftParenT data Token = LeftParenT
| RightParenT | RightParenT
| BackslashT
| SymbolT String | SymbolT String
| ApostropheT | ApostropheT
| IntT Integer | IntT Integer