Replace TSyntax with Syntax in a couple places, plus a bit of refactoring (#2074)
Some checks failed
Enforce issue references for TODOs / Enforce issue references (push) Waiting to run
Haskell-CI-Windows / Haskell-CI - ${{ matrix.os }} - ghc-${{ matrix.ghc }} (3.10.1.0, 9.8.2, windows-latest) (push) Waiting to run
Haskell-CI / Haskell-CI - Linux - ${{ matrix.compiler }} (false, ghc-9.2.8, ghc, 9.2.8, ghcup) (push) Waiting to run
Haskell-CI / Haskell-CI - Linux - ${{ matrix.compiler }} (false, ghc-9.4.8, ghc, 9.4.8, ghcup) (push) Waiting to run
Haskell-CI / Haskell-CI - Linux - ${{ matrix.compiler }} (false, ghc-9.6.5, ghc, 9.6.5, ghcup) (push) Waiting to run
Haskell-CI / Haskell-CI - Linux - ${{ matrix.compiler }} (false, ghc-9.8.2, ghc, 9.8.2, ghcup) (push) Waiting to run
HLint / HLint (push) Waiting to run
Normalize cabal file formatting / Normalize cabal (push) Waiting to run
VSCode CI / VSCode build (16, ubuntu-latest) (push) Has been cancelled

A bit of initial refactoring towards #495 .

- Replace `TSyntax` with `Syntax` in a couple places where the types are not needed.
    - Once we implement #495 producing `TSyntax` is going to get a lot harder, so the more places we can eliminate it the better.
- Also some related minor refactoring, and a new function `readNonemptyTerm`.  We're not using it yet, but (1) I thought I would need it in this PR, and (2) when I realized I didn't, I decided to leave it there anyway since it will likely come in handy later.
This commit is contained in:
Brent Yorgey 2024-07-22 17:40:01 -05:00 committed by GitHub
parent 57e0b7f526
commit 32dca485a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 13 deletions

View File

@ -12,7 +12,7 @@ import GHC.Generics (Generic)
import Swarm.Language.Syntax
data CodeSizeDeterminators = CodeSizeDeterminators
{ initialCode :: Maybe TSyntax
{ initialCode :: Maybe Syntax
, hasUsedREPL :: Bool
}
deriving (Show)

View File

@ -35,6 +35,8 @@ module Swarm.Game.State (
-- ** GameState initialization
initGameState,
CodeToRun (..),
toRunSource,
toRunSyntax,
Sha1 (..),
SolutionSource (..),
parseCodeFile,
@ -127,7 +129,12 @@ data SolutionSource
-- on a leaderboard.
PlayerAuthored FilePath Sha1
data CodeToRun = CodeToRun SolutionSource TSyntax
data CodeToRun = CodeToRun
{ _toRunSource :: SolutionSource
, _toRunSyntax :: TSyntax
}
makeLenses ''CodeToRun
getRunCodePath :: CodeToRun -> Maybe FilePath
getRunCodePath (CodeToRun solutionSource _) = case solutionSource of

View File

@ -50,7 +50,7 @@ import Swarm.Game.State.Substate
import Swarm.Game.Universe as U (offsetBy)
import Swarm.Game.World.Gen (Seed)
import Swarm.Language.Capability (constCaps)
import Swarm.Language.Syntax (allConst)
import Swarm.Language.Syntax (allConst, erase)
import Swarm.Language.Types
import Swarm.Util (binTuples, (?))
import System.Clock qualified as Clock
@ -99,7 +99,7 @@ pureScenarioToGameState scenario theSeed now toRun gsc =
& randomness . randGen .~ mkStdGen theSeed
& recipesInfo %~ modifyRecipesInfo
& landscape .~ mkLandscape sLandscape worldTuples theSeed
& gameControls . initiallyRunCode .~ initialCodeToRun
& gameControls . initiallyRunCode .~ (erase <$> initialCodeToRun)
& gameControls . replStatus .~ case running of -- When the base starts out running a program, the REPL status must be set to working,
-- otherwise the store of definition cells is not saved (see #333, #838)
False -> REPLDone Nothing
@ -118,11 +118,9 @@ pureScenarioToGameState scenario theSeed now toRun gsc =
baseID = 0
(things, devices) = partition (M.null . getMap . view entityCapabilities) (M.elems (entitiesByName em))
getCodeToRun (CodeToRun _ s) = s
robotsByBasePrecedence = genRobotTemplates sLandscape worldTuples
initialCodeToRun = getCodeToRun <$> toRun
initialCodeToRun = view toRunSyntax <$> toRun
robotListRaw =
zipWith (instantiateRobot Nothing) [baseID ..] robotsByBasePrecedence

View File

@ -109,7 +109,7 @@ import Swarm.Game.Scenario.Topography.Structure.Recognition.Type (RecognizerAuto
import Swarm.Game.State.Config
import Swarm.Game.Tick (TickNumber (..))
import Swarm.Game.World.Gen (Seed)
import Swarm.Language.Syntax (Const, TSyntax)
import Swarm.Language.Syntax (Const, Syntax)
import Swarm.Language.Types (Polytype)
import Swarm.Language.Value (Value)
import Swarm.Log
@ -302,7 +302,7 @@ data GameControls = GameControls
{ _replStatus :: REPLStatus
, _replNextValueIndex :: Integer
, _inputHandler :: Maybe (Text, Value)
, _initiallyRunCode :: Maybe TSyntax
, _initiallyRunCode :: Maybe Syntax
}
makeLensesNoSigs ''GameControls
@ -318,7 +318,7 @@ inputHandler :: Lens' GameControls (Maybe (Text, Value))
-- | Code that is run upon scenario start, before any
-- REPL interaction.
initiallyRunCode :: Lens' GameControls (Maybe TSyntax)
initiallyRunCode :: Lens' GameControls (Maybe Syntax)
data Discovery = Discovery
{ _allDiscoveredEntities :: Inventory

View File

@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
-- |
@ -10,10 +11,13 @@
-- typechecks, elaborates, and capability checks a term all at once.
module Swarm.Language.Parser (
readTerm,
readNonemptyTerm,
readTerm',
) where
import Control.Monad ((>=>))
import Data.Bifunctor (first, second)
import Data.Either.Extra (maybeToEither)
import Data.Sequence (Seq)
import Data.Text (Text)
import Swarm.Language.Parser.Comment (populateComments)
@ -25,6 +29,13 @@ import Swarm.Language.Syntax (Comment, Syntax)
import Text.Megaparsec.Error (errorBundlePretty)
import Witch (from)
-- | Parse some input 'Text' completely as a 'Term', consuming leading
-- whitespace and ensuring the parsing extends all the way to the
-- end of the input 'Text'. Returns an error if the term was only
-- whitespace.
readNonemptyTerm :: Text -> Either Text Syntax
readNonemptyTerm = readTerm >=> maybeToEither "Empty term"
-- | Parse some input 'Text' completely as a 'Term', consuming leading
-- whitespace and ensuring the parsing extends all the way to the
-- end of the input 'Text'. Returns either the resulting 'Term' (or

View File

@ -226,8 +226,8 @@ scenarioToAppState siPair@(scene, _) lp = do
gameState .= gs
void $ withLensIO uiState $ scenarioToUIState isAutoplaying siPair gs
where
isAutoplaying = case runIdentity (initialCode lp) of
Just (CodeToRun ScenarioSuggested _) -> True
isAutoplaying = case fmap (view toRunSource) . runIdentity $ initialCode lp of
Just ScenarioSuggested -> True
_ -> False
withLensIO :: (MonadIO m, MonadState AppState m) => Lens' AppState x -> (x -> IO x) -> m x

View File

@ -31,6 +31,7 @@ roots = [
"^Swarm.Game.World.lookupTerrainM$",
"^Swarm.Language.Context.withBindings$",
"^Swarm.Language.Context.singleton$",
"^Swarm.Language.Parser.readNonemptyTerm$",
"^Swarm.Language.Parser.Util.showShortError$",
"^Swarm.Language.Pipeline.extractTCtx$",
"^Swarm.Language.Pretty.Prec$",
@ -43,7 +44,7 @@ roots = [
"^Swarm.Util.reflow$",
"^Swarm.Util.isSuccessOr$",
"^Swarm.Util._NonEmpty$",
# True positives (unused lenses):
# -------------------------------
"^Swarm.Language.Typed.polytype$",