diff --git a/app/Main.hs b/app/Main.hs index 47297c3..c77d1a0 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,8 +5,5 @@ -- Main -} -import AST -import TextToAST - main :: IO () -main = putStrLn (showMaybeTree (textToAST "(fst 1 (scd 2 3 4) 12)")) +main = putStrLn ("Hello, World!") diff --git a/src/AST.hs b/src/AST.hs index 867d76e..697c38b 100644 --- a/src/AST.hs +++ b/src/AST.hs @@ -8,8 +8,7 @@ module AST ( Symbol, - Atom (Number, Symbol, Boolean), - Tree (Node, Leaf, Variadic, Empty), + Tree (Number, Symbol, Boolean, List), showMaybeTree ) where @@ -17,34 +16,21 @@ import Data.Int (Int64) type Symbol = String -data Atom = Number Int64 | Symbol Symbol | Boolean Bool - -data Tree = Node Symbol (Maybe Tree) (Maybe Tree) | Leaf Atom | Variadic (Maybe Tree) (Maybe Tree) | Empty +data Tree = Number Int64 | Symbol Symbol | Boolean Bool | List [Tree] showMaybeTree :: Maybe Tree -> String showMaybeTree Nothing = "Nothing" showMaybeTree (Just tree) = show tree -instance Eq Atom where +instance Eq Tree where Number a == Number b = a == b Symbol a == Symbol b = a == b Boolean a == Boolean b = a == b + List a == List b = a == b _ == _ = False -instance Show Atom where +instance Show Tree where show (Number a) = "N:'" ++ show a ++ "'" show (Symbol a) = "S:'" ++ a ++ "'" show (Boolean value) = "B: " ++ show value - -instance Eq Tree where - Node a fst_ scd == Node b bfst bscd = a == b && fst_ == bfst && scd == bscd - Leaf a == Leaf b = a == b - Variadic fst_ scd == Variadic bfst bscd = fst_ == bfst && scd == bscd - Empty == Empty = True - _ == _ = False - -instance Show Tree where - show (Node value fst_ scd) = "Node:'" ++ value ++ "' first: '{" ++ showMaybeTree fst_ ++ "} second: {" ++ showMaybeTree scd ++ "}'" - show (Leaf value) = "Leaf:'" ++ show value ++ "'" - show (Variadic fst_ scd) = "Variadic first: {" ++ showMaybeTree fst_ ++ "} second: {" ++ showMaybeTree scd ++ "}" - show (Empty) = "Empty" + show (List list) = "L: " ++ show list diff --git a/src/ComputeAST.hs b/src/ComputeAST.hs index 1d163ab..7b11276 100644 --- a/src/ComputeAST.hs +++ b/src/ComputeAST.hs @@ -7,64 +7,64 @@ module ComputeAST ( - computeAST, - computeAllAST + --computeAST, + --computeAllAST ) where -import AST -import Defines -import Data.Int (Int64) -import Functions +--import AST +--import Data.Int (Int64) +--import Functions ------------ ComputeNode ------------ -computeNode :: Env -> Tree -> Int64 -computeNode env tree@(Node "+" _ _) = additionTree env tree -computeNode env tree@(Node "-" _ _) = substactionTree env tree -computeNode env tree@(Node "*" _ _) = multiplicationTree env tree -computeNode env tree@(Node "div" _ _) = divisionTree env tree -computeNode env tree@(Node "mod" _ _) = moduloTree env tree --- TODO: Error handling -computeNode _ _ = 0 - ------------- Resolve deepest ------------ - -resolveDeepestNode :: Env -> Tree -> Tree --- Node [Leaf] [Leaf] -resolveDeepestNode env (Node symbol (Just (Leaf left)) (Just (Leaf right))) = - Leaf (Number (computeNode env - (Node symbol (Just (Leaf left)) (Just (Leaf right))))) --- Node [Leaf] [Node] -resolveDeepestNode env (Node symbol (Just (Leaf left)) (Just right)) = - Node symbol (Just (Leaf left)) (Just $ resolveDeepestNode env right) --- Node [Node] [Leaf] -resolveDeepestNode env (Node symbol (Just left) (Just (Leaf right))) = - Node symbol (Just $ resolveDeepestNode env left) (Just (Leaf right)) --- Node [Node] [Node] -resolveDeepestNode env (Node symbol (Just left) (Just right)) = - Node symbol (Just $ resolveDeepestNode env left) - (Just $ resolveDeepestNode env right) --- TODO: Error handling -resolveDeepestNode _ _ = (Leaf (Number 0)) - ------------- COMPUTE TREE ---------- - -computeTree :: Env -> Tree -> Atom -computeTree env (Leaf (Symbol symbol)) = Number (getSymbolValue env symbol) -computeTree _ (Leaf (Number number)) = Number number -computeTree _ (Leaf (Boolean value)) = Boolean value -computeTree env tree = computeTree env (resolveDeepestNode env tree) - ------------- COMPUTE AST ------------ - -computeAST :: Env -> Tree -> (Env, Maybe Atom) -computeAST env tree@(Node "define" _ _) = (registerDefine env tree, Nothing) -computeAST env tree = (env, Just (computeTree env tree)) - --- Call computeAST on every tree in the list -computeAllAST :: Env -> [Tree] -> [Atom] -computeAllAST _ [] = [] -computeAllAST env (tree:rest) = case atom' of - Just atom -> atom : computeAllAST newEnv rest - Nothing -> computeAllAST newEnv rest - where (newEnv, atom') = computeAST env tree +--computeNode :: Env -> Tree -> Int64 +--computeNode env tree@(Node "+" _ _) = additionTree env tree +--computeNode env tree@(Node "-" _ _) = substactionTree env tree +--computeNode env tree@(Node "*" _ _) = multiplicationTree env tree +--computeNode env tree@(Node "div" _ _) = divisionTree env tree +--computeNode env tree@(Node "mod" _ _) = moduloTree env tree +---- TODO: Error handling +--computeNode _ _ = 0 +-- +-------------- Resolve deepest ------------ +-- +--resolveDeepestNode :: Env -> Tree -> Tree +---- Node [Leaf] [Leaf] +--resolveDeepestNode env (Node symbol (Just (Leaf left)) (Just (Leaf right))) = +-- Leaf (Number (computeNode env +-- (Node symbol (Just (Leaf left)) (Just (Leaf right))))) +---- Node [Leaf] [Node] +--resolveDeepestNode env (Node symbol (Just (Leaf left)) (Just right)) = +-- Node symbol (Just (Leaf left)) (Just $ resolveDeepestNode env right) +---- Node [Node] [Leaf] +--resolveDeepestNode env (Node symbol (Just left) (Just (Leaf right))) = +-- Node symbol (Just $ resolveDeepestNode env left) (Just (Leaf right)) +---- Node [Node] [Node] +--resolveDeepestNode env (Node symbol (Just left) (Just right)) = +-- Node symbol (Just $ resolveDeepestNode env left) +-- (Just $ resolveDeepestNode env right) +---- TODO: Error handling +--resolveDeepestNode _ _ = (Leaf (Number 0)) +-- +-------------- COMPUTE TREE ---------- +-- +--computeTree :: Env -> Tree -> Atom +--computeTree env (Leaf (Symbol symbol)) = Number (getSymbolValue env symbol) +--computeTree _ (Leaf (Number number)) = Number number +--computeTree _ (Leaf (Boolean value)) = Boolean value +--computeTree env tree = computeTree env (resolveDeepestNode env tree) +-- +-------------- COMPUTE AST ------------ +-- +--computeAST :: Env -> Tree -> (Env, Maybe Atom) +--computeAST env tree@(Node "define" _ _) = (registerDefine env tree, Nothing) +--computeAST env tree = (env, Just (computeTree env tree)) +-- +---- Call computeAST on every tree in the list +--computeAllAST :: Env -> [Tree] -> [Atom] +--computeAllAST _ [] = [] +--computeAllAST env (tree:rest) = case atom' of +-- Just atom -> atom : computeAllAST newEnv rest +-- Nothing -> computeAllAST newEnv rest +-- where (newEnv, atom') = computeAST env tree +-- \ No newline at end of file diff --git a/src/Defines.hs b/src/Defines.hs index 7fe80cd..8f8fea7 100644 --- a/src/Defines.hs +++ b/src/Defines.hs @@ -9,7 +9,7 @@ module Defines ( Define (Define), Env (Env), - registerDefine, + --registerDefine, getSymbolValue ) where @@ -19,7 +19,7 @@ import Data.Int (Int64) -- Define = data Define = Define { symbol :: String, - expression :: Atom + expression :: Tree } deriving (Show) -- used to store defines, and more in the future @@ -39,10 +39,10 @@ getSymbolValue (Env ((Define symbl value):rest)) symbolToFind | otherwise = getSymbolValue (Env rest) symbolToFind -- Register a define in the Defines list -registerDefine :: Env -> Tree -> Env -registerDefine env - (Node "define" - (Just (Leaf (Symbol defSymbol))) - (Just (Leaf defexpression))) - = env { defines = defines env ++ [Define defSymbol defexpression] } -registerDefine env _ = env +--registerDefine :: Env -> Tree -> Env +--registerDefine env +-- (Node "define" +-- (Just (Leaf (Symbol defSymbol))) +-- (Just (Leaf defexpression))) +-- = env { defines = defines env ++ [Define defSymbol defexpression] } +--registerDefine env _ = env diff --git a/src/Functions.hs b/src/Functions.hs index 519f8b7..b19bc57 100644 --- a/src/Functions.hs +++ b/src/Functions.hs @@ -7,76 +7,77 @@ module Functions ( - additionTree, - substactionTree, - multiplicationTree, - divisionTree, - moduloTree + --additionTree, + --substactionTree, + --multiplicationTree, + --divisionTree, + --moduloTree ) where -import AST -import Defines -import Data.Int (Int64) +--import AST +--import Defines +--import Data.Int (Int64) -- Compute a "+ - div * mod" node, using defines if needed -- Todo: See for an error handling and division by 0 -additionTree :: Env -> Tree -> Int64 -additionTree _ (Node "+" (Just (Leaf (Number left))) - (Just (Leaf (Number right)))) = left + right -additionTree env (Node "+" (Just (Leaf (Number left))) - (Just (Leaf (Symbol right)))) = left + getSymbolValue env right -additionTree env (Node "+" (Just (Leaf (Symbol left))) - (Just (Leaf (Number right)))) = getSymbolValue env left + right -additionTree env (Node "+" (Just (Leaf (Symbol left))) - (Just (Leaf (Symbol right)))) = - getSymbolValue env left + getSymbolValue env right -additionTree _ _ = 0 - -substactionTree :: Env -> Tree -> Int64 -substactionTree _ (Node "-" (Just (Leaf (Number left))) - (Just (Leaf (Number right)))) = left - right -substactionTree env (Node "-" (Just (Leaf (Number left))) - (Just (Leaf (Symbol right)))) = left - getSymbolValue env right -substactionTree env (Node "-" (Just (Leaf (Symbol left))) - (Just (Leaf (Number right)))) = getSymbolValue env left - right -substactionTree env (Node "-" (Just (Leaf (Symbol left))) - (Just (Leaf (Symbol right)))) = - getSymbolValue env left - getSymbolValue env right -substactionTree _ _ = 0 - -multiplicationTree :: Env -> Tree -> Int64 -multiplicationTree _ (Node "*" (Just (Leaf (Number left))) - (Just (Leaf (Number right)))) = left * right -multiplicationTree env (Node "*" (Just (Leaf (Number left))) - (Just (Leaf (Symbol right)))) = left * getSymbolValue env right -multiplicationTree env (Node "*" (Just (Leaf (Symbol left))) - (Just (Leaf (Number right)))) = getSymbolValue env left * right -multiplicationTree env (Node "*" (Just (Leaf (Symbol left))) - (Just (Leaf (Symbol right)))) = - getSymbolValue env left * getSymbolValue env right -multiplicationTree _ _ = 0 - -divisionTree :: Env -> Tree -> Int64 -divisionTree _ (Node "div" (Just (Leaf (Number left))) - (Just (Leaf (Number right)))) = left `div` right -divisionTree env (Node "div" (Just (Leaf (Number left))) - (Just (Leaf (Symbol right)))) = left `div` getSymbolValue env right -divisionTree env (Node "div" (Just (Leaf (Symbol left))) - (Just (Leaf (Number right)))) = getSymbolValue env left `div` right -divisionTree env (Node "div" (Just (Leaf (Symbol left))) - (Just (Leaf (Symbol right)))) = - getSymbolValue env left `div` getSymbolValue env right -divisionTree _ _ = 0 - -moduloTree :: Env -> Tree -> Int64 -moduloTree _ (Node "mod" (Just (Leaf (Number left))) - (Just (Leaf (Number right)))) = left `mod` right -moduloTree env (Node "mod" (Just (Leaf (Number left))) - (Just (Leaf (Symbol right)))) = left `mod` getSymbolValue env right -moduloTree env (Node "mod" (Just (Leaf (Symbol left))) - (Just (Leaf (Number right)))) = getSymbolValue env left `mod` right -moduloTree env (Node "mod" (Just (Leaf (Symbol left))) - (Just (Leaf (Symbol right)))) = - getSymbolValue env left `mod` getSymbolValue env right -moduloTree _ _ = 0 +--additionTree :: Env -> Tree -> Int64 +--additionTree _ (Node "+" (Just (Leaf (Number left))) +-- (Just (Leaf (Number right)))) = left + right +--additionTree env (Node "+" (Just (Leaf (Number left))) +-- (Just (Leaf (Symbol right)))) = left + getSymbolValue env right +--additionTree env (Node "+" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Number right)))) = getSymbolValue env left + right +--additionTree env (Node "+" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Symbol right)))) = +-- getSymbolValue env left + getSymbolValue env right +--additionTree _ _ = 0 +-- +--substactionTree :: Env -> Tree -> Int64 +--substactionTree _ (Node "-" (Just (Leaf (Number left))) +-- (Just (Leaf (Number right)))) = left - right +--substactionTree env (Node "-" (Just (Leaf (Number left))) +-- (Just (Leaf (Symbol right)))) = left - getSymbolValue env right +--substactionTree env (Node "-" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Number right)))) = getSymbolValue env left - right +--substactionTree env (Node "-" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Symbol right)))) = +-- getSymbolValue env left - getSymbolValue env right +--substactionTree _ _ = 0 +-- +--multiplicationTree :: Env -> Tree -> Int64 +--multiplicationTree _ (Node "*" (Just (Leaf (Number left))) +-- (Just (Leaf (Number right)))) = left * right +--multiplicationTree env (Node "*" (Just (Leaf (Number left))) +-- (Just (Leaf (Symbol right)))) = left * getSymbolValue env right +--multiplicationTree env (Node "*" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Number right)))) = getSymbolValue env left * right +--multiplicationTree env (Node "*" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Symbol right)))) = +-- getSymbolValue env left * getSymbolValue env right +--multiplicationTree _ _ = 0 +-- +--divisionTree :: Env -> Tree -> Int64 +--divisionTree _ (Node "div" (Just (Leaf (Number left))) +-- (Just (Leaf (Number right)))) = left `div` right +--divisionTree env (Node "div" (Just (Leaf (Number left))) +-- (Just (Leaf (Symbol right)))) = left `div` getSymbolValue env right +--divisionTree env (Node "div" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Number right)))) = getSymbolValue env left `div` right +--divisionTree env (Node "div" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Symbol right)))) = +-- getSymbolValue env left `div` getSymbolValue env right +--divisionTree _ _ = 0 +-- +--moduloTree :: Env -> Tree -> Int64 +--moduloTree _ (Node "mod" (Just (Leaf (Number left))) +-- (Just (Leaf (Number right)))) = left `mod` right +--moduloTree env (Node "mod" (Just (Leaf (Number left))) +-- (Just (Leaf (Symbol right)))) = left `mod` getSymbolValue env right +--moduloTree env (Node "mod" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Number right)))) = getSymbolValue env left `mod` right +--moduloTree env (Node "mod" (Just (Leaf (Symbol left))) +-- (Just (Leaf (Symbol right)))) = +-- getSymbolValue env left `mod` getSymbolValue env right +--moduloTree _ _ = 0 +-- \ No newline at end of file diff --git a/src/TextToAST.hs b/src/TextToAST.hs index 9f2783f..4907d5d 100644 --- a/src/TextToAST.hs +++ b/src/TextToAST.hs @@ -7,136 +7,133 @@ module TextToAST ( - textToAST + --textToAST ) where -import AST -import Data.Int (Int64) -import Data.Char (isDigit) - -isFunction :: String -> Bool -isFunction [] = False -isFunction ('(':_) = True -isFunction _ = False - -skipableChar :: Char -> Bool -skipableChar ' ' = True -skipableChar '\t' = True -skipableChar _ = False - -notSkipableChar :: Char -> Bool -notSkipableChar x = not (skipableChar x) - -stringIsNumber' :: String -> Bool -stringIsNumber' [] = False -stringIsNumber' [x] | Data.Char.isDigit x = True - | otherwise = False -stringIsNumber' (x:xs) | Data.Char.isDigit x = stringIsNumber' xs - | otherwise = False - -stringIsNumber :: String -> Bool -stringIsNumber [] = False -stringIsNumber [x] | Data.Char.isDigit x = True - | otherwise = False -stringIsNumber str = - stringIsNumber' (takeWhile (\x -> notSkipableChar x && x /= ')') str) - -parseStringNumber :: String -> Atom -parseStringNumber str = - Number (read - (takeWhile (\x -> notSkipableChar x && x /= ')') str) - :: Data.Int.Int64) - -popBackPrths :: String -> String -popBackPrths [] = [] -popBackPrths [')'] = [] -popBackPrths (x:xs) = (x:popBackPrths xs) - -nextToParse' :: String -> Int -> String -nextToParse' [] _ = [] -nextToParse' (')':xs) 1 = dropWhile (\x -> skipableChar x || x == ')') xs -nextToParse' (')':xs) depth = nextToParse' xs (depth - 1) -nextToParse' ('(':xs) depth = nextToParse' xs (depth + 1) -nextToParse' (_:xs) depth = nextToParse' xs depth - -cutAtClose :: String -> String -cutAtClose [] = [] -cutAtClose (')':_) = [] -cutAtClose (x:xs) = (x:cutAtClose xs) - -nextToParse :: String -> String -nextToParse [] = [] -nextToParse ('(':xs) = nextToParse' xs 1 -nextToParse str | skipableChar (head str) = nextToParse - (dropWhile skipableChar str) - | (last str) == ')' = nextToParse (popBackPrths str) - | otherwise = dropWhile skipableChar - (dropWhile notSkipableChar (dropWhile skipableChar str)) - -countAtoms :: String -> Int -> Int -countAtoms str depth | depth >= 2 = 2 - | not (null $ takeWhile - (/= ')') - (dropWhile skipableChar str)) = - countAtoms (nextToParse str) (depth + 1) - | otherwise = depth - -createVariadic :: String -> Maybe Tree -createVariadic str = - Just $ Variadic (textToAST str) (textToAST (nextToParse str)) - -createNodeFromFunction :: Symbol -> String -> String -> Int -> Maybe Tree -createNodeFromFunction [] _ _ _ = Nothing -createNodeFromFunction (_:xs) [] _ 0 = Just (Leaf (Symbol xs)) -createNodeFromFunction (_:xs) str _ 0 = Just (Node xs (textToAST str) - (Just Empty)) -createNodeFromFunction _ [] _ _ = Nothing -createNodeFromFunction (_:xs) str tail_ 1 = Just (Node xs (textToAST str) - (textToAST tail_)) -createNodeFromFunction (_:xs) str tail_ 2 = - Just (Node xs (textToAST str) (createVariadic tail_)) -createNodeFromFunction _ _ _ _ = Nothing - -stringIsBool :: String -> Bool -stringIsBool "#t" = True -stringIsBool "#f" = True -stringIsBool "#f)" = True -stringIsBool "#t)" = True -stringIsBool ('#':'t':xs) | dropWhile skipableChar xs == ")" = True - | otherwise = False -stringIsBool ('#':'f':xs) | dropWhile skipableChar xs == ")" = True - | otherwise = False -stringIsBool _ = False - -createBool :: String -> Maybe Tree -createBool "#f" = Just (Leaf (Boolean False)) -createBool "#t" = Just (Leaf (Boolean True)) -createBool "#f)" = Just (Leaf (Boolean False)) -createBool "#t)" = Just (Leaf (Boolean True)) -createBool ('#':'t':xs) | dropWhile skipableChar xs == ")" = - Just (Leaf (Boolean True)) - | otherwise = Nothing -createBool ('#':'f':xs) | dropWhile skipableChar xs == ")" = - Just (Leaf (Boolean False)) - | otherwise = Nothing -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 - (takeWhile (/= ')') split) - (cutAtClose str) - (nextToParse str) - (countAtoms (nextToParse str) 0) - | otherwise = - Just (Leaf (Symbol $ takeWhile (/= ')') split)) - -textToAST :: String -> Maybe Tree -textToAST [] = Nothing -textToAST (x:xs) | skipableChar x = textToAST xs - | otherwise = treeFromAtom - (takeWhile notSkipableChar (x:xs)) - (dropWhile notSkipableChar (x:xs)) +--isFunction :: String -> Bool +--isFunction [] = False +--isFunction ('(':_) = True +--isFunction _ = False +-- +--skipableChar :: Char -> Bool +--skipableChar ' ' = True +--skipableChar '\t' = True +--skipableChar _ = False +-- +--notSkipableChar :: Char -> Bool +--notSkipableChar x = not (skipableChar x) +-- +--stringIsNumber' :: String -> Bool +--stringIsNumber' [] = False +--stringIsNumber' [x] | Data.Char.isDigit x = True +-- | otherwise = False +--stringIsNumber' (x:xs) | Data.Char.isDigit x = stringIsNumber' xs +-- | otherwise = False +-- +--stringIsNumber :: String -> Bool +--stringIsNumber [] = False +--stringIsNumber [x] | Data.Char.isDigit x = True +-- | otherwise = False +--stringIsNumber str = +-- stringIsNumber' (takeWhile (\x -> notSkipableChar x && x /= ')') str) +-- +--parseStringNumber :: String -> Atom +--parseStringNumber str = +-- Number (read +-- (takeWhile (\x -> notSkipableChar x && x /= ')') str) +-- :: Data.Int.Int64) +-- +--popBackPrths :: String -> String +--popBackPrths [] = [] +--popBackPrths [')'] = [] +--popBackPrths (x:xs) = (x:popBackPrths xs) +-- +--nextToParse' :: String -> Int -> String +--nextToParse' [] _ = [] +--nextToParse' (')':xs) 1 = dropWhile (\x -> skipableChar x || x == ')') xs +--nextToParse' (')':xs) depth = nextToParse' xs (depth - 1) +--nextToParse' ('(':xs) depth = nextToParse' xs (depth + 1) +--nextToParse' (_:xs) depth = nextToParse' xs depth +-- +--cutAtClose :: String -> String +--cutAtClose [] = [] +--cutAtClose (')':_) = [] +--cutAtClose (x:xs) = (x:cutAtClose xs) +-- +--nextToParse :: String -> String +--nextToParse [] = [] +--nextToParse ('(':xs) = nextToParse' xs 1 +--nextToParse str | skipableChar (head str) = nextToParse +-- (dropWhile skipableChar str) +-- | (last str) == ')' = nextToParse (popBackPrths str) +-- | otherwise = dropWhile skipableChar +-- (dropWhile notSkipableChar (dropWhile skipableChar str)) +-- +--countAtoms :: String -> Int -> Int +--countAtoms str depth | depth >= 2 = 2 +-- | not (null $ takeWhile +-- (/= ')') +-- (dropWhile skipableChar str)) = +-- countAtoms (nextToParse str) (depth + 1) +-- | otherwise = depth +-- +--createVariadic :: String -> Maybe Tree +--createVariadic str = +-- Just $ Variadic (textToAST str) (textToAST (nextToParse str)) +-- +--createNodeFromFunction :: Symbol -> String -> String -> Int -> Maybe Tree +--createNodeFromFunction [] _ _ _ = Nothing +--createNodeFromFunction (_:xs) [] _ 0 = Just (Leaf (Symbol xs)) +--createNodeFromFunction (_:xs) str _ 0 = Just (Node xs (textToAST str) +-- (Just Empty)) +--createNodeFromFunction _ [] _ _ = Nothing +--createNodeFromFunction (_:xs) str tail_ 1 = Just (Node xs (textToAST str) +-- (textToAST tail_)) +--createNodeFromFunction (_:xs) str tail_ 2 = +-- Just (Node xs (textToAST str) (createVariadic tail_)) +--createNodeFromFunction _ _ _ _ = Nothing +-- +--stringIsBool :: String -> Bool +--stringIsBool "#t" = True +--stringIsBool "#f" = True +--stringIsBool "#f)" = True +--stringIsBool "#t)" = True +--stringIsBool ('#':'t':xs) | dropWhile skipableChar xs == ")" = True +-- | otherwise = False +--stringIsBool ('#':'f':xs) | dropWhile skipableChar xs == ")" = True +-- | otherwise = False +--stringIsBool _ = False +-- +--createBool :: String -> Maybe Tree +--createBool "#f" = Just (Leaf (Boolean False)) +--createBool "#t" = Just (Leaf (Boolean True)) +--createBool "#f)" = Just (Leaf (Boolean False)) +--createBool "#t)" = Just (Leaf (Boolean True)) +--createBool ('#':'t':xs) | dropWhile skipableChar xs == ")" = +-- Just (Leaf (Boolean True)) +-- | otherwise = Nothing +--createBool ('#':'f':xs) | dropWhile skipableChar xs == ")" = +-- Just (Leaf (Boolean False)) +-- | otherwise = Nothing +--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 +-- (takeWhile (/= ')') split) +-- (cutAtClose str) +-- (nextToParse str) +-- (countAtoms (nextToParse str) 0) +-- | otherwise = +-- Just (Leaf (Symbol $ takeWhile (/= ')') split)) +-- +--textToAST :: String -> Maybe Tree +--textToAST [] = Nothing +--textToAST (x:xs) | skipableChar x = textToAST xs +-- | otherwise = treeFromAtom +-- (takeWhile notSkipableChar (x:xs)) +-- (dropWhile notSkipableChar (x:xs)) +-- \ No newline at end of file diff --git a/test/Spec.hs b/test/Spec.hs index e734185..e5f07de 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -3,245 +3,145 @@ import Test.Tasty.HUnit import Test.Tasty.Runners.Html import AST -import TextToAST -import ComputeAST -import Defines +--import TextToAST +--import ComputeAST +--import Defines main :: IO () main = defaultMainWithIngredients (htmlRunner : defaultIngredients) tests tests :: TestTree -tests = testGroup "Tests" [unitTestsASTEqual, unitTestsASTParse, unitTestASTCompute] +tests = testGroup "Tests" [unitTestsASTEqual]--, unitTestsASTParse, unitTestASTCompute] unitTestsASTEqual :: TestTree unitTestsASTEqual = testGroup "AST Equal Tests" [ testCase "Basic AST creation 0" $ assertEqual "define x 42" - (Node "define" (Just $ Leaf (Symbol "x")) (Just $ Leaf (Number 42))) - (Node "define" (Just $ Leaf (Symbol "x")) (Just $ Leaf (Number 42))) + (List [Symbol "define", Symbol "x", Number 42]) + (List [Symbol "define", Symbol "x", Number 42]) , testCase "Basic AST creation 1" $ assertEqual "foo" - (Leaf (Symbol "foo")) - (Leaf (Symbol "foo")) + (Symbol "foo") + (Symbol "foo") , testCase "Basic AST creation 2" $ assertEqual "42" - (Leaf (Number 42)) - (Leaf (Number 42)) + (Number 42) + (Number 42) , testCase "Basic AST creation 3" $ assertEqual "#f" - (Leaf (Boolean False)) - (Leaf (Boolean False)) + (Boolean False) + (Boolean False) , testCase "Basic AST creation 4" $ assertEqual "#t" - (Leaf (Boolean True)) - (Leaf (Boolean True)) + (Boolean True) + (Boolean True) ] -unitTestsASTParse :: TestTree -unitTestsASTParse = testGroup "AST Parse Tests" - [ testCase "(foo abc def hij)" $ - assertEqual "(foo abc def hij)" - (Just $ Node "foo" - (Just $ Leaf (Symbol "abc")) - (Just $ Variadic - (Just $ Leaf (Symbol "def")) - (Just $ Leaf (Symbol "hij")) - ) - ) - (textToAST "(foo abc def hij)") - , testCase "(define x 42)" $ - assertEqual "(define x 42)" - (Just $ Node "define" - (Just $ Leaf (Symbol "x")) - (Just $ Leaf (Number 42)) - ) - (textToAST "(define x 42)") - , testCase "42" $ - assertEqual "42" - (Just $ Leaf (Number 42)) - (textToAST "42") - , testCase "#f" $ - assertEqual "#f" - (Just $ Leaf (Boolean False)) - (textToAST "#f") - , testCase "#t" $ - assertEqual "#t" - (Just $ Leaf (Boolean True)) - (textToAST "#t") - , testCase "foo" $ - assertEqual "foo" - (Just $ Leaf (Symbol "foo")) - (textToAST "foo") - , testCase "(foo)" $ - assertEqual "(foo)" - (Just $ Leaf (Symbol "foo")) - (textToAST "(foo)") - , testCase "(foo def)" $ - assertEqual "(foo def)" - (Just $ Node "foo" - (Just $ Leaf (Symbol "def")) - (Just $ Empty) - ) - (textToAST "(foo def)") - , testCase "(foo def #t)" $ - assertEqual "(foo def #t)" - (Just $ Node "foo" - (Just $ Leaf (Symbol "def")) - (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" - (Just $ Leaf (Symbol "def")) - (Just $ Variadic - (Just $ Leaf (Boolean True)) - (Just $ Leaf (Boolean False)) - ) - ) - (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)") - , testCase "(fst 1 (scd 2 3 4))" $ - assertEqual "(fst 1 (scd 2 3 4))" - (Just $ Node "fst" - (Just $ Leaf (Number 1)) - (Just $ Node "scd" - (Just $ Leaf (Number 2)) - (Just $ Variadic - (Just $ Leaf (Number 3)) - (Just $ Leaf (Number 4)) - ) - ) - ) - (textToAST "(fst 1 (scd 2 3 4))") - , testCase "(fst 1 (scd 2 3 4) 12)" $ - assertEqual "(fst 1 (scd 2 3 4) 12)" - (Just $ Node "fst" - (Just $ Leaf (Number 1)) - (Just $ Variadic - (Just $ Node "scd" - (Just $ Leaf (Number 2)) - (Just $ Variadic - (Just $ Leaf (Number 3)) - (Just $ Leaf (Number 4)) - ) - ) - (Just $ Leaf (Number 12)) - ) - ) - (textToAST "(fst 1 (scd 2 3 4) 12)") - , testCase "(foo 42 )" $ - assertEqual "(foo 42 )" - (Just $ Node "foo" - (Just $ Leaf (Number 42)) - (Just $ Empty) - ) - (textToAST "(foo 42 )") - , testCase "(foo def )" $ - assertEqual "(foo def )" - (Just $ Node "foo" - (Just $ Leaf (Symbol "def")) - (Just $ Empty) - ) - (textToAST "(foo def )") - , testCase "(foo ((def)) #t)" $ - assertEqual "(foo ((def)) #t)" - (Just $ Node "foo" - (Just $ Leaf (Symbol "(def")) - (Just $ Leaf (Boolean True)) - ) - (textToAST "(foo ((def)) #t)") - , testCase "(do (re (mi)) 12)" $ - assertEqual "(do (re (mi)) 12)" - (Just $ Node "do" - (Just $ Node "re" - (Just $ Leaf (Symbol "mi")) - (Just $ Empty) - ) - (Just $ Leaf (Number 12)) - ) - (textToAST "(do (re (mi)) 12)") - , testCase "(do (re (mi)) 12 (re (mi)))" $ - assertEqual "(do (re (mi)) 12 (re (mi)))" - (Just $ Node "do" - (Just $ Node "re" - (Just $ Leaf (Symbol "mi")) - (Just $ Empty) - ) - (Just $ Variadic - (Just $ Leaf (Number 12)) - (Just $ Node "re" - (Just $ Leaf (Symbol "mi")) - (Just $ Empty) - ) - ) - ) - (textToAST "(do (re (mi)) 12 (re (mi)))") - ] - -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"))]) - , testCase "test3" $ - assertEqual "define foo 42 and do foo + 42" - [Number 84] - (computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42))))]) - , testCase "test3" $ - assertEqual "define foo 42 and do 42 + foo" - [Number 84] - (computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Number 42))) (Just (Leaf (Symbol "foo"))))]) - , testCase "test5" $ - assertEqual "define foo 42 and do foo + foo" - [Number 84] - (computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Symbol "foo"))))]) - , testCase "test6" $ - assertEqual "define foo 42 and bar 21 and do foo + bar" - [Number 63] - (computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), (Node "define" (Just (Leaf (Symbol "bar"))) (Just (Leaf (Number 21)))), (Node "+" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Symbol "bar"))))]) - , testCase "test7" $ - assertEqual "2 + (5 * 2) (result = 12)" - [Number 12] - (computeAllAST (Env []) [(Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2))))))]) - , testCase "test8" $ - assertEqual "(2 * 5) + (foo / 2) (result = 10 + 21 = 31)" - [Number 31] - (computeAllAST (Env []) [(Node "define" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 42)))), Node "+" (Just (Node "*" (Just (Leaf (Number 2))) (Just (Leaf (Number 5))))) (Just (Node "div" (Just (Leaf (Symbol "foo"))) (Just (Leaf (Number 2)))))]) - , testCase "test9" $ - assertEqual "2 + 2 + (5 * 2) (result = 14)" - [Number 14] - (computeAllAST (Env []) [(Node "+" (Just (Leaf (Number 2))) (Just (Node "+" (Just (Leaf (Number 2))) (Just (Node "*" (Just (Leaf (Number 5))) (Just (Leaf (Number 2))))))))]) - , testCase "test10" $ - assertEqual "14 mod 5 (result = 4)" - [Number 4] - (computeAllAST (Env []) [(Node "mod" (Just (Leaf (Number 14))) (Just (Leaf (Number 5))))]) - ] +--unitTestsASTParse :: TestTree +--unitTestsASTParse = testGroup "AST Parse Tests" +-- [ testCase "(foo abc def hij)" $ +-- assertEqual "(foo abc def hij)" +-- (textToAST "(foo abc def hij)") +-- , testCase "(define x 42)" $ +-- assertEqual "(define x 42)" +-- (textToAST "(define x 42)") +-- , testCase "42" $ +-- assertEqual "42" +-- (textToAST "42") +-- , testCase "#f" $ +-- assertEqual "#f" +-- (textToAST "#f") +-- , testCase "#t" $ +-- assertEqual "#t" +-- (textToAST "#t") +-- , testCase "foo" $ +-- assertEqual "foo" +-- (textToAST "foo") +-- , testCase "(foo)" $ +-- assertEqual "(foo)" +-- (textToAST "(foo)") +-- , testCase "(foo def)" $ +-- assertEqual "(foo def)" +-- (textToAST "(foo def)") +-- , testCase "(foo def #t)" $ +-- assertEqual "(foo def #t)" +-- (textToAST "(foo def #t)") +-- , testCase "(foo def #f)" $ +-- assertEqual "(foo def #f)" +-- (textToAST "(foo def #f)") +-- , testCase "(foo #f def)" $ +-- assertEqual "(foo #f def)" +-- (textToAST "(foo #f def)") +-- , testCase "(foo def #t #f)" $ +-- assertEqual "(foo def #t #f)" +-- (textToAST "(foo def #t #f)") +-- , testCase "(foo def #f #t)" $ +-- assertEqual "(foo def #f #t)" +-- (textToAST "(foo def #f #t)") +-- , testCase "(fst 1 (scd 2 3 4))" $ +-- assertEqual "(fst 1 (scd 2 3 4))" +-- (textToAST "(fst 1 (scd 2 3 4))") +-- , testCase "(fst 1 (scd 2 3 4) 12)" $ +-- assertEqual "(fst 1 (scd 2 3 4) 12)" +-- (textToAST "(fst 1 (scd 2 3 4) 12)") +-- , testCase "(foo 42 )" $ +-- assertEqual "(foo 42 )" +-- (textToAST "(foo 42 )") +-- , testCase "(foo def )" $ +-- assertEqual "(foo def )" +-- (textToAST "(foo def )") +-- , testCase "(foo ((def)) #t)" $ +-- assertEqual "(foo ((def)) #t)" +-- (textToAST "(foo ((def)) #t)") +-- , testCase "(do (re (mi)) 12)" $ +-- assertEqual "(do (re (mi)) 12)" +-- (textToAST "(do (re (mi)) 12)") +-- , testCase "(do (re (mi)) 12 (re (mi)))" $ +-- assertEqual "(do (re (mi)) 12 (re (mi)))" +-- (textToAST "(do (re (mi)) 12 (re (mi)))") +-- ] +-- +--unitTestASTCompute :: TestTree +--unitTestASTCompute = testGroup "AST compute Tests" +-- [ testCase "test1" $ +-- assertEqual "number 21 + number 21 = 42" +-- [Number 42] +-- computeAllAST (Env []) [(List [Symbol "+", Number 21, Number 21])] +-- , testCase "test2" $ +-- assertEqual "define foo 42 and tree with Symbol foo" +-- [Number 42] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (Symbol "foo")] +-- , testCase "test3" $ +-- assertEqual "define foo 42 and do foo + 42" +-- [Number 84] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (List [Symbol "+", Symbol "foo", Number 42])] +-- , testCase "test3" $ +-- assertEqual "define foo 42 and do 42 + foo" +-- [Number 84] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (List [Symbol "+", Number 42, Symbol "foo")])] +-- , testCase "test5" $ +-- assertEqual "define foo 42 and do foo + foo" +-- [Number 84] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (List [Symbol "+", Symbol "foo", Symbol "foo"])] +-- , testCase "test6" $ +-- assertEqual "define foo 42 and bar 21 and do foo + bar" +-- [Number 63] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (List [Symbol "define", Symbol "bar", Number 21]), (List [Symbol "+", Symbol "foo", Symbol "bar"])] +-- , testCase "test7" $ +-- assertEqual "2 + (5 * 2) (result = 12)" +-- [Number 12] +-- computeAllAST (Env []) [(List [Symbol "+", Number 2, List [Symbol "*", Number 5, Number 2])])] +-- , testCase "test8" $ +-- assertEqual "define foo 42 and (2 * 5) + (foo / 2) (result = 10 + 21 = 31)" +-- [Number 31] +-- computeAllAST (Env []) [(List [Symbol "define", Symbol "foo", Number 42]), (List [Symbol "+", List [Symbol "*", Number 2, Number 5]), List [Symbol "/", Symbol "foo", Number 2])])] +-- , testCase "test9" $ +-- assertEqual "2 + 2 + (5 * 2) (result = 14)" +-- [Number 14] +-- computeAllAST (Env []) [(List [Symbol "+", List [Symbol "+", Number 2, Number 2]), List [Symbol "*", Number 5, Number 2])])] +-- , testCase "test10" $ +-- assertEqual "14 mod 5 (result = 4)" +-- [Number 4] +-- computeAllAST (Env []) [(List [Symbol "mod", Number 14, Number 5])] +-- ] +-- \ No newline at end of file