diff options
author | Marvin Borner | 2022-08-12 15:26:03 +0200 |
---|---|---|
committer | Marvin Borner | 2022-08-12 15:26:03 +0200 |
commit | cce495b3b4440997274ecab3d72ed61d6a50b007 (patch) | |
tree | cc56c7af514dd6ca31edb5360a6682ac5439af01 /src/Parser.hs | |
parent | b3cf49974e8af4e35ffc01fbe2f8e181d38de03a (diff) |
Added infix operator support
This isn't compatible with the :test .. = .. syntax, therefore I removed
it. They also don't have custom precedence/associativity support and
aren't chainable right now.
Diffstat (limited to 'src/Parser.hs')
-rw-r--r-- | src/Parser.hs | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index 4237db7..ddf09fb 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -17,14 +17,14 @@ type Parser = Parsec Void String sc :: Parser () sc = void $ char ' ' --- zero or more spaces --- scs :: Parser () --- scs = void $ takeWhileP (Just "white space") (== ' ') +infixOperator :: Parser String +infixOperator = some $ oneOf "!?*@+$%^&<>/|=" -- def identifier disallows the import prefix dots defIdentifier :: Parser String defIdentifier = ((:) <$> (letterChar <|> char '_') <*> many (alphaNumChar <|> oneOf "?!'_-")) + <|> parens infixOperator <?> "defining identifier" -- TODO: write as extension to defIdentifier @@ -53,7 +53,7 @@ parseAbstraction = do -- one or more singletons wrapped in coupled application parseApplication :: Parser Expression parseApplication = do - s <- sepEndBy1 parseSingleton sc -- TODO: Fix consuming space at end (re. test =) + s <- sepEndBy1 parseSingleton sc pure $ foldl1 Application s parseBruijn :: Parser Expression @@ -95,19 +95,29 @@ parseVariable = do var <- identifier pure $ Variable var +parseInfix :: Parser Expression +parseInfix = do + e1 <- parseSingleton + sc + i <- infixOperator + sc + e2 <- parseSingleton + pure $ Infix e1 i e2 + parseSingleton :: Parser Expression parseSingleton = parseBruijn - <|> parseNumeral + <|> try parseNumeral <|> parseString <|> parseChar <|> parseAbstraction + <|> try (parens parseInfix <?> "enclosed infix expr") <|> (parens parseApplication <?> "enclosed application") <|> parseVariable parseExpression :: Parser Expression parseExpression = do - e <- parseApplication + e <- try parseInfix <|> parseApplication pure e <?> "expression" parseEvaluate :: Parser Instruction @@ -160,9 +170,9 @@ parseTest :: Parser Instruction parseTest = do inp <- getInput _ <- string ":test " <?> "test" - e1 <- parseExpression - _ <- string "= " -- TODO: Disallow missing space (non-trivial) - e2 <- parseExpression + e1 <- (parens parseExpression <?> "first expression") + sc + e2 <- (parens parseExpression <?> "second expression") pure $ ContextualInstruction (Test e1 e2) inp parseCommentBlock :: Parser Instruction |