aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2022-03-03 15:56:53 +0100
committerMarvin Borner2022-03-03 15:56:53 +0100
commit4d5aa27a4636abcf58afeec83e598118eb02fb5c (patch)
treeead37ffe2b89e2f692a71672c4ec1fab00487b06
parent65293eedaf3469bb0f4a0b174cd53bb89c762ff5 (diff)
Tree
-rw-r--r--src/Fun/Compiler.hs4
-rw-r--r--src/Fun/Grammar.hs3
-rw-r--r--src/Fun/Parser.hs11
-rw-r--r--src/Fun/Tree.hs33
4 files changed, 9 insertions, 42 deletions
diff --git a/src/Fun/Compiler.hs b/src/Fun/Compiler.hs
index b53ff73..46cfe13 100644
--- a/src/Fun/Compiler.hs
+++ b/src/Fun/Compiler.hs
@@ -23,8 +23,8 @@ traceTree c ts = foldr join "" (map (traceBranch c) ts)
genTrace :: [Trace] -> String
genTrace ts = "Trace of expectance:\n" ++ traceTree 0 ts
-parse :: String -> Either String Program -- TODO: Should be tree
-parse file = case program file of
+parse :: String -> Either String Tree
+parse file = case tree file of
Left a -> Left $ "Parse fault!\n" ++ case a of
State [] Nothing -> "No context available"
State t Nothing -> genTrace t
diff --git a/src/Fun/Grammar.hs b/src/Fun/Grammar.hs
index 2b46418..8014a20 100644
--- a/src/Fun/Grammar.hs
+++ b/src/Fun/Grammar.hs
@@ -3,8 +3,9 @@ module Fun.Grammar where
import Fun.Parser
import Fun.Tree
+-- TODO: Multiple programs (= files) in tree
tree :: Parser Tree
-tree = iterFull program >>> Tree
+tree = program >>> build >>> Tree <?> "tree" where build p = [p]
program :: Parser Program
program = iterFull block >>> Program <?> "program"
diff --git a/src/Fun/Parser.hs b/src/Fun/Parser.hs
index 0a16bc0..ff1e6bb 100644
--- a/src/Fun/Parser.hs
+++ b/src/Fun/Parser.hs
@@ -18,16 +18,16 @@ upperAlpha :: [Char]
upperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ"
isDigit :: Char -> Bool
-isDigit c = elem c "0123456789"
+isDigit = (`elem` "0123456789")
isAlpha :: Char -> Bool
-isAlpha c = elem c $ lowerAlpha ++ upperAlpha
+isAlpha = (`elem` lowerAlpha ++ upperAlpha)
isLower :: Char -> Bool
-isLower c = elem c lowerAlpha
+isLower = (`elem` lowerAlpha)
isUpper :: Char -> Bool
-isUpper c = elem c upperAlpha
+isUpper = (`elem` upperAlpha)
----
@@ -94,11 +94,10 @@ oneOrMore m = (iter m) <=> (/= "") <?> "one or more chars"
iter :: Parser a -> Parser [a]
iter m = m <+> iter m >>> (\(x, y) -> x : y) <|> result [] <?> "multiple"
--- TODO: Improve this for better error reporting
iterFull :: Parser a -> Parser [a]
iterFull m = m <+> iterFull m >>> (\(x, y) -> x : y) <|> iterFull'
iterFull' "" = Right ([], "")
-iterFull' _ = Left $ State [StringTrace "<something>"] $ Nothing
+iterFull' _ = Left $ State [] $ Nothing
token :: Parser a -> Parser a
token = (<+-> space)
diff --git a/src/Fun/Tree.hs b/src/Fun/Tree.hs
index 5de4695..354677d 100644
--- a/src/Fun/Tree.hs
+++ b/src/Fun/Tree.hs
@@ -36,36 +36,3 @@ data FunctionBody = FunctionBody [FunctionBodyElement]
deriving Show
data FunctionBodyElement = Statement String | FunctionBodyIdentifier String | FunctionBodyParameter String | FunctionBodyString String | FunctionBodyNumber Integer
deriving Show
-
-----
-
-data NodeValue = NodeName String | FunctionBlockNode FunctionBlock -- | Type, Lambda, .. (TODO)
- deriving Show
-data EmptyValue = EmptyValue
- deriving Show
-data Node = EmptyNode | Node NodeValue [Node]
- deriving Show
-data Crumb = Crumb NodeValue [Node] [Node]
- deriving Show
-type Zipper = (Node, [Crumb])
-
-(-:) :: Maybe Zipper -> (Maybe Zipper -> Maybe Zipper) -> Maybe Zipper
-u -: f = f u
-
-nodePred :: (NodeValue -> Bool) -> Node -> Bool
-nodePred pred (Node value _) = pred value
-nodePred _ _ = False
-
--- One layer search from top
-treeTo :: (NodeValue -> Bool) -> Maybe Zipper -> Maybe Zipper
-treeTo _ Nothing = Nothing
-treeTo pred (Just (EmptyNode, _)) = Nothing
-treeTo pred (Just (Node value nodes, bs)) =
- let (ls, node : rs) = break (nodePred pred) nodes
- in Just (node, Crumb value ls rs : bs)
-
-treeUp :: Maybe Zipper -> Maybe Zipper
-treeUp Nothing = Nothing
-treeUp (Just (EmptyNode, _)) = Nothing
-treeUp (Just (node, Crumb value ls rs : bs)) =
- Just (Node value (ls ++ [node] ++ rs), bs)