Skip to content

Commit

Permalink
Add State instances
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanCacqueray committed Jun 26, 2022
1 parent e99b7db commit fd78831
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/Swarm/Game/CESK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ module Swarm.Game.CESK (
) where

import Control.Lens.Combinators (pattern Empty)
import Data.Aeson (FromJSON, ToJSON)
import qualified Data.Aeson
import Data.Aeson (FromJSON, Null, ToJSON)
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import Data.List (intercalate)
Expand Down Expand Up @@ -379,7 +378,8 @@ instance Eq WorldUpdate where _ == _ = True

instance Eq RobotUpdate where _ == _ = True

-- TODO: remove these instances once Update fields are concret
instance FromJSON WorldUpdate where parseJSON _ = pure $ WorldUpdate $ \w -> Right w
instance ToJSON WorldUpdate where toJSON _ = Data.Aeson.Null
instance ToJSON WorldUpdate where toJSON _ = Null
instance FromJSON RobotUpdate where parseJSON _ = pure $ RobotUpdate id
instance ToJSON RobotUpdate where toJSON _ = Data.Aeson.Null
instance ToJSON RobotUpdate where toJSON _ = Null
1 change: 1 addition & 0 deletions src/Swarm/Game/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ data EntityMap = EntityMap
{ entitiesByName :: Map Text Entity
, entitiesByCap :: Map Capability [Entity]
}
deriving (Generic, FromJSON, ToJSON)

instance Semigroup EntityMap where
EntityMap n1 c1 <> EntityMap n2 c2 = EntityMap (n1 <> n2) (c1 <> c2)
Expand Down
11 changes: 8 additions & 3 deletions src/Swarm/Game/Recipe.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}

Expand Down Expand Up @@ -50,6 +51,7 @@ import Data.List (foldl')
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Witch

import Data.Yaml
Expand Down Expand Up @@ -79,7 +81,10 @@ data Recipe e = Recipe
, _recipeTime :: Integer
, _recipeWeight :: Integer
}
deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
deriving (Eq, Ord, Show, Functor, Foldable, Traversable, Generic)

deriving instance ToJSON (Recipe Entity)
deriving instance FromJSON (Recipe Entity)

makeLensesWith (lensRules & generateSignatures .~ False) ''Recipe

Expand Down
2 changes: 2 additions & 0 deletions src/Swarm/Game/Scenario.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE MultiParamTypeClasses #-}
Expand Down
16 changes: 11 additions & 5 deletions src/Swarm/Game/State.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -89,6 +93,7 @@ import Control.Applicative ((<|>))
import Control.Arrow (Arrow ((&&&)))
import Control.Lens hiding (Const, use, uses, view, (%=), (+=), (.=), (<+=), (<<.=))
import Control.Monad.Except
import Data.Aeson (FromJSON, ToJSON)
import Data.Array (Array, listArray)
import Data.Int (Int64)
import Data.IntMap (IntMap)
Expand All @@ -104,6 +109,7 @@ import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T (lines)
import qualified Data.Text.IO as T (readFile)
import GHC.Generics (Generic)
import Linear
import qualified System.Clock
import System.Random (StdGen, mkStdGen, randomRIO)
Expand Down Expand Up @@ -141,7 +147,7 @@ data ViewCenterRule
VCLocation (V2 Int64)
| -- | The view should be centered on a certain robot.
VCRobot RID
deriving (Eq, Ord, Show)
deriving (Eq, Ord, Show, Generic, FromJSON, ToJSON)

makePrisms ''ViewCenterRule

Expand All @@ -154,7 +160,7 @@ data REPLStatus
-- entered. The @Maybe Value@ starts out as @Nothing@ and gets
-- filled in with a result once the command completes.
REPLWorking Polytype (Maybe Value)
deriving (Eq, Show)
deriving (Eq, Show, Generic, FromJSON, ToJSON)

data WinCondition
= -- | There is no winning condition.
Expand All @@ -165,7 +171,7 @@ data WinCondition
| -- | The player has won. The boolean indicates whether they have
-- already been congratulated.
Won Bool
deriving (Show)
deriving (Show, Generic, FromJSON, ToJSON)

makePrisms ''WinCondition

Expand All @@ -178,14 +184,14 @@ data RunStatus
| -- | The game got paused while visiting the help,
-- and it should unpause after returning back to the game.
AutoPause
deriving (Eq, Show)
deriving (Eq, Show, Generic, FromJSON, ToJSON)

-- | A data type to keep track of discovered recipes and commands
data Notifications a = Notifications
{ _notificationsCount :: Int
, _notificationsContent :: [a]
}
deriving (Eq, Show)
deriving (Eq, Show, Generic, FromJSON, ToJSON)

instance Semigroup (Notifications a) where
Notifications count1 xs1 <> Notifications count2 xs2 = Notifications (count1 + count2) (xs1 <> xs2)
Expand Down
3 changes: 2 additions & 1 deletion src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Swarm.Language.Capability (
constCaps,
) where

import Data.Aeson (FromJSONKey, ToJSONKey)
import Data.Char (toLower)
import Data.Hashable (Hashable)
import Data.Maybe (fromMaybe)
Expand Down Expand Up @@ -113,7 +114,7 @@ data Capability
-- checking challenge mode win conditions, and not for use by
-- players.
CGod
deriving (Eq, Ord, Show, Read, Enum, Bounded, Generic, Hashable, Data)
deriving (Eq, Ord, Show, Read, Enum, Bounded, Generic, Hashable, Data, FromJSONKey, ToJSONKey)

capabilityName :: Capability -> Text
capabilityName = from @String . map toLower . drop 1 . show
Expand Down
5 changes: 4 additions & 1 deletion src/Swarm/Language/Pipeline.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

-- |
Expand Down Expand Up @@ -27,6 +29,7 @@ import Data.Data (Data)
import Data.Set (Set)
import Data.Text (Text)
import Data.Yaml as Y
import GHC.Generics (Generic)
import Witch

import Swarm.Language.Capability
Expand All @@ -50,7 +53,7 @@ data ProcessedTerm
-- ^ Capabilities required by the term
CapCtx
-- ^ Capability context for any definitions embedded in the term
deriving (Data, Show)
deriving (Data, Show, Generic, ToJSON)

instance FromJSON ProcessedTerm where
parseJSON = withText "Term" tryProcess
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/Language/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ type UPolytype = Poly UType
-- contains the overall type of the expression, as well as the
-- context giving the types of any defined variables.
data Module s t = Module {moduleTy :: s, moduleCtx :: Ctx t}
deriving (Show, Eq, Functor, Data)
deriving (Show, Eq, Functor, Data, Generic, FromJSON, ToJSON)

-- | A 'TModule' is the final result of the type inference process on
-- an expression: we get a polytype for the expression, and a
Expand Down

0 comments on commit fd78831

Please sign in to comment.