Skip to content

Commit d293cfa

Browse files
committed
get basic imports working with envSourceMap
1 parent c9e6056 commit d293cfa

File tree

8 files changed

+101
-20
lines changed

8 files changed

+101
-20
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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, (^.), (&), (%~), (.~))
8586
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON, genericToJSON)
8687
import Data.IntMap.Strict (IntMap)
8788
import Data.IntMap.Strict qualified as IM
89+
import Data.Map qualified as M
8890
import GHC.Generics (Generic)
8991
import Prettyprinter (Doc, Pretty (..), encloseSep, hsep, (<+>))
9092
import Swarm.Game.Entity (Entity)
@@ -93,6 +95,7 @@ import Swarm.Game.Ingredients (Count)
9395
import Swarm.Game.Tick
9496
import Swarm.Game.World (WorldUpdate (..))
9597
import Swarm.Language.Elaborate (insertSuspend)
98+
import Swarm.Language.Load (SourceMap)
9699
import Swarm.Language.Requirements.Type (Requirements)
97100
import Swarm.Language.Syntax
98101
import 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

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import Swarm.Game.Step.Util
7878
import Swarm.Game.Step.Util.Command
7979
import Swarm.Game.Tick
8080
import Swarm.Language.Capability
81+
import Swarm.Language.Load (moduleTerm)
8182
import Swarm.Language.Requirements qualified as R
8283
import Swarm.Language.Syntax
8384
import Swarm.Language.TDVar (tdVarName)
@@ -91,7 +92,7 @@ import System.Clock (TimeSpec)
9192
import System.Metrics.Counter qualified as Counter
9293
import System.Metrics.Distribution qualified as Distribution
9394
import System.Metrics.Gauge qualified as Gauge
94-
import Witch (From (from))
95+
import Witch (From (from), into)
9596
import Prelude hiding (lookup)
9697

9798
-- | GameState with support for IO and Time effect
@@ -712,10 +713,15 @@ stepCESK cesk = case cesk of
712713
-- If we see a primitive application of suspend, package it up as
713714
-- a value until it's time to execute.
714715
In (TSuspend t) e s k -> return $ Out (VSuspend t e) s k
715-
-- XXX keep a map from imports to corresponding Env, don't re-evaluate if it's already
716-
-- in the map. To make this sound, need to disallow all but defs in an import.
717-
-- XXX Evaluate the code corresponding to an import.
718-
In (TImportIn loc t) e s k -> return $ In t e s k
716+
-- Evaluate the code corresponding to an import.
717+
In (TImportIn loc t) e s k -> do
718+
return $ case M.lookup loc (e ^. envSourceMap) of
719+
Nothing -> Up (Fatal (T.append "Import not found: " (into @Text (locToFilePath loc)))) s k
720+
Just mmod -> case moduleTerm mmod of
721+
Nothing -> In t e s k
722+
Just m -> In (insertSuspend $ erase m ^. sTerm) e s (FExec : FBind Nothing Nothing t e : k)
723+
-- XXX keep a map from imports to corresponding Env, don't re-evaluate if it's already
724+
-- in the map. To make this sound, need to disallow all but defs in an import.
719725
-- Ignore explicit parens.
720726
In (TParens t) e s k -> return $ In t e s k
721727
------------------------------------------------------------
@@ -815,12 +821,12 @@ stepCESK cesk = case cesk of
815821
--
816822
-- x; z <- y; q; r
817823
--
818-
Suspended _ e s (FBind Nothing _ t2 _ : k) -> return $ In t2 e s (FExec : k)
824+
Suspended _ e s (FBind Nothing _ t2 _ : k) -> return $ In t2 e s k
819825
Suspended v e s (FBind (Just x) mtr t2 _ : k) -> do
820826
let e' = case mtr of
821827
Nothing -> addValueBinding x v e
822828
Just (ty, reqs) -> addBinding x (WithType v ty reqs) e
823-
return $ In t2 e' s (FExec : k)
829+
return $ In t2 e' s k
824830
-- Otherwise, if we're suspended with nothing else left to do,
825831
-- return the machine unchanged (but throw away the rest of the
826832
-- continuation stack).

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ insertSuspend t = case t of
7575
TRequire {} -> thenSuspend
7676
TStock {} -> thenSuspend
7777
TRequirements {} -> thenSuspend
78-
-- Recurse through def, tydef, bind, and annotate.
78+
-- Recurse through def, tydef, bind, annotate, and import.
7979
TLet ls r x mty mpty mreq t1 t2 -> TLet ls r x mty mpty mreq t1 (insertSuspend t2)
8080
TTydef x pty mtd t1 -> TTydef x pty mtd (insertSuspend t1)
8181
TBind mx mty mreq c1 c2 -> TBind mx mty mreq c1 (insertSuspend c2)
8282
TAnnotate t1 ty -> TAnnotate (insertSuspend t1) ty
83+
TImportIn loc t1 -> TImportIn loc (insertSuspend t1)
8384
-- Replace pure or noop with suspend
8485
TApp (TConst Pure) t1 -> TSuspend t1
8586
TConst Noop -> TSuspend TUnit

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import Control.Effect.State (State, get, modify)
2222
import Control.Effect.Throw (Throw, throwError)
2323
import Control.Lens ((.~))
2424
import Control.Monad (forM_)
25-
import Data.Aeson (ToJSON)
2625
import Data.Data (Data, Typeable)
2726
import Data.Function ((&))
2827
import Data.Hashable (Hashable)
@@ -78,6 +77,7 @@ data Module phase = Module
7877
}
7978
deriving (Generic)
8079

