Skip to content

Commit 2beaf0b

Browse files
committed
Add exit code at the end
1 parent 36a944a commit 2beaf0b

23 files changed

Lines changed: 39 additions & 16 deletions

src/LambdaComp/AM/Eval.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Data.Maybe (mapMaybe, maybeToList)
2020
import Data.Tuple (swap)
2121
import Data.Vector (Vector)
2222
import Data.Vector qualified as Vector
23+
import System.Exit (ExitCode (ExitFailure, ExitSuccess))
2324
import System.IO (Handle, hPrint)
2425

2526
import LambdaComp.AM.Syntax
@@ -32,9 +33,13 @@ data Item where
3233
ItThunk :: !Ident -> !(Vector Item) -> Item
3334
deriving stock Show
3435

35-
topEval :: Handle -> [CodeSection] -> IO Item
36-
topEval out cs = returnReg <$> runMachine evalData evalState
36+
topEval :: Handle -> [CodeSection] -> IO ExitCode
37+
topEval out cs = toExitCode . returnReg <$> runMachine evalData evalState
3738
where
39+
toExitCode (ItInt 0) = ExitSuccess
40+
toExitCode (ItInt n) = ExitFailure n
41+
toExitCode _ = error "Compiler bug: TypeError in AM evaluation!"
42+
3843
evalData = foldr insertCodeSection (Map.empty, ([], out)) cs
3944
evalState =
4045
EvalState

src/LambdaComp/Driver.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mainFuncWithOptions outH (Options inputFp backend phase mayFp) = (<* hFlush outH
6060
DirectCBackend -> do
6161
cCode <- getCCode
6262
runWithFp (const $ genAndExeCExe outH cCode) mayFp
63-
AMBackend -> getAMTm >>= lift . topEval outH >>= pHPrintNoColor outH
63+
AMBackend -> getAMTm >>= exitCodeToExceptT . topEval outH
6464

6565
handleElabError :: Handle -> Either ElaborationError a -> ExceptT Int IO a
6666
handleElabError outH (Left elabErr) = lift (hPutStrLn outH "Elab") >> pHPrintNoColor outH elabErr >> throwError 1

test/Main.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ module Main (main) where
33
import Control.Monad (void)
44
import Data.ByteString.Lazy qualified as LBS
55
import Data.Int (Int64)
6+
import Data.Maybe (fromMaybe)
7+
import GHC.IO.Exception (ExitCode (ExitFailure))
68
import System.Directory (makeAbsolute)
79
import System.FilePath (takeFileName, (<.>), (</>))
8-
import System.IO (Handle, hClose)
10+
import System.IO (Handle, hClose, hPrint)
911
import System.IO.Temp (withSystemTempFile)
1012
import System.Timeout (timeout)
1113
import Test.Tasty
@@ -94,8 +96,9 @@ amExecutionTests allExamples =
9496
$ executionOfExample "am" (makeAMOptions Run) <$> allExamples
9597

9698
executionOfExample :: String -> (FilePath -> Options) -> String -> TestTree
97-
executionOfExample tag = goldenOf (Just 10000) ("execution" <.> tag <.> "out") $ \handle ->
98-
timeout 300000 . mainFuncWithOptions handle
99+
executionOfExample tag = goldenOf (Just 10000) ("execution" <.> tag <.> "out") $ \handle opt -> do
100+
exitCode <- timeoutWithExitCode 300000 $ mainFuncWithOptions handle opt
101+
hPrint handle exitCode
99102

100103
goldenOf :: Maybe Int64 -> String -> (Handle -> Options -> IO a) -> (FilePath -> Options) -> String -> TestTree
101104
goldenOf maySize tag f optionBuilder s =
@@ -125,3 +128,8 @@ getExampleDir = makeAbsolute "examples"
125128

126129
gitDiff :: FilePath -> FilePath -> [String]
127130
gitDiff ref new = ["git", "diff", ref, new]
131+
132+
timeoutWithExitCode :: Int -> IO ExitCode -> IO ExitCode
133+
timeoutWithExitCode n = fmap (fromMaybe timeOutExitCode) . timeout n
134+
where
135+
timeOutExitCode = ExitFailure 124
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
5
2-
ItInt 0
2+
ExitSuccess
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
5
2+
ExitSuccess

test/golden/Fib10.lc.execution.am.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
34
2121
10
2222
55
23-
ItInt 0
23+
ExitSuccess

test/golden/Fib10.lc.execution.c.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
34
2121
10
2222
55
23+
ExitSuccess

test/golden/Fib100.lc.execution.am.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,4 @@
200200
-2437933049959450366
201201
100
202202
3736710778780434371
203-
ItInt 0
203+
ExitSuccess

test/golden/Fib100.lc.execution.c.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,4 @@
200200
-889489150
201201
100
202202
-980107325
203+
ExitSuccess
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
5
22
4
33
3
4-
ItInt 0
4+
ExitSuccess

0 commit comments

Comments
 (0)