module Fun.Compiler where import Control.Exception import Fun.Parser 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 parse :: String -> Either String Program -- TODO: Should be tree parse file = case program file of Left a -> Left $ "Parse fault!\n" ++ case a of State [] Nothing -> "No context available" State t Nothing -> genTrace t State [] (Just str) -> "Around here:\n" ++ str State t (Just str) -> genTrace t ++ "\nAround here:\n" ++ str Right (a, b) -> Right a compile :: String -> IO () compile path = do file <- try $ readFile path case file of Left exception -> print (exception :: IOError) Right file -> case parse file of Left err -> do hPutStrLn stderr err exitWith (ExitFailure 1) Right block -> putStrLn . show $ block