diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-19 03:59:47 +0200 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2025-05-19 03:59:47 +0200 |
commit | 2dab312844708c869422a9a39a9ccc6a2a49effc (patch) | |
tree | cffb2f5f0c51c2c86c39be25117c84e589106223 | |
parent | 2337a9680699acd5cc69a663b358bb20080fd063 (diff) |
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.
-rw-r--r-- | src/Language/SimpleShell/Parser/Expr.hs | 26 |
1 files changed, 15 insertions, 11 deletions
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 |