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

Commit

Permalink
Fix last part of variadic
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
Saverio976 committed Dec 4, 2023
1 parent c21ab8f commit d2414a3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ tags

.idea/*

/koaky
/koaky-exe
/glados

*.log
Expand Down
13 changes: 10 additions & 3 deletions src/TextToAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ stringIsNumber [x] | Data.Char.isDigit x = True
stringIsNumber (x:xs) | Data.Char.isDigit x = stringIsNumber xs
| otherwise = False

nextToParse' :: String -> Int -> String
nextToParse' [] _ = []
nextToParse' (')':xs) 1 = dropWhile skipableChar xs
nextToParse' (')':xs) depth = nextToParse' xs (depth - 1)
nextToParse' ('(':xs) depth = nextToParse' xs (depth + 1)
nextToParse' (_:xs) depth = nextToParse' xs depth

nextToParse :: String -> String
nextToParse [] = []
nextToParse ('(':xs) = xs
nextToParse ('(':xs) = nextToParse' xs 0
nextToParse str = dropWhile skipableChar (dropWhile notSkipableChar (dropWhile skipableChar str))

countAtoms :: String -> Int -> Int
Expand All @@ -52,7 +59,7 @@ createNodeFromFunction [] _ _ = Nothing
createNodeFromFunction _ [] _ = Nothing
createNodeFromFunction (_:xs) _ 0 = Just (Node xs Nothing Nothing)
createNodeFromFunction (_:xs) str 1 = Just (Node xs (textToAST str)
(checkNextToParse textToAST (nextToParse str)))
(textToAST (nextToParse str)))
createNodeFromFunction (_:xs) str 2 =
Just (Node xs (textToAST str) (createVariadic (nextToParse str)))
createNodeFromFunction _ _ _ = Nothing
Expand All @@ -74,7 +81,7 @@ treeFromAtom split _ | stringIsNumber split =
| stringIsBool split = createBool split
treeFromAtom split str | isFunction split =
createNodeFromFunction split str (countAtoms (nextToParse str) 0)
| otherwise = Just (Leaf (Symbol split))
| otherwise = Just (Leaf (Symbol $ takeWhile (/= ')') split))

textToAST :: String -> Maybe Tree
textToAST [] = Nothing
Expand Down
2 changes: 1 addition & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ unitTestsASTEqual = testGroup "AST Equal Tests"
unitTestsASTParse :: TestTree
unitTestsASTParse = testGroup "AST Parse Tests"
[ testCase "Basic AST creation 0" $
assertEqual (textToAST "(foo abc def hij)") (Just $ (Node "foo" (Leaf (Symbol "abc"))) (Varidadic (Leaf (Symbol "def") (Leaf (Symbol "hij")))))
assertEqual "(foo abc def hij)" (textToAST "(foo abc def hij)") (Just $ (Node "foo" (Just $ Leaf (Symbol "abc"))) (Just $ Variadic (Just $ Leaf (Symbol "def")) (Just $ Leaf (Symbol "hij"))))
]

0 comments on commit d2414a3

Please sign in to comment.