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

Commit

Permalink
Merge pull request #20 from X-R-G-B/19-option-help
Browse files Browse the repository at this point in the history
19 option help
  • Loading branch information
guillaumeAbel authored Dec 15, 2023
2 parents 5ffc248 + 9d3970f commit 6d8f8be
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 30 deletions.
61 changes: 61 additions & 0 deletions app/Args.hs
Original file line number Diff line number Diff line change
@@ -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
42 changes: 12 additions & 30 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
62 changes: 62 additions & 0 deletions app/Run.hs
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions app/Version.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-
-- EPITECH PROJECT, 2023
-- koaky
-- File description:
-- version
-}

module Version
( printVersion
) where

import KoakyLibVersion

printVersion :: IO ()
printVersion = putStrLn koakyLibVersion
4 changes: 4 additions & 0 deletions koaky.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ library
Computing.Defines
Computing.Errors
Computing.Functions
KoakyLibVersion
Parsing.Parser
Types
other-modules:
Expand All @@ -46,6 +47,9 @@ library
executable koaky-exe
main-is: Main.hs
other-modules:
Args
Run
Version
Paths_koaky
hs-source-dirs:
app
Expand Down
33 changes: 33 additions & 0 deletions src/KoakyLibVersion.hs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6d8f8be

Please sign in to comment.