diff options
author | Marvin Borner | 2022-04-12 14:20:22 +0200 |
---|---|---|
committer | Marvin Borner | 2022-04-12 14:20:22 +0200 |
commit | af953120359d9d65f2841f4ec67c4e2178d282af (patch) | |
tree | e0ee80be02ffa37c6f51cfdcf4ffe7a548e4adc4 /src/Parser.hs | |
parent | 32515bf8bf04958f22ce2cfe98edebc7b892774c (diff) |
OK
Diffstat (limited to 'src/Parser.hs')
-rw-r--r-- | src/Parser.hs | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/Parser.hs b/src/Parser.hs index 611261f..1f768bd 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -5,10 +5,18 @@ import Text.Parsec import Text.Parsec.Language import qualified Text.Parsec.Token as Token +data Error = SyntaxError ParseError | UndeclaredFunction String | InvalidIndex Int | FatalError String +instance Show Error where + show (SyntaxError err) = show err + show (UndeclaredFunction err) = "ERROR: undeclared function " <> show err + show (InvalidIndex err) = "ERROR: invalid index " <> show err + show (FatalError err) = show err +type Failable = Either Error + languageDef :: GenLanguageDef String u Identity languageDef = emptyDef { Token.commentLine = "#" , Token.identStart = letter - , Token.identLetter = alphaNum <|> char '_' + , Token.identLetter = alphaNum <|> char '?' , Token.reservedOpNames = ["[", "]"] } @@ -26,27 +34,38 @@ reservedOp = Token.reservedOp lexer parens :: Parser a -> Parser a parens = Token.parens lexer -data Expression = Index Int | Abstraction Int Expression | Application Expression Expression - deriving (Ord, Eq) +data Expression = Bruijn Int | Variable String | Abstraction Expression | Application Expression Expression + deriving (Ord, Eq, Show) data Instruction = Define String Expression | Evaluate Expression | Comment String + deriving (Show) parseAbstraction :: Parser Expression parseAbstraction = do reservedOp "[" - idc <- endBy1 digit spaces - build idc <$> parseExpression - where - build (idx : idc) body = - Abstraction ((read . pure :: Char -> Int) idx) $ build idc body - curry [] body = body + exp <- parseExpression + reservedOp "]" + pure $ Abstraction exp parseApplication :: Parser Expression parseApplication = do s <- sepBy1 parseSingleton spaces pure $ foldl1 Application s +parseBruijn :: Parser Expression +parseBruijn = do + idx <- digit + spaces + pure $ Bruijn $ (read . pure) idx + +parseVariable :: Parser Expression +parseVariable = do + var <- identifier + spaces + pure $ Variable var + parseSingleton :: Parser Expression -parseSingleton = parseAbstraction <|> parens parseApplication +parseSingleton = + parseAbstraction <|> parens parseApplication <|> parseBruijn <|> parseVariable parseExpression :: Parser Expression parseExpression = do @@ -56,11 +75,14 @@ parseExpression = do parseDefine :: Parser Instruction parseDefine = do var <- identifier - space + spaces Define var <$> parseExpression +parseComment :: Parser Instruction +parseComment = string "#" >> Comment <$> many letter + parseLine :: Parser Instruction -parseLine = try parseDefine +parseLine = try parseDefine <|> parseComment parseReplLine :: Parser Expression parseReplLine = try parseExpression |