Skip to content

Commit

Permalink
fix Ctrl-C so it doesn't lose all definitions in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Jun 17, 2024
1 parent 540fcf9 commit 4b9307e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/swarm-engine/Swarm/Game/CESK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ prepareTerm e t = case whnfType (e ^. envTydefs) (ptBody (t ^. sType)) of

-- | Cancel the currently running computation.
cancel :: CESK -> CESK
cancel cesk = Out VUnit s' []
cancel cesk = Up Cancel s' (cesk ^. cont)
where
s' = resetBlackholes $ cesk ^. store

Expand Down
17 changes: 17 additions & 0 deletions src/swarm-engine/Swarm/Game/Exception.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Swarm.Game.Exception (
Exn (..),
IncapableFix (..),
formatExn,
exnSeverity,
IncapableFixWords (..),

-- * Helper functions
Expand All @@ -32,6 +33,7 @@ import Swarm.Language.JSON ()
import Swarm.Language.Pretty (prettyText)
import Swarm.Language.Requirements.Type (Requirements (..))
import Swarm.Language.Syntax (Const, Term)
import Swarm.Log (Severity (..))
import Swarm.Util
import Witch (from)

Expand Down Expand Up @@ -67,6 +69,11 @@ data Exn
-- be caught by a @try@ block (but at least it will not crash
-- the entire UI).
Fatal Text
| -- | The user manually cancelled the computation (e.g. by hitting
-- Ctrl-C). This cannot be caught by a @try@ block, and results
-- in the CESK machine unwinding the stack all the way back to
-- the top level.
Cancel
| -- | An infinite loop was detected via a blackhole. This cannot
-- be caught by a @try@ block.
InfiniteLoop
Expand Down Expand Up @@ -94,11 +101,21 @@ formatExn em = \case
, "Please report this as a bug at"
, "<" <> swarmRepoUrl <> "issues/new>."
]
Cancel -> "Computation cancelled."
InfiniteLoop -> "Infinite loop detected!"
(CmdFailed c t _) -> T.concat [prettyText c, ": ", t]
(User t) -> "Player exception: " <> t
(Incapable f caps tm) -> formatIncapable em f caps tm

exnSeverity :: Exn -> Severity
exnSeverity = \case
Fatal {} -> Critical
Cancel {} -> Info
InfiniteLoop {} -> Error
Incapable {} -> Error
CmdFailed {} -> Error
User {} -> Error

-- ------------------------------------------------------------------
-- INCAPABLE HELPERS
-- ------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-engine/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ stepCESK cesk = case cesk of
let s' = resetBlackholes s
h <- hasCapability CLog
em <- use $ landscape . terrainAndEntities . entityMap
when h $ void $ traceLog RobotError Error (formatExn em exn)
when h $ void $ traceLog RobotError (exnSeverity exn) (formatExn em exn)
return $ case menv of
Nothing -> Out VExc s' []
Just env -> Suspended VExc env s' []
Expand Down

0 comments on commit 4b9307e

Please sign in to comment.