Skip to content

Commit

Permalink
fix: add error handling for file that miss permission or doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelHumeau committed Feb 18, 2024
1 parent 1d77b56 commit 8e4bd75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions LobsterLang/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file modified exemple/Fibonacci.lob
100644 → 100755
Empty file.

0 comments on commit 8e4bd75

Please sign in to comment.