From 7ee62393f935b2554e7657f1999973d75369d79b Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 15:39:52 +0100 Subject: [PATCH 1/6] test(parsing): test for parseCmpString --- LobsterLang/test/ParserSpec.hs | 6 ++++++ exemple/Fibonacci.lob | 13 ++++++++++++- exemple/Lambda.lob | 20 ++++++++++---------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/LobsterLang/test/ParserSpec.hs b/LobsterLang/test/ParserSpec.hs index 7743700..837b656 100644 --- a/LobsterLang/test/ParserSpec.hs +++ b/LobsterLang/test/ParserSpec.hs @@ -184,3 +184,9 @@ spec = do runParser parseExpr (0,0) "! *" `shouldBe` Left (errorParsing (0,2)) it "Check parseExpr Unary Operation Failure (missing operator)" $ do runParser parseExpr (0,0) "error" `shouldBe` Right (AST.Symbol "error" Nothing, "", (0, 5)) + it "Check parseCmpString Success" $ do + runParser (parseCmpString "test") (0,0) "test" `shouldBe` Right ("test", "", (0, 4)) + it "Check parseCmpString Success with remaining string" $ do + runParser (parseCmpString "test") (0,0) "test abc" `shouldBe` Right ("test", "abc", (0, 5)) + it "Check parseCmpString Failure" $ do + runParser (parseCmpString "test") (0,0) "testa abc" `shouldBe` Left (errorParsing (0, 0)) diff --git a/exemple/Fibonacci.lob b/exemple/Fibonacci.lob index 883644c..6ba7ed9 100644 --- a/exemple/Fibonacci.lob +++ b/exemple/Fibonacci.lob @@ -1,4 +1,4 @@ -fn fibonacci(| x: integer |) -> integer { +fn fibonacci(| x |) { if x == 0 { 0 } else if x == 1 { @@ -7,3 +7,14 @@ fn fibonacci(| x: integer |) -> integer { fibonacci(| x - 1 |) + fibonacci(| x - 2 |) } } + +fibonacci(| 1 |) +fibonacci(| 2 |) +fibonacci(| 3 |) +fibonacci(| 4 |) +fibonacci(| 5 |) +fibonacci(| 6 |) +fibonacci(| 7 |) +fibonacci(| 8 |) +fibonacci(| 9 |) +fibonacci(| 10 |) diff --git a/exemple/Lambda.lob b/exemple/Lambda.lob index 5f3e156..d890e5e 100644 --- a/exemple/Lambda.lob +++ b/exemple/Lambda.lob @@ -1,15 +1,15 @@ -sqrt = λ (\ x: integer /) -> integer x * x +sqrt = λ (| x |) {| x * x |} -add = λ (\ a: integer, b: integer /) -> integer a + b +add = λ (| a, b |){| a + b |} -sqrt(\ 5 /) # return 25 -add(\ 2, 9 /) # return 11 - -abs = λ (\ x: integer /) -> integer { - if x < 0 { +sqrt(| 5 |) # return 25 +add(| 2, 9 |) # return 11 + # test +abs = λ (| x |) {| + if x < 0 {| x * -1 - } else { + |} else {| x - } -} + |} +|} From ef230b7b363f236340e1bc345335f12fb80dd3c9 Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 17:17:19 +0100 Subject: [PATCH 2/6] test(parsing): tests for parseFunctionValue --- LobsterLang/test/ParserSpec.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/LobsterLang/test/ParserSpec.hs b/LobsterLang/test/ParserSpec.hs index 3cb7935..92e3073 100644 --- a/LobsterLang/test/ParserSpec.hs +++ b/LobsterLang/test/ParserSpec.hs @@ -190,3 +190,11 @@ spec = do runParser (parseCmpString "test") (0,0) "test abc" `shouldBe` Right ("test", "abc", (0, 5)) it "Check parseCmpString Failure" $ do runParser (parseCmpString "test") (0,0) "testa abc" `shouldBe` Left (errorParsing (0, 0)) + it "Check parseFunctionValue Success" $ do + runParser parseFunctionValue (0,0) "(|a,b|) {| a + b |}" `shouldBe` Right (AST.FunctionValue ["a","b"] (AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) Nothing,"",(0,19)) + it "Check parseFunctionValue Failure missing brackets" $ do + runParser parseFunctionValue (0,0) "(|a,b|) a + b" `shouldBe` Left (errorParsing (0, 8)) + it "Check parseFunctionValue Failure parametters" $ do + runParser parseFunctionValue (0,0) "{| a + b |}" `shouldBe` Left (errorParsing (0, 0)) + it "Check parseFunctionValue Failure empty brackets" $ do + runParser parseFunctionValue (0,0) "(|a,b|) {||}" `shouldBe` Left (errorParsing (0, 10)) From d0581419c07f23c9e99484f3b57ab2ca4deb63b4 Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 17:18:58 +0100 Subject: [PATCH 3/6] fix: example files --- exemple/Factorial.lob | 10 +++++----- exemple/Fibonacci.lob | 12 ++++++------ exemple/Neg.lob | 4 ++-- exemple/Power.lob | 10 +++++----- exemple/RangeToStr.lob | 10 +++++----- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/exemple/Factorial.lob b/exemple/Factorial.lob index 7fa41fd..63d5ee3 100644 --- a/exemple/Factorial.lob +++ b/exemple/Factorial.lob @@ -1,7 +1,7 @@ -fn factorial(| x |) { - if x == 0 { +fn factorial(| x |) {| + if x == 0 {| 1 - } else { + |} else {| x * factorial(| x - 1 |) - } -} + |} +|} diff --git a/exemple/Fibonacci.lob b/exemple/Fibonacci.lob index 6ba7ed9..5a481d4 100644 --- a/exemple/Fibonacci.lob +++ b/exemple/Fibonacci.lob @@ -1,12 +1,12 @@ -fn fibonacci(| x |) { - if x == 0 { +fn fibonacci(| x |) {| + if x == 0 {| 0 - } else if x == 1 { + |} else if x == 1 {| 1 - } else { + |} else {| fibonacci(| x - 1 |) + fibonacci(| x - 2 |) - } -} + |} +|} fibonacci(| 1 |) fibonacci(| 2 |) diff --git a/exemple/Neg.lob b/exemple/Neg.lob index b747f37..cfdce51 100644 --- a/exemple/Neg.lob +++ b/exemple/Neg.lob @@ -1,3 +1,3 @@ -fn neg(| x |) { +fn neg(| x |) {| 0 - x -} +|} diff --git a/exemple/Power.lob b/exemple/Power.lob index 945b588..c9890f7 100644 --- a/exemple/Power.lob +++ b/exemple/Power.lob @@ -1,9 +1,9 @@ -fn power(| a, pow |) { - if pow == 0 { +fn power(| a, pow |) {| + if pow == 0 {| 1 - } else { + |} else {| a * power(| a, pow - 1 |) - } -} + |} +|} power(| 5, 5 |) diff --git a/exemple/RangeToStr.lob b/exemple/RangeToStr.lob index a6c5099..941573f 100644 --- a/exemple/RangeToStr.lob +++ b/exemple/RangeToStr.lob @@ -1,7 +1,7 @@ -fn rangeToStr(| a, b |) { - if a >= b { +fn rangeToStr(| a, b |) {| + if a >= b {| @ b - } else { + |} else {| @ a + rangeToStr(| a + 1, b |) - } -} + |} +|} From 1ce04d39f571bfac470708cee6a7b6d00c271bb7 Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 17:44:29 +0100 Subject: [PATCH 4/6] test(parsing): tests for parseBracket and parseLambda --- LobsterLang/test/ParserSpec.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/LobsterLang/test/ParserSpec.hs b/LobsterLang/test/ParserSpec.hs index 92e3073..6af1abb 100644 --- a/LobsterLang/test/ParserSpec.hs +++ b/LobsterLang/test/ParserSpec.hs @@ -198,3 +198,25 @@ spec = do runParser parseFunctionValue (0,0) "{| a + b |}" `shouldBe` Left (errorParsing (0, 0)) it "Check parseFunctionValue Failure empty brackets" $ do runParser parseFunctionValue (0,0) "(|a,b|) {||}" `shouldBe` Left (errorParsing (0, 10)) + it "Check parseFunctionValue Success no parametter" $ do + runParser parseFunctionValue (0,0) "(||) {|1|}" `shouldBe` Right (AST.FunctionValue [] (AST.Value 1) Nothing, "", (0,10)) + it "Check parseBracket Success" $ do + runParser parseBracket (0,0) "{| a + b |}" `shouldBe` Right ((AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]), "", (0, 11)) + it "Check parseBracket Failure invalid start bracket" $ do + runParser parseBracket (0,0) "{ a + b |}" `shouldBe` Left (errorParsing (0, 1)) + it "Check parseBracket Failure invalid end bracket" $ do + runParser parseBracket (0,0) "{| a + b }" `shouldBe` Left (errorParsing (0, 9)) + it "Check parseBracket Failure empty brackets" $ do + runParser parseBracket (0,0) "{||}" `shouldBe` Left (errorParsing (0, 2)) + it "Check parseLambda Success" $ do + runParser parseLambda (0,0) "lambda(|a,b|) {| a + b |}" `shouldBe` Right (AST.FunctionValue ["a","b"] (AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) Nothing,"",(0,25)) + it "Check parseLambda Success λ" $ do + runParser parseLambda (0,0) "λ(|a,b|) {| a + b |}" `shouldBe` Right (AST.FunctionValue ["a","b"] (AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) Nothing,"",(0,20)) + it "Check parseLambda Failure missing lambda/λ" $ do + runParser parseLambda (0,0) "(|a,b|) {| a + b |}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseLambda Failure invalid keyword" $ do + runParser parseLambda (0,0) "lambdaa(||) {|1|}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseLambda Failure word after λ" $ do + runParser parseLambda (0,0) "λ a(||) {|1|}" `shouldBe` Left (errorParsing (0,2)) + it "Check parseLambda Failure empty brackets" $ do + runParser parseLambda (0,0) "λ(||) {||}" `shouldBe` Left (errorParsing (0,8)) From 472f0659e8e2665e5889d35300cf3d67bbf1189d Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 18:11:15 +0100 Subject: [PATCH 5/6] test(parsing): tests for parseCond --- LobsterLang/src/Parse.hs | 4 +++- LobsterLang/test/ParserSpec.hs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/LobsterLang/src/Parse.hs b/LobsterLang/src/Parse.hs index 6034010..0ac30b6 100644 --- a/LobsterLang/src/Parse.hs +++ b/LobsterLang/src/Parse.hs @@ -39,7 +39,9 @@ module Parse ( errorParsing, parseDefineFn, parseLambda, - parseCond + parseCond, + parseFunctionValue, + parseBracket ) where import qualified AST diff --git a/LobsterLang/test/ParserSpec.hs b/LobsterLang/test/ParserSpec.hs index 6af1abb..5d6cda9 100644 --- a/LobsterLang/test/ParserSpec.hs +++ b/LobsterLang/test/ParserSpec.hs @@ -220,3 +220,21 @@ spec = do runParser parseLambda (0,0) "λ a(||) {|1|}" `shouldBe` Left (errorParsing (0,2)) it "Check parseLambda Failure empty brackets" $ do runParser parseLambda (0,0) "λ(||) {||}" `shouldBe` Left (errorParsing (0,8)) + it "Check parseCond Success single if" $ do + runParser parseCond (0,0) "if a == b {| 1 |}" `shouldBe` Right (AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) (AST.Value 1) Nothing,"",(0,17)) + it "Check parseCond Success if and else" $ do + runParser parseCond (0,0) "if a == b {| 1 |} else {| 0 |}" `shouldBe` Right ((AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) (AST.Value 1) (Just (AST.Value 0))),"",(0,30)) + it "Check parseCond Success if, else if and else" $ do + runParser parseCond (0,0) "if a == b {| 1 |} else if a == 2 {| 2 |} else {| 0 |}" `shouldBe` Right (AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) (AST.Value 1) (Just (AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Value 2]) (AST.Value 2) (Just (Value 0)))),"",(0,53)) + it "Check parseCond Failure invalid expr" $ do + runParser parseCond (0,0) "if a *= 2 {|1|}" `shouldBe` Left (errorParsing (0,5)) + it "Check parseCond Failure no expr" $ do + runParser parseCond (0,0) "if {|1|}" `shouldBe` Left (errorParsing (0,3)) + it "Check parseCond Failure empty brackets" $ do + runParser parseCond (0,0) "if a {||}" `shouldBe` Left (errorParsing (0,7)) + it "Check parseCond Failure no if" $ do + runParser parseCond (0,0) "a == b {||}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseCond Failure no space between if and expr" $ do + runParser parseCond (0,0) "ifa==b {||}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseCond Success ignore error in else" $ do + runParser parseCond (0,0) "if a==b {|1|} else" `shouldBe` Right (AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) (AST.Value 1) Nothing, "else", (0, 14)) From 711406a4cf0c7f03f6d172570f9b5223de648cd3 Mon Sep 17 00:00:00 2001 From: Mathieu MARQUES Date: Sun, 14 Jan 2024 19:12:26 +0100 Subject: [PATCH 6/6] test(parsing): tests for parseComment and parseDefineFn --- LobsterLang/src/Parse.hs | 5 +++-- LobsterLang/test/ParserSpec.hs | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/LobsterLang/src/Parse.hs b/LobsterLang/src/Parse.hs index 0ac30b6..3003e69 100644 --- a/LobsterLang/src/Parse.hs +++ b/LobsterLang/src/Parse.hs @@ -41,7 +41,8 @@ module Parse ( parseLambda, parseCond, parseFunctionValue, - parseBracket + parseBracket, + parseComment ) where import qualified AST @@ -424,7 +425,7 @@ parseComment :: Parser Char parseComment = parseChar '#' *> Parser f where f :: Position -> String -> Either String (Char, String, Position) - f (row, col) ('\n':xs) = Right ('\n', xs, (row + 1, col)) + f (row, _) ('\n':xs) = Right ('\n', xs, (row + 1, 0)) f (row, col) "" = Right ('\n', "", (row, col + 1)) f (row, col) (_:xs) = f (row, col + 1) xs diff --git a/LobsterLang/test/ParserSpec.hs b/LobsterLang/test/ParserSpec.hs index 5d6cda9..d567061 100644 --- a/LobsterLang/test/ParserSpec.hs +++ b/LobsterLang/test/ParserSpec.hs @@ -194,11 +194,11 @@ spec = do runParser parseFunctionValue (0,0) "(|a,b|) {| a + b |}" `shouldBe` Right (AST.FunctionValue ["a","b"] (AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) Nothing,"",(0,19)) it "Check parseFunctionValue Failure missing brackets" $ do runParser parseFunctionValue (0,0) "(|a,b|) a + b" `shouldBe` Left (errorParsing (0, 8)) - it "Check parseFunctionValue Failure parametters" $ do + it "Check parseFunctionValue Failure parameters" $ do runParser parseFunctionValue (0,0) "{| a + b |}" `shouldBe` Left (errorParsing (0, 0)) it "Check parseFunctionValue Failure empty brackets" $ do runParser parseFunctionValue (0,0) "(|a,b|) {||}" `shouldBe` Left (errorParsing (0, 10)) - it "Check parseFunctionValue Success no parametter" $ do + it "Check parseFunctionValue Success no parameter" $ do runParser parseFunctionValue (0,0) "(||) {|1|}" `shouldBe` Right (AST.FunctionValue [] (AST.Value 1) Nothing, "", (0,10)) it "Check parseBracket Success" $ do runParser parseBracket (0,0) "{| a + b |}" `shouldBe` Right ((AST.Call "+" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]), "", (0, 11)) @@ -238,3 +238,27 @@ spec = do runParser parseCond (0,0) "ifa==b {||}" `shouldBe` Left (errorParsing (0,0)) it "Check parseCond Success ignore error in else" $ do runParser parseCond (0,0) "if a==b {|1|} else" `shouldBe` Right (AST.Cond (AST.Call "==" [AST.Symbol "a" Nothing, AST.Symbol "b" Nothing]) (AST.Value 1) Nothing, "else", (0, 14)) + it "Check parseComment Success" $ do + runParser parseComment (0,0) "#fn test(||) {|1|}" `shouldBe` Right ('\n', "", (0, 19)) + it "Check parseComment Failure no #" $ do + runParser parseComment (0,0) "fn test(||) {|1|}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseComment Success with new line" $ do + runParser parseComment (0,0) "#fn test(||) {|1|}\ntest=1" `shouldBe` Right ('\n', "test=1", (1, 0)) + it "Check parseDefineFn Success" $ do + runParser parseDefineFn (0,0) "fn function(|a|) {|a|}" `shouldBe` Right (AST.Define "function" (AST.FunctionValue ["a"] (AST.Symbol "a" Nothing) Nothing),"",(0,22)) + it "Check parseDefineFn Success" $ do + runParser parseDefineFn (0,0) "fn function(|a|) {|a|}" `shouldBe` Right (AST.Define "function" (AST.FunctionValue ["a"] (AST.Symbol "a" Nothing) Nothing),"",(0,22)) + it "Check parseDefineFn Failure no fn" $ do + runParser parseDefineFn (0,0) "function(|a|) {|a|}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseDefineFn Failure no space after fn" $ do + runParser parseDefineFn (0,0) "fnfunction(|a|) {|a|}" `shouldBe` Left (errorParsing (0,0)) + it "Check parseDefineFn Failure no function name" $ do + runParser parseDefineFn (0,0) "fn (|a|) {|a|}" `shouldBe` Left (errorParsing (0,3)) + it "Check parseDefineFn Failure no parameter" $ do + runParser parseDefineFn (0,0) "fn function {|a|}" `shouldBe` Left (errorParsing (0,12)) + it "Check parseDefineFn Failure no brackets" $ do + runParser parseDefineFn (0,0) "fn function(|a|)" `shouldBe` Left (errorParsing (0,16)) + it "Check parseDefineFn Failure invalid parameters" $ do + runParser parseDefineFn (0,0) "fn function(|a,|) {|a|}" `shouldBe` Left (errorParsing (0,15)) + it "Check parseDefineFn Failure empty brackets" $ do + runParser parseDefineFn (0,0) "fn function(|a|) {||}" `shouldBe` Left (errorParsing (0,19))