This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from X-R-G-B/19-option-help
19 option help
- Loading branch information
Showing
6 changed files
with
187 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |