Skip to content

Commit a977bdf

Browse files
committed
add SyntaxWithImports type and thread SrcMap through a bunch more stuff
1 parent 161fb3b commit a977bdf

File tree

23 files changed

+151
-89
lines changed

23 files changed

+151
-89
lines changed

src/swarm-engine/Swarm/Game/CESK.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import Swarm.Game.Ingredients (Count)
9595
import Swarm.Game.Tick
9696
import Swarm.Game.World (WorldUpdate (..))
9797
import Swarm.Language.Elaborate (insertSuspend)
98-
import Swarm.Language.Load (SourceMap)
98+
import Swarm.Language.Load (SourceMap, SyntaxWithImports (..))
9999
import Swarm.Language.Requirements.Type (Requirements)
100100
import Swarm.Language.Syntax
101101
import Swarm.Language.Types
@@ -326,21 +326,21 @@ cont = lens get set
326326
Waiting t c -> Waiting t (set c k)
327327
Suspended v e s _ -> Suspended v e s k
328328

329-
-- | Create a brand new CESK machine, with empty environment and
330-
-- store, to evaluate a given term. We always initialize the
331-
-- machine with a single FExec frame as the continuation; if the
332-
-- given term does not have a command type, we wrap it in @pure@.
333-
initMachine :: Syntax Elaborated -> CESK
334-
initMachine t = In (prepareTerm V.emptyEnv t) V.emptyEnv emptyStore [FExec]
329+
-- | Create a brand new CESK machine to evaluate a given term. We
330+
-- always initialize the machine with a single FExec frame as the
331+
-- continuation; if the given term does not have a command type, we
332+
-- wrap it in @pure@.
333+
initMachine :: SyntaxWithImports Elaborated -> CESK
334+
initMachine (SyntaxWithImports srcMap t) = In (prepareTerm V.emptyEnv t) (V.envFromSrcMap srcMap) emptyStore [FExec]
335335

336336
-- | Load a program into an existing robot CESK machine: either
337337
-- continue from a suspended state, or, as a fallback, start from
338338
-- scratch with an empty environment but the same store.
339339
--
340340
-- Also insert a @suspend@ primitive at the end, so the resulting
341341
-- term is suitable for execution by the base (REPL) robot.
342-
continue :: SourceMap Elaborated -> Syntax Elaborated -> CESK -> CESK
343-
continue srcMap t = \case
342+
continue :: SyntaxWithImports Elaborated -> CESK -> CESK
343+
continue (SyntaxWithImports srcMap t) = \case
344344
-- The normal case is when we are continuing from a suspended state. We:
345345
--
346346
-- (1) insert a suspend call at the end of the term, so that in

src/swarm-engine/Swarm/Game/Robot/Concrete.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ import Swarm.Game.Robot.Activity
4343
import Swarm.Game.Robot.Walk (emptyExceptions)
4444
import Swarm.Game.Tick
4545
import Swarm.Game.Universe
46+
import Swarm.Language.Load (SyntaxWithImports)
4647
import Swarm.Language.Pipeline.QQ (tmQ)
47-
import Swarm.Language.Syntax (Phase (..), Syntax)
48+
import Swarm.Language.Syntax (Phase (..))
4849
import Swarm.Language.Value as V
4950
import Swarm.Log
5051

@@ -112,7 +113,7 @@ instance ToSample (Robot Instantiated) where
112113
emptyExceptions
113114
0
114115

115-
mkMachine :: Maybe (Syntax Elaborated) -> C.CESK
116+
mkMachine :: Maybe (SyntaxWithImports Elaborated) -> C.CESK
116117
mkMachine Nothing = C.Out VUnit C.emptyStore []
117118
mkMachine (Just t) = C.initMachine t
118119

src/swarm-engine/Swarm/Game/State.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ import Swarm.Game.Tick (addTicks)
130130
import Swarm.Game.Universe as U
131131
import Swarm.Game.World qualified as W
132132
import Swarm.Game.World.Coords
133+
import Swarm.Language.Load (SyntaxWithImports (..))
133134
import Swarm.Language.Pipeline (processSource, requireNonEmptyTerm)
134135
import Swarm.Language.Syntax (Phase (..), SrcLoc (..), Syntax, sLoc)
135136
import Swarm.Language.Value (Env)
@@ -149,7 +150,7 @@ data SolutionSource
149150

