diff options
author | Marvin Borner | 2022-02-23 20:42:03 +0100 |
---|---|---|
committer | Marvin Borner | 2022-02-23 20:42:03 +0100 |
commit | de249084d343a1503400112580a28fe5b038d4f6 (patch) | |
tree | 0b3aadfa3d32279a33dcdc48018d42a15f14878f | |
parent | eec77f103115b92230af6d1b43ea1f2b58db28b8 (diff) |
Cool trees
-rw-r--r-- | src/Fun/Compiler.hs | 19 | ||||
-rw-r--r-- | src/Fun/Parser.hs | 11 |
2 files changed, 21 insertions, 9 deletions
diff --git a/src/Fun/Compiler.hs b/src/Fun/Compiler.hs index a8134ed..4d9a67a 100644 --- a/src/Fun/Compiler.hs +++ b/src/Fun/Compiler.hs @@ -6,10 +6,21 @@ import Fun.Tree import System.Exit import System.IO -genTrace :: [String] -> String -genTrace xs = - "Trace of expectance: " - ++ foldr (\a b -> a ++ if b == "" then b else " -> " ++ b) "" xs +traceBranch :: Int -> Trace -> String +traceBranch c (StringTrace t) = "\n" ++ (replicate c ' ') ++ t +traceBranch c (OrTrace t1 t2) = + "\n" + ++ (replicate c ' ') + ++ "<either>" + ++ (traceTree (c + 2) t1) + ++ (traceTree (c + 2) t2) + +traceTree :: Int -> [Trace] -> String -- TODO: Indent/arrow first map +traceTree c ts = foldr join "" (map (traceBranch c) ts) + where join = (\a b -> a ++ if b == "" then b else " " ++ b) + +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 diff --git a/src/Fun/Parser.hs b/src/Fun/Parser.hs index 4519e2f..8042caa 100644 --- a/src/Fun/Parser.hs +++ b/src/Fun/Parser.hs @@ -2,8 +2,9 @@ module Fun.Parser where import Fun.Tree +data Trace = StringTrace String | OrTrace [Trace] [Trace] data State = State - { trace :: [String] + { trace :: [Trace] , line :: Maybe String } type Parser a = String -> Either State (a, String) @@ -33,7 +34,7 @@ isAlpha c = elem c $ lowerAlpha ++ upperAlpha ---- char :: Parser Char -char [] = Left $ State ["char"] Nothing +char [] = Left $ State [StringTrace "char"] Nothing char (x : xs) = Right (x, xs) digit :: Parser Char @@ -86,7 +87,7 @@ result :: a -> Parser a result a cs = Right (a, cs) invalid :: a -> Parser a -invalid a cs = Left $ State ["<unknown>"] $ Just "Invalid" +invalid a cs = Left $ State [StringTrace "<unknown>"] $ Just "Invalid" iter :: Parser Char -> Parser String iter m = (iterS m) <=> (/= "") <?> "multiple chars" @@ -166,7 +167,7 @@ infixl 3 <|> Left (State t' l) -> case (t, t') of (t , []) -> Left (State t l) ([], t') -> Left (State t' l) - (t , t') -> Left (State (t ++ ["<OR>"] ++ t') l) + (t , t') -> Left (State [OrTrace t t'] l) Right (result, cs) -> Right (result, cs) result -> result @@ -174,7 +175,7 @@ infixl 3 <|> infix 0 <?> (<?>) :: Parser a -> String -> Parser a (parser <?> string) input = case parser input of - Left (State t l) -> Left $ State ([string] ++ t) l + Left (State t l) -> Left $ State ([StringTrace string] ++ t) l Right a -> Right a ---- |