From e1fbd95e4eab218952d8b72f672e35c771291dfd Mon Sep 17 00:00:00 2001 From: rockofox Date: Sat, 7 Dec 2024 14:27:57 +0100 Subject: [PATCH] Make parser properly parse function definitions with 0 parameters (fix #11) --- lib/Parser.hs | 2 +- tests/ParserSpec.hs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Parser.hs b/lib/Parser.hs index b15e551..b26d917 100644 --- a/lib/Parser.hs +++ b/lib/Parser.hs @@ -260,7 +260,7 @@ defArg = try structLit <|> var <|> parens listPattern <|> array <|> placeholder funcDef :: Parser Expr funcDef = do name <- identifier <|> gravis "function name" - args <- some defArg "function arguments" + args <- many defArg "function arguments" symbol "=" FuncDef name args <$> expr "function body" diff --git a/tests/ParserSpec.hs b/tests/ParserSpec.hs index c9a053c..99da369 100644 --- a/tests/ParserSpec.hs +++ b/tests/ParserSpec.hs @@ -21,6 +21,15 @@ parserCompilerFlags = initCompilerFlags{needsMain = False} spec :: Spec spec = do + describe "Function definitions" $ do + it "Should parse a simple function" $ + parseProgram "add a b = a + b" parserCompilerFlags + `shouldBe` Right + (Program [FuncDef{name = "add", args = [Var "a" anyPosition, Var "b" anyPosition], body = Add (Var "a" anyPosition) (Var "b" anyPosition)}]) + it "Should prase a function with 0 parameters" $ + parseProgram "thunk = 1" parserCompilerFlags + `shouldBe` Right + (Program [FuncDef{name = "thunk", args = [], body = IntLit 1}]) describe "compareTypes" $ do it "Should be true for exact matches" $ property $