From 8e4bd75090fe2cdca9e3064655f827430612ca71 Mon Sep 17 00:00:00 2001 From: Axel Humeau Date: Sun, 18 Feb 2024 18:58:16 +0100 Subject: [PATCH] fix: add error handling for file that miss permission or doesn't exist --- LobsterLang/app/Main.hs | 32 ++++++++++++++++++++------------ exemple/Fibonacci.lob | 0 2 files changed, 20 insertions(+), 12 deletions(-) mode change 100644 => 100755 exemple/Fibonacci.lob diff --git a/LobsterLang/app/Main.hs b/LobsterLang/app/Main.hs index 62ef8fb..ce755c3 100644 --- a/LobsterLang/app/Main.hs +++ b/LobsterLang/app/Main.hs @@ -104,22 +104,30 @@ compileFile file s = case runParser parseLobster (0, 0) s of checkArgs :: [String] -> IO () checkArgs [] = print "Launch Interpreter" >> inputLoop [] -checkArgs ("-e" : file : _) = putStr "Result: " >> CompiletoVm.makeConvert file - >>= \instructions -> case fst (Vm.exec 0 [] [] instructions []) of - Left err -> print err - Right (IntVal res) -> print res - Right (BoolVal res) -> print res - Right (CharVal res) -> print res - Right (StringVal res) -> print res - Right (ListVal res) -> print res - Right (Op res) -> print res - Right (Function res _) -> print res +checkArgs ("-e" : file : _) = putStr "Result: " >> + (either (\_ -> print "File doesn't exist or permission denied" >> + exitWith (ExitFailure 84)) return + =<< (try (CompiletoVm.makeConvert file) :: IO (Either SomeException Inst))) + >>= \instructions -> printResult (fst (Vm.exec 0 [] [] instructions [])) checkArgs (file : _) = either - (\_ -> print "File doesn't exist" >> exitWith (ExitFailure 84)) - (compileFile file) + (\_ -> print "File doesn't exist or permission denied" >> + exitWith (ExitFailure 84)) + (\a -> either (\_ -> print "Permission denied on result file" >> + exitWith (ExitFailure 84)) return + =<< (try (compileFile file a) :: IO (Either SomeException ()))) =<< (try (readFile file) :: IO (Either SomeException String)) +printResult :: Either String Value -> IO () +printResult (Left err) = print err +printResult (Right (IntVal res)) = print res +printResult (Right (BoolVal res)) = print res +printResult (Right (CharVal res)) = print res +printResult (Right (StringVal res)) = print res +printResult (Right (ListVal res)) = print res +printResult (Right (Op res)) = print res +printResult (Right (Function res _)) = print res + -- | Main main :: IO () main = getArgs >>= \argv -> checkArgs argv diff --git a/exemple/Fibonacci.lob b/exemple/Fibonacci.lob old mode 100644 new mode 100755