Skip to content

Commit

Permalink
Merge branch 'main' into feature/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tzhengtek authored Jan 14, 2024
2 parents ac0f36c + 6c3f3ae commit 2a5fcfa
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
7 changes: 5 additions & 2 deletions LobsterLang/src/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ module Parse (
errorParsing,
parseDefineFn,
parseLambda,
parseCond
parseCond,
parseFunctionValue,
parseBracket,
parseComment
) where

import qualified AST
Expand Down Expand Up @@ -422,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

Expand Down
78 changes: 78 additions & 0 deletions LobsterLang/test/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,81 @@ spec = do
runParser parseExpr (0,0) "! *" `shouldBe` Left (errorParsing (0,0))
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))
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 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 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))
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))
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))
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))
13 changes: 12 additions & 1 deletion exemple/Fibonacci.lob
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
fn fibonacci(| x |) {|
if true {|
if x == 0 {|
0
|} else if x == 1 {|
1
|} else {|
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 |)
4 changes: 2 additions & 2 deletions exemple/Lambda.lob
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ sqrt = λ (| x |) {| x * x |}

add = λ (| a, b |) {| a + b |}

sqrt(| 5 |)
sqrt(| 5 |) # return 25

add(| 2, 9 |)
add(| 2, 9 |) # return 11

abs = λ (| x |) {|
if x < 0 {|
Expand Down

0 comments on commit 2a5fcfa

Please sign in to comment.