diff --git a/app/Args.hs b/app/Args.hs new file mode 100644 index 0000000..c502b03 --- /dev/null +++ b/app/Args.hs @@ -0,0 +1,61 @@ +{- +-- EPITECH PROJECT, 2023 +-- Koaky +-- File description: +-- args +-} + + +module Args + ( + Action(..), + Args(..), + RunOpt(..), + parseArgs, + printHelp + ) where + +data Action = ShowHelp | ShowVersion | Run +data RunOpt = RunStdin | RunFile + +data Args = Args { + action :: Action, + runOpt :: RunOpt, + filePath :: String +} + +parseArgs' :: [String] -> Args -> Either Args String +parseArgs' [] args = + Left args +parseArgs' ("--help":xs) args = + parseArgs' xs (args {action = ShowHelp}) +parseArgs' ("-h":xs) args = + parseArgs' xs (args {action = ShowHelp}) +parseArgs' ("--version":xs) args = + parseArgs' xs (args {action = ShowVersion}) +parseArgs' ("-v":xs) args = + parseArgs' xs (args {action = ShowVersion}) +parseArgs' ("-f":f:xs) args = + parseArgs' xs (args {action = Run, runOpt = RunFile, filePath = f}) +parseArgs' ["-f"] _ = + Right "No file specified" +parseArgs' ("-":xs) args = + parseArgs' xs (args {action = Run, runOpt = RunStdin}) +parseArgs' (x:_) _ = + Right ("Unknown option: " ++ x) + +parseArgs :: [String] -> Either Args String +parseArgs args = + parseArgs' args (Args {action = Run, runOpt = RunStdin, filePath = ""}) + +printHelp :: IO () +printHelp = putStr help + where + line1 = "Usage: koaky [OPTION]\n\nInterpret Lisp\n" + line2a = "With no options, koaky reads from standard input.\n\n" + line3 = "Options:\n" + line4 = "\t-h, --help\n\t\tDisplay this help and exit\n" + line5 = "\t-v, --version\n\t\tOutput version information and exit\n" + line6 = "\t-f FILE, --file FILE\n\t\tRead FILE and Interpret it\n" + line7 = "\t-\n\t\tRead from standard input and Interpret it\n" + help = line1 ++ line2a ++ line3 ++ line4 ++ line5 ++ line6 ++ line7 diff --git a/app/Main.hs b/app/Main.hs index ee3fcd9..cf421bf 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,35 +5,17 @@ -- Main -} -import Computing.ComputeAST -import Parsing.Parser -import Types -import System.IO - -printErrors :: (Env) -> IO () -printErrors (Env defines_ []) = - printErrors (Env defines_ ["Unable to compute"]) -printErrors (Env defines_ errors_) = - mapM_ putStrLn errors_ >> handleInput (Env defines_ []) - -checkComputing :: (Env, Maybe Result) -> IO () -checkComputing (env, Nothing) = printErrors env -checkComputing (env, Just result) = putStrLn (show result) >> handleInput env - -checkParsing :: Maybe (Tree, String) -> Env -> IO () -checkParsing Nothing _ = return () -checkParsing (Just (tree, _)) env = checkComputing (computeAST env tree) - -checkInput :: String -> Env -> IO () -checkInput ":q" _ = return () -checkInput input env = checkParsing (runParser (parseTree) input) env - -checkEOF :: Env -> Bool -> IO () -checkEOF _ True = return () -checkEOF env False = getLine >>= (\x -> checkInput x env) - -handleInput :: Env -> IO () -handleInput env = isEOF >>= (\x -> checkEOF env x) +import System.Environment (getArgs) +import Args +import Run +import Version + +dispatchAction :: Either Args String -> IO () +dispatchAction (Right error_) = putStrLn error_ +dispatchAction (Left (Args ShowHelp _ _)) = printHelp +dispatchAction (Left (Args ShowVersion _ _)) = printVersion +dispatchAction (Left (Args Run RunFile f)) = runFile f +dispatchAction (Left (Args Run RunStdin _)) = runStdin main :: IO () -main = handleInput (Env [] []) +main = getArgs >>= (dispatchAction . parseArgs) diff --git a/app/Run.hs b/app/Run.hs new file mode 100644 index 0000000..55caba0 --- /dev/null +++ b/app/Run.hs @@ -0,0 +1,62 @@ +{- +-- EPITECH PROJECT, 2023 +-- Koaky +-- File description: +-- run +-} + +module Run + ( + runStdin, + runFile + ) where + +import Computing.ComputeAST +import Parsing.Parser +import Types +import System.IO + +data HHHandle = HHHandle Handle Bool + +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_ []) + +checkComputing :: HHHandle -> (Env, Maybe Result) -> IO () +checkComputing hand (env, Nothing) = printErrors 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 = + checkComputing hand (computeAST env tree) + +checkInput :: HHHandle -> String -> Env -> IO () +checkInput _ ":q" _ = return () +checkInput hand input env = checkParsing hand (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) + +handleInput :: HHHandle -> Env -> IO () +handleInput (HHHandle ff shouldClosee) env = + hIsEOF ff >>= (\x -> checkEOF (HHHandle ff shouldClosee) env x) + +runStdin :: IO () +runStdin = runFileHandle stdin False + +runFile :: String -> IO () +runFile filePath = openFile filePath ReadMode >>= \x -> runFileHandle x True + +onEnd :: HHHandle -> IO () +onEnd (HHHandle ff True) = hClose ff +onEnd _ = return () + +runFileHandle :: Handle -> Bool -> IO () +runFileHandle ff shouldClosee = + handleInput (HHHandle ff shouldClosee) (Env [] []) >> + onEnd (HHHandle ff shouldClosee) diff --git a/app/Version.hs b/app/Version.hs new file mode 100644 index 0000000..815099c --- /dev/null +++ b/app/Version.hs @@ -0,0 +1,15 @@ +{- +-- EPITECH PROJECT, 2023 +-- koaky +-- File description: +-- version +-} + +module Version + ( printVersion + ) where + +import KoakyLibVersion + +printVersion :: IO () +printVersion = putStrLn koakyLibVersion diff --git a/koaky.cabal b/koaky.cabal index 026aa9d..f7efbb0 100644 --- a/koaky.cabal +++ b/koaky.cabal @@ -32,6 +32,7 @@ library Computing.Defines Computing.Errors Computing.Functions + KoakyLibVersion Parsing.Parser Types other-modules: @@ -46,6 +47,9 @@ library executable koaky-exe main-is: Main.hs other-modules: + Args + Run + Version Paths_koaky hs-source-dirs: app diff --git a/src/KoakyLibVersion.hs b/src/KoakyLibVersion.hs new file mode 100644 index 0000000..7f4d84c --- /dev/null +++ b/src/KoakyLibVersion.hs @@ -0,0 +1,33 @@ +{- +-- EPITECH PROJECT, 2023 +-- koaky +-- File description: +-- lib version +-} + +module KoakyLibVersion + ( + koakyLibVersionPatch, + koakyLibVersionMinor, + koakyLibVersionMajor, + koakyLibVersion + ) + where + +koakyLibVersionPatch :: Int +koakyLibVersionPatch = 0 + +koakyLibVersionMinor :: Int +koakyLibVersionMinor = 0 + +koakyLibVersionMajor :: Int +koakyLibVersionMajor = 0 + + +koakyLibVersion :: String +koakyLibVersion = fullVersion + where + fMaj = show koakyLibVersionMajor + fMin = show koakyLibVersionMinor + fPat = show koakyLibVersionPatch + fullVersion = fMaj ++ "." ++ fMin ++ "." ++ fPat