From 2dab312844708c869422a9a39a9ccc6a2a49effc Mon Sep 17 00:00:00 2001 From: Einhard Leichtfuß Date: Mon, 19 May 2025 03:59:47 +0200 Subject: Fix expression parser Some were clear bugs, generally there was some confusion between expressions and terms. The latter have now been subdivided into strong and weak terms, where weak terms form the basis of expressions and strong terms are required for function arguments. --- src/Language/SimpleShell/Parser/Expr.hs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/Language/SimpleShell') diff --git a/src/Language/SimpleShell/Parser/Expr.hs b/src/Language/SimpleShell/Parser/Expr.hs index d0bd01a..57b1d22 100644 --- a/src/Language/SimpleShell/Parser/Expr.hs +++ b/src/Language/SimpleShell/Parser/Expr.hs @@ -28,22 +28,26 @@ import qualified Text.Megaparsec.Char.Lexer as L (charLiteral, decimal) exprP :: Parser TypedExpr exprP - = makeExprParser termP binaryOperatorTable + = makeExprParser weakTermP binaryOperatorTable + +weakTermP :: Parser TypedExpr +weakTermP + = strongTermP + <|> unaryOpP <|> builtinUnaryFunP <|> funP -termP :: Parser TypedExpr -termP +strongTermP :: Parser TypedExpr +strongTermP = literalP <|> varP - <|> unaryOpP <|> lexeme (char '(') *> exprP <* lexeme (char ')') --- | Parse expression with fixed type. -exprP' :: String -> SimpleType -> Parser Expr -exprP' errMsg t = do - (t', e) <- exprP +-- | Parse "strong" term with fixed type. +strongTermP' :: String -> SimpleType -> Parser Expr +strongTermP' errMsg t = do + (t', e) <- strongTermP if t == t' then return e else fail errMsg @@ -71,7 +75,7 @@ funP :: Parser TypedExpr funP = do fname <- lexeme funNameP (t', ts) <- lookupFun fname - args <- mapM (exprP' "Type mismatch with function signature.") ts + args <- mapM (strongTermP' "Type mismatch with function signature.") ts return (t', FunCall fname args) @@ -169,8 +173,8 @@ unaryOperators, builtinUnaryFuns :: [UnaryParser] unaryOpP, builtinUnaryFunP :: Parser TypedExpr (unaryOpP, builtinUnaryFunP) = - ( asum $ map (aux "unary operator" termP) unaryOperators - , asum $ map (aux "builtin unary function" exprP) builtinUnaryFuns + ( asum $ map (aux "unary operator" weakTermP ) unaryOperators + , asum $ map (aux "builtin unary function" strongTermP) builtinUnaryFuns ) where aux :: String -> Parser TypedExpr -> UnaryParser -> Parser TypedExpr -- cgit v1.2.3