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
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 = char ')' >> return RightParenT
backslashT :: GenParser Char st Token
backslashT = char '\\' >> return BackslashT
symbolT :: GenParser Char st Token
symbolT = do
var <- map toLower <$> many1 (letter <|> oneOf "+-*/\\!|@#$~%&/=<>")
var <- map toLower <$> many1 (letter <|> oneOf "+-*/!|@#$~%&/=<>")
return $ SymbolT var

View File

@ -20,7 +20,7 @@ expressionFromTokensEOF = do
expressionFromTokens :: GenParser Token st Expr
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
listE :: GenParser Token st Expr
@ -71,6 +71,14 @@ setE = do
_ <- parseRightParenT
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
@ -78,6 +86,9 @@ satisfyT f = tokenPrim show
(\pos _ _ -> incSourceColumn pos 1)
(\t -> if f t then Just t else Nothing)
parseBackslashT :: GenParser Token st Token
parseBackslashT = satisfyT (== BackslashT)
parseLeftParenT :: GenParser Token st Token
parseLeftParenT = satisfyT (== LeftParenT)

View File

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