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

Commit

Permalink
Add unit tests for compute
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Dec 5, 2023
1 parent b21f8b9 commit 9c88c5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
18 changes: 5 additions & 13 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import AST
import Defines

-- Foo = 42
test0 :: Tree
test0 = Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))

test1 :: Tree
test1 = Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))
test1 = Leaf (Symbol "foo")

-- Bar = 21
test2 :: Tree
Expand Down Expand Up @@ -46,15 +49,4 @@ test9 :: Tree
test9 = Node "+" (Just (Node "*" (Just (Leaf (Number 2))) (Just (Leaf (Number 5))))) (Just (Node "/" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 2)))))

main :: IO ()
main = computeAllAST (Env [])
[
test1,
test2,
test3,
test4,
test5,
test6,
test7,
test8,
test9
]
main = putStrLn $ show $ computeAllAST (Env []) [test0, test1, test2, test3, test4, test5, test6, test7, test8, test9]
30 changes: 10 additions & 20 deletions src/ComputeAST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,20 @@ resolveDeepestNode _ _ = (Leaf (Number 0))

------------ COMPUTE TREE ----------

-- Call recursively resolveDeepestNode until the tree is fully resolved (node is a leaf)
computeTree :: Env -> Tree -> IO Env
-- One node remaining symbol
computeTree env (Leaf (Symbol symbol)) = do
let result = getSymbolValue env symbol
putStrLn $ show result
return env
-- One node remaining number
computeTree env (Leaf (Number number)) = do
putStrLn $ show number
return env
-- More than one node remaining, call resolveDeepestNode on the tree
computeTree :: Env -> Tree -> Atom
computeTree env (Leaf (Symbol symbol)) = Number (getSymbolValue env symbol)
computeTree _ (Leaf (Number number)) = Number number
computeTree env tree = computeTree env (resolveDeepestNode env tree)

------------ COMPUTE AST ------------

-- Call appropriate function depending on the node
computeAST :: Env -> Tree -> IO Env
computeAST env tree@(Node "define" _ _) = return $ registerDefine env tree
computeAST env tree = computeTree env tree
computeAST :: Env -> [Atom] -> Tree -> (Env, [Atom])
computeAST env atoms tree@(Node "define" _ _) = (registerDefine env tree, atoms)
computeAST env atoms tree = (env, atoms ++ [computeTree env tree])

-- Call computeAST on every tree in the list
computeAllAST :: Env -> [Tree] -> IO ()
computeAllAST _ [] = return ()
computeAllAST env (tree:rest) = do
updatedEnv <- computeAST env tree
computeAllAST updatedEnv rest
computeAllAST :: Env -> [Tree] -> [Atom]
computeAllAST _ [] = []
computeAllAST env (tree:rest) = atoms ++ computeAllAST newEnv rest
where (newEnv, atoms) = computeAST env [] tree
16 changes: 15 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import Test.Tasty.Runners.Html

import AST
import TextToAST
import ComputeAST
import Defines

main :: IO ()
main = defaultMainWithIngredients (htmlRunner : defaultIngredients) tests

tests :: TestTree
tests = testGroup "Tests" [unitTestsASTEqual, unitTestsASTParse]
tests = testGroup "Tests" [unitTestsASTEqual, unitTestsASTParse, unitTestASTCompute]

unitTestsASTEqual :: TestTree
unitTestsASTEqual = testGroup "AST Equal Tests"
Expand Down Expand Up @@ -150,3 +152,15 @@ unitTestsASTParse = testGroup "AST Parse Tests"
)
(textToAST "(foo def )")
]

unitTestASTCompute :: TestTree
unitTestASTCompute = testGroup "AST compute Tests"
[ testCase "test1" $
assertEqual "number 21 + number 21 = 42"
[Number 42]
(computeAllAST (Env []) [Node "+" (Just (Leaf (Number 21))) (Just (Leaf (Number 21)))])
, testCase "test2" $
assertEqual "define foo 42 and tree with leaf foo"
[Number 42]
(computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Leaf (Symbol "foo"))])
]

0 comments on commit 9c88c5a

Please sign in to comment.