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

Commit

Permalink
Add multiple line handler
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Dec 17, 2023
1 parent 6d8f8be commit 71d04cc
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions app/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ printErrors :: HHHandle -> Env -> IO ()
printErrors hand (Env defines_ []) =
printErrors hand (Env defines_ ["Unable to compute"])
printErrors hand (Env defines_ errors_) =
mapM_ putStrLn errors_ >> handleInput hand (Env defines_ [])
mapM_ putStrLn errors_ >> handleInput hand (Env defines_ []) []

checkComputing :: HHHandle -> (Env, Maybe Result) -> IO ()
checkComputing hand (env, Nothing) = printErrors hand env
checkComputing hand (env, Just result) = print result >> handleInput hand env
checkComputing hand (env, Just result) = print result >> handleInput hand env []

checkParsing :: HHHandle -> Maybe (Tree, String) -> Env -> IO ()
checkParsing _ Nothing _ = return ()
checkParsing hand (Just (tree, _)) env =
checkParsing :: HHHandle -> String -> Maybe (Tree, String) -> Env -> IO ()
checkParsing hand str Nothing env = handleInput hand env str
checkParsing hand _ (Just (tree, _)) env =
checkComputing hand (computeAST env tree)

checkInput :: HHHandle -> String -> Env -> IO ()
checkInput _ ":q" _ = return ()
checkInput hand input env = checkParsing hand (runParser (parseTree) input) env
checkInput hand input env = checkParsing hand input (runParser (parseTree) input) env

checkEOF :: HHHandle -> Env -> Bool -> IO ()
checkEOF _ _ True = return ()
checkEOF (HHHandle ff shouldClosee) env False = hGetLine ff >>=
(\x -> checkInput (HHHandle ff shouldClosee) x env)
checkEOF :: HHHandle -> Env -> String -> Bool -> IO ()
checkEOF _ _ _ True = return ()
checkEOF (HHHandle ff shouldClosee) env prevStr False = hGetLine ff >>=
(\x -> checkInput (HHHandle ff shouldClosee) (prevStr ++ x) env)

handleInput :: HHHandle -> Env -> IO ()
handleInput (HHHandle ff shouldClosee) env =
hIsEOF ff >>= (\x -> checkEOF (HHHandle ff shouldClosee) env x)
handleInput :: HHHandle -> Env -> String -> IO ()
handleInput (HHHandle ff shouldClosee) env prevStr =
hIsEOF ff >>= (\x -> checkEOF (HHHandle ff shouldClosee) env prevStr x)

runStdin :: IO ()
runStdin = runFileHandle stdin False
Expand All @@ -58,5 +58,5 @@ onEnd _ = return ()

runFileHandle :: Handle -> Bool -> IO ()
runFileHandle ff shouldClosee =
handleInput (HHHandle ff shouldClosee) (Env [] []) >>
handleInput (HHHandle ff shouldClosee) (Env [] []) [] >>
onEnd (HHHandle ff shouldClosee)

0 comments on commit 71d04cc

Please sign in to comment.