80+
deriving instance (Show (Anchor (ImportPhaseFor phase)), Show (SwarmType phase), Show (ModuleCtx phase), Show (ModuleImports phase)) => Show (Module phase)
8181
deriving instance (Eq (ModuleImports phase), Eq (ModuleCtx phase), Eq (SwarmType phase), Eq (Anchor (ImportPhaseFor phase))) => Eq (Module phase)
8282
deriving instance (Eq (Anchor (ImportPhaseFor phase)), Data (Anchor (ImportPhaseFor phase)), Typeable phase, Typeable (ImportPhaseFor phase), Data (ModuleCtx phase), Data (ModuleImports phase), Data (SwarmType phase)) => Data (Module phase)
8383
deriving instance (Hashable (ModuleImports phase), Hashable (ModuleCtx phase), Hashable (SwarmType phase), Hashable (Anchor (ImportPhaseFor phase)), Generic (Anchor (ImportPhaseFor phase))) => Hashable (Module phase)
@@ -168,7 +168,7 @@ resolveImport parent loc = do
168168
-- Finally, record the loaded module in the SourceMap.
169169
modify @(SourceMap Resolved) (M.insert canonicalLoc $ Module mt' () imps)
170170

171-
-- XXX make sure imports contain ONLY defs?
171+
-- XXX make sure imports contain ONLY defs + more imports?
172172
-- Otherwise we can't cache their values---we would have to
173173
-- re-execute them every time they are imported.
174174

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ extractReqCtx (Syntax _ t _ _) = extractReqCtxTerm t
163163

164164
class Processable t where
165165
process :: (Has (Lift IO) sig m, Has (Error SystemFailure) sig m) => t Raw -> m (t Elaborated)
166-
-- XXX should m include effects to save resulting SrcMap ??
167166

168167
instance Processable Syntax where
169168
process s = do

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,8 @@ collectDefs ::
11171117
(Has Unification sig m, Has (Reader UCtx) sig m) =>
11181118
Syntax Inferred ->
11191119
m UCtx
1120-
collectDefs (Syntax _ (SLet LSDef _ x _ _ _ _ t) _ ty) = do
1121-
ty' <- generalize ty
1120+
collectDefs (Syntax _ (SLet LSDef _ x _ _ _ body t) _ _) = do
1121+
ty' <- generalize (body ^. sType)
11221122
(Ctx.singleton (lvVar x) ty' <>) <$> collectDefs t
11231123
collectDefs _ = pure Ctx.empty
11241124

src/swarm-tui/Swarm/TUI/Controller/Util.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,12 @@ runBaseTerm = mapM_ startBaseProgram
270270
-- The player typed something at the REPL and hit Enter; this
271271
-- function takes the resulting term (if the REPL
272272
-- input is valid) and sets up the base robot to run it.
273-
startBaseProgram (srcMap, t) = do -- XXX use srcMap!
273+
startBaseProgram (srcMap, t) = do
274274
-- Set the REPL status to Working
275275
gameState . gameControls . replStatus .= REPLWorking (t ^. sType) Nothing
276276
-- Set up the robot's CESK machine to evaluate/execute the
277277
-- given term.
278-
gameState . baseRobot . machine %= continue t
278+
gameState . baseRobot . machine %= continue srcMap t
279279
-- Finally, be sure to activate the base robot.
280280
gameState %= execState (zoomRobots $ activateRobot 0)
281281

0 commit comments

Comments
 (0)