@@ -73,6 +73,7 @@ module Swarm.Game.CESK (
7373 continue ,
7474 cancel ,
7575 prepareTerm ,
76+ insertSuspend ,
7677
7778 -- ** Extracting information
7879 finalValue ,
@@ -81,10 +82,11 @@ module Swarm.Game.CESK (
8182 cont ,
8283) where
8384
84- import Control.Lens (Lens' , Traversal' , lens , traversal , (^.) )
85+ import Control.Lens (Lens' , Traversal' , lens , traversal , (^.) , (&) , (%~) , (.~) )
8586import Data.Aeson (FromJSON (.. ), ToJSON (.. ), genericParseJSON , genericToJSON )
8687import Data.IntMap.Strict (IntMap )
8788import Data.IntMap.Strict qualified as IM
89+ import Data.Map qualified as M
8890import GHC.Generics (Generic )
8991import Prettyprinter (Doc , Pretty (.. ), encloseSep , hsep , (<+>) )
9092import Swarm.Game.Entity (Entity )
@@ -93,6 +95,7 @@ import Swarm.Game.Ingredients (Count)
9395import Swarm.Game.Tick
9496import Swarm.Game.World (WorldUpdate (.. ))
9597import Swarm.Language.Elaborate (insertSuspend )
98+ import Swarm.Language.Load (SourceMap )
9699import Swarm.Language.Requirements.Type (Requirements )
97100import Swarm.Language.Syntax
98101import Swarm.Language.Types
@@ -336,8 +339,8 @@ initMachine t = In (prepareTerm V.emptyEnv t) V.emptyEnv emptyStore [FExec]
336339--
337340-- Also insert a @suspend@ primitive at the end, so the resulting
338341-- term is suitable for execution by the base (REPL) robot.
339- continue :: Syntax Elaborated -> CESK -> CESK
340- continue t = \ case
342+ continue :: SourceMap Elaborated -> Syntax Elaborated -> CESK -> CESK
343+ continue srcMap t = \ case
341344 -- The normal case is when we are continuing from a suspended state. We:
342345 --
343346 -- (1) insert a suspend call at the end of the term, so that in
@@ -351,11 +354,15 @@ continue t = \case
351354 -- environment e (any names brought into scope by executing the
352355 -- term will be discarded). If the term succeeds, the extra
353356 -- FRestoreEnv frame will be discarded.
354- Suspended _ e s k -> In (insertSuspend $ prepareTerm e t) e s (FExec : FRestoreEnv e : k)
357+ Suspended _ e s k ->
358+ let e' = e & envSourceMap %~ M. union srcMap
359+ in In (insertSuspend $ prepareTerm e' t) e' s (FExec : FRestoreEnv e : k)
355360 -- In any other state, just start with an empty environment. This
356361 -- happens e.g. when running a program on the base robot for the
357362 -- very first time.
358- cesk -> In (insertSuspend $ prepareTerm V. emptyEnv t) V. emptyEnv (cesk ^. store) (FExec : (cesk ^. cont))
363+ cesk ->
364+ let e = V. emptyEnv & envSourceMap .~ srcMap
365+ in In (insertSuspend $ prepareTerm e t) e (cesk ^. store) (FExec : (cesk ^. cont))
359366
360367-- | Prepare a term for evaluation by a CESK machine in the given
361368-- environment: erase all type annotations, and optionally wrap it
0 commit comments