Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Add more tests and Fix bug
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
Saverio976 committed Dec 4, 2023
1 parent 8afc1f9 commit 91c86ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/TextToAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ createVariadic str = Just $ Variadic (textToAST str) (textToAST (nextToParse str

createNodeFromFunction :: Symbol -> String -> Int -> Maybe Tree
createNodeFromFunction [] _ _ = Nothing
createNodeFromFunction (_:xs) [] 0 = Just (Node xs Nothing Nothing)
createNodeFromFunction (_:xs) str 0 = Just (Node xs (textToAST str) Nothing)
createNodeFromFunction _ [] _ = Nothing
createNodeFromFunction (_:xs) _ 0 = Just (Node xs Nothing Nothing)
createNodeFromFunction (_:xs) str 1 = Just (Node xs (textToAST str)
(textToAST (nextToParse str)))
createNodeFromFunction (_:xs) str 2 =
Expand All @@ -73,20 +74,32 @@ createNodeFromFunction _ _ _ = Nothing
stringIsBool :: String -> Bool
stringIsBool "#t" = True
stringIsBool "#f" = True
stringIsBool ('#':'t':xs) | xs == ")" = True
| dropWhile skipableChar xs == ")" = True
stringIsBool ('#':'f':xs) | xs == ")" = True
| dropWhile skipableChar xs == ")" = True
stringIsBool _ = False

createBool :: String -> Maybe Tree
createBool "#t" = Just (Leaf (Boolean True))
createBool ('#':'t':xs) | xs == ")" = Just (Leaf (Boolean True))
| dropWhile skipableChar xs == ")" =
Just (Leaf (Boolean True))
createBool "#f" = Just (Leaf (Boolean False))
createBool ('#':'f':xs) | xs == ")" = Just (Leaf (Boolean False))
| dropWhile skipableChar xs == ")" =
Just (Leaf (Boolean False))
createBool _ = Nothing

treeFromAtom :: String -> String -> Maybe Tree
treeFromAtom [] _ = Nothing
treeFromAtom split str | stringIsNumber split =
Just (Leaf (parseStringNumber split))
| stringIsBool split = createBool split
| isFunction split =
createNodeFromFunction split str (countAtoms (nextToParse str) 0)
| isFunction split = createNodeFromFunction
(takeWhile (/= ')') split)
str
(countAtoms (nextToParse str) 0)
| otherwise = Just (Leaf (Symbol $ takeWhile (/= ')') split))

textToAST :: String -> Maybe Tree
Expand Down
24 changes: 24 additions & 0 deletions test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ unitTestsASTParse = testGroup "AST Parse Tests"
(Just $ Leaf (Boolean True))
)
(textToAST "(foo def #t)")
, testCase "(foo def #f)" $
assertEqual "(foo def #f)"
(Just $ Node "foo"
(Just $ Leaf (Symbol "def"))
(Just $ Leaf (Boolean False))
)
(textToAST "(foo def #f)")
, testCase "(foo #f def)" $
assertEqual "(foo #f def)"
(Just $ Node "foo"
(Just $ Leaf (Boolean False))
(Just $ Leaf (Symbol "def"))
)
(textToAST "(foo #f def)")
, testCase "(foo def #t #f)" $
assertEqual "(foo def #t #f)"
(Just $ Node "foo"
Expand All @@ -98,4 +112,14 @@ unitTestsASTParse = testGroup "AST Parse Tests"
)
)
(textToAST "(foo def #t #f)")
, testCase "(foo def #f #t)" $
assertEqual "(foo def #f #t)"
(Just $ Node "foo"
(Just $ Leaf (Symbol "def"))
(Just $ Variadic
(Just $ Leaf (Boolean False))
(Just $ Leaf (Boolean True))
)
)
(textToAST "(foo def #f #t)")
]

0 comments on commit 91c86ef

Please sign in to comment.