150151
data CodeToRun = CodeToRun
151152
{ _toRunSource :: SolutionSource
152-
, _toRunSyntax :: Syntax Elaborated
153+
, _toRunSyntax :: SyntaxWithImports Elaborated
153154
}
154155

155156
makeLenses ''CodeToRun
@@ -166,8 +167,8 @@ parseCodeFile ::
166167
parseCodeFile filepath = do
167168
contents <- sendIO $ TIO.readFile filepath
168169
mpt <- sendIO . runError $ requireNonEmptyTerm =<< processSource contents Nothing
169-
pt <- either (throwError @SystemFailure) (pure . snd) mpt -- XXX need SourceMap?
170-
let srcLoc = pt ^. sLoc
170+
pt <- either (throwError @SystemFailure) pure mpt
171+
let srcLoc = getSyntax pt ^. sLoc
171172
strippedText = stripSrc srcLoc contents
172173
programBytestring = TL.encodeUtf8 $ TL.fromStrict strippedText
173174
sha1Hash = showDigest $ sha1 programBytestring
@@ -204,7 +205,7 @@ data GameState = GameState
204205
{ _creativeMode :: Bool
205206
, _temporal :: TemporalState
206207
, _winCondition :: WinCondition Elaborated
207-
, _winSolution :: Maybe (Syntax Elaborated)
208+
, _winSolution :: Maybe (SyntaxWithImports Elaborated)
208209
, _robotInfo :: Robots
209210
, _pathCaching :: PathCaching
210211
, _discovery :: Discovery
@@ -236,7 +237,7 @@ winCondition :: Lens' GameState (WinCondition Elaborated)
236237

237238
-- | How to win (if possible). This is useful for automated testing
238239
-- and to show help to cheaters (or testers).
239-
winSolution :: Lens' GameState (Maybe (Syntax Elaborated))
240+
winSolution :: Lens' GameState (Maybe (SyntaxWithImports Elaborated))
240241

241242
-- | Get a list of all the robots at a particular location.
242243
robotsAtLocation :: Cosmic Location -> GameState -> [Robot Instantiated]

src/swarm-engine/Swarm/Game/State/Substate.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ import Swarm.Game.Scenario.Topography.Structure.Recognition.Registry (emptyFound
113113
import Swarm.Game.State.Config
114114
import Swarm.Game.Tick (TickNumber (..))
115115
import Swarm.Game.World.Gen (Seed)
116+
import Swarm.Language.Load (ModuleCtx, ModuleImports, SyntaxWithImports)
116117
import Swarm.Language.Syntax (Anchor, Const, ImportPhaseFor, Phase (..), SwarmType, Syntax, Unresolvable)
117118
import Swarm.Language.Types (Polytype)
118119
import Swarm.Language.Value (Value)
@@ -160,7 +161,7 @@ data WinCondition phase
160161
deriving (Generic)
161162

162163
deriving instance FromJSON (WinCondition Raw)
163-
deriving instance (PrettyPrec (Anchor (ImportPhaseFor phase)), Unresolvable (ImportPhaseFor phase), Generic (Anchor (ImportPhaseFor phase)), ToJSON (Anchor (ImportPhaseFor phase)), ToJSON (SwarmType phase)) => ToJSON (WinCondition phase)
164+
deriving instance (PrettyPrec (Anchor (ImportPhaseFor phase)), Unresolvable (ImportPhaseFor phase), Generic (Anchor (ImportPhaseFor phase)), ToJSON (Anchor (ImportPhaseFor phase)), ToJSON (SwarmType phase), ToJSON (ModuleCtx phase), ToJSON (ModuleImports phase)) => ToJSON (WinCondition phase)
164165

165166
makePrisms ''WinCondition
166167

@@ -318,7 +319,7 @@ data GameControls = GameControls
318319
, _replNextValueIndex :: Integer
319320
, _replListener :: Text -> IO ()
320321
, _inputHandler :: Maybe (Text, Value)
321-
, _initiallyRunCode :: Maybe (Syntax Elaborated)
322+
, _initiallyRunCode :: Maybe (SyntaxWithImports Elaborated)
322323
}
323324

324325
makeLensesNoSigs ''GameControls
@@ -338,7 +339,7 @@ inputHandler :: Lens' GameControls (Maybe (Text, Value))
338339

339340
-- | Code that is run upon scenario start, before any
340341
-- REPL interaction.
341-
initiallyRunCode :: Lens' GameControls (Maybe (Syntax Elaborated))
342+
initiallyRunCode :: Lens' GameControls (Maybe (SyntaxWithImports Elaborated))
342343

343344
data Discovery = Discovery
344345
{ _allDiscoveredEntities :: Inventory

src/swarm-engine/Swarm/Game/Step.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import Swarm.Game.Step.Util
8080
import Swarm.Game.Step.Util.Command
8181
import Swarm.Game.Tick
8282
import Swarm.Language.Capability
83-
import Swarm.Language.Load (moduleTerm)
83+
import Swarm.Language.Load (SyntaxWithImports, moduleTerm)
8484
import Swarm.Language.Requirements qualified as R
8585
import Swarm.Language.Syntax
8686
import Swarm.Language.TDVar (tdVarName)
@@ -449,7 +449,7 @@ evalT ::
449449
, Has (State GameState) sig m
450450
, Has (Lift IO) sig m
451451
) =>
452-
Syntax Elaborated ->
452+
SyntaxWithImports Elaborated ->
453453
m Value
454454
evalT = evaluateCESK . initMachine
455455

src/swarm-engine/Swarm/Game/Step/Combustion.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Swarm.Game.Step.RobotStepState
3939
import Swarm.Game.Step.Util
4040
import Swarm.Game.Step.Util.Inspect
4141
import Swarm.Game.Universe
42+
import Swarm.Language.Load (SyntaxWithImports)
4243
import Swarm.Language.Pipeline.QQ (tmQ)
4344
import Swarm.Language.Syntax
4445
import Swarm.Language.Syntax.Direction (Direction)
@@ -142,7 +143,7 @@ addCombustionBot inputEntity combustibility ts loc = do
142143
-- 3. Spawn more robots whose sole purpose is to observe for changes to neighbor
143144
-- cells. This would avoid polluting the logic of the currently burning cell
144145
-- with logic to manage probabilities of combustion propagation.
145-
combustionProgram :: Integer -> Combustibility -> Syntax Elaborated
146+
combustionProgram :: Integer -> Combustibility -> SyntaxWithImports Elaborated
146147
combustionProgram _combustionDuration (Combustibility _ _ _ maybeCombustionProduct) =
147148
[tmQ|
148149
wait $int:_combustionDuration;
@@ -227,7 +228,7 @@ addIgnitionBot ignitionDelay inputEntity ts loc =
227228
ts
228229

229230
-- Triggers the ignition of the entity underfoot with some delay.
230-
ignitionProgram :: Integer -> Syntax Elaborated
231+
ignitionProgram :: Integer -> SyntaxWithImports Elaborated
231232
ignitionProgram _waitTime =
232233
[tmQ|
233234
wait $int:_waitTime;

src/swarm-engine/Swarm/Game/Step/Const.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,10 +1237,10 @@ execConst runChildProg c vs s k = do
12371237

12381238
case mt of
12391239
Nothing -> return $ mkReturn ()
1240-
Just (_, t) -> do
1240+
Just t -> do
12411241
void $ traceLog CmdStatus Info "run: OK."
12421242
cesk <- use machine
1243-
return $ continue M.empty t cesk
1243+
return $ continue t cesk
12441244
_ -> badConst
12451245
Not -> case vs of
12461246
[VBool b] -> return $ Out (VBool (not b)) s k

src/swarm-engine/Swarm/Game/Step/Util/Command.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import Swarm.Game.Universe
5858
import Swarm.Game.World qualified as W
5959
import Swarm.Game.World.Coords
6060
import Swarm.Language.Capability
61+
import Swarm.Language.Load (SyntaxWithImports)
6162
import Swarm.Language.Pipeline.QQ (tmQ)
6263
import Swarm.Language.Requirements.Type qualified as R
6364
import Swarm.Language.Syntax
@@ -519,7 +520,7 @@ seedProgram ::
519520
Integer ->
520521
-- | entity to place
521522
EntityName ->
522-
Syntax Elaborated
523+
SyntaxWithImports Elaborated
523524
seedProgram _minTime _randTime _seedlingCount _seedlingRadius _thing =
524525
[tmQ|
525526
def doN = \n. \f. if (n > 0) {f; doN (n - 1) f} {}; end;
@@ -589,7 +590,7 @@ addAsphyxiateBot ts loc =
589590
-- we detect that the base has the life support system re-equipped,
590591
-- stop the countdown and self-destruct. Otherwise, at the end of
591592
-- the countdown, destroy the base.
592-
asphyxiateProg :: Syntax Elaborated
593+
asphyxiateProg :: SyntaxWithImports Elaborated
593594
asphyxiateProg =
594595
[tmQ|
595596
def countdown : Int -> Cmd Unit = \n.

src/swarm-lang/Swarm/Language/JSON.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
module Swarm.Language.JSON where
1212

1313
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON, genericToJSON, withText)
14+
import Data.Map qualified as M
1415
import GHC.Generics (Generic)
15-
import Swarm.Language.Load (Module, ModuleCtx, ModuleImports)
16+
import Swarm.Language.Load (Module, ModuleCtx, ModuleImports, SyntaxWithImports (..))
1617
import Swarm.Language.Parser (readNonemptyTerm)
1718
import Swarm.Language.Syntax (Anchor, ImportPhaseFor, Phase (Raw), SwarmType, Syntax, Term)
1819
import Swarm.Language.Value (Env, Value (..))
@@ -33,6 +34,12 @@ instance (Generic (Anchor (ImportPhaseFor phase)), ToJSON (Anchor (ImportPhaseFo
3334

3435
deriving instance (Generic (Anchor (ImportPhaseFor phase)), ToJSON (Anchor (ImportPhaseFor phase)), ToJSON (SwarmType phase), ToJSON (ModuleCtx phase), ToJSON (ModuleImports phase)) => ToJSON (Module phase)
3536

37+
instance FromJSON (SyntaxWithImports Raw) where
38+
parseJSON v = SyntaxWithImports M.empty <$> parseJSON @(Syntax Raw) v
39+
40+
instance (Generic (Anchor (ImportPhaseFor phase)), ToJSON (Anchor (ImportPhaseFor phase)), ToJSON (SwarmType phase), ToJSON (ModuleCtx phase), ToJSON (ModuleImports phase)) => ToJSON (SyntaxWithImports phase) where
41+
toJSON = genericToJSON optionsMinimize . getSyntax
42+
3643
instance ToJSON Value where
3744
toJSON = genericToJSON optionsMinimize
3845

src/swarm-lang/Swarm/Language/LSP.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Language.LSP.Protocol.Types qualified as LSP
2222
import Language.LSP.Server
2323
import Language.LSP.VFS (VirtualFile (..), virtualFileText)
2424
import Swarm.Failure (SystemFailure (..))
25+
import Swarm.Language.Load (SyntaxWithImports (..))
2526
import Swarm.Language.LSP.Hover qualified as H
2627
import Swarm.Language.LSP.VarUsage qualified as VU
2728
import Swarm.Language.Parser.Util (getLocRange)
@@ -84,7 +85,7 @@ validateSwarmCode doc version content = do
8485
res <- liftIO . runError $ processSource content Nothing
8586
let (errors, warnings) = case res of
8687
Right Nothing -> ([], [])
87-
Right (Just (_, term)) -> ([], unusedWarnings)
88+
Right (Just (SyntaxWithImports _ term)) -> ([], unusedWarnings)
8889
where
8990
VU.Usage _ problems = VU.getUsage mempty (eraseRaw term)
9091
unusedWarnings = mapMaybe (VU.toErrPos content) problems

0 commit comments

Comments
 (0)