Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planar direction sum type #1358

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
"north"
"west"
"south"
"down"
"forward"
"left"
"right"
"back"
"forward"
"down"
"right"
))
(x-types '("int" "text" "dir" "bool" "cmd" "void" "unit" "actor"))

Expand Down
13 changes: 13 additions & 0 deletions editors/vscode/DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ vsce package --baseImagesUrl "https://raw.githubusercontent.com/swarm-game/swarm
```

To share this extension with the world, read on https://code.visualstudio.com/docs about publishing an extension or ask @xsebek to do it.

## Troubleshooting

If you encounter an error that looks like this:
```
ERROR in test case test/hello.sw
-- existing snapshot
++ new changes
```
Then you may have to update the test snapshot as follows:
```
npm test vscode-tmgrammar-snap -- --updateSnapshot
```
4 changes: 2 additions & 2 deletions editors/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion editors/vscode/syntaxes/swarm.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"patterns": [
{
"name": "variable.language.dir",
"match": "\\b(?i)(east|north|west|south|left|right|back|forward|down)\\b"
"match": "\\b(?i)(east|north|west|south|down|forward|left|back|right|)\\b"
},
{
"name": "variable.parameter",
Expand Down
5 changes: 1 addition & 4 deletions editors/vscode/test/hello.sw.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@
>
>return "";
#^^^^^^ source.swarm keyword.other
# ^ source.swarm
# ^ source.swarm string.quoted.double
# ^ source.swarm string.quoted.double
# ^^ source.swarm
# ^^^^^ source.swarm
>
14 changes: 7 additions & 7 deletions src/Swarm/Game/Location.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import Data.Map qualified as M
import Data.Yaml (FromJSON (parseJSON), ToJSON (toJSON))
import Linear (Additive (..), V2 (..), negated, norm, perp, unangle)
import Linear.Affine (Affine (..), Point (..), origin)
import Swarm.Language.Syntax (AbsoluteDir (..), Direction (..), RelativeDir (..), isCardinal)
import Swarm.Language.Syntax (AbsoluteDir (..), Direction (..), PlanarRelativeDir (..), RelativeDir (..), isCardinal)
import Swarm.Util qualified as Util

-- $setup
Expand Down Expand Up @@ -122,11 +122,11 @@ down = zero
applyTurn :: Direction -> Heading -> Heading
applyTurn d = case d of
DRelative e -> case e of
DLeft -> perp
DRight -> negated . perp
DBack -> negated
DPlanar DLeft -> perp
DPlanar DRight -> negated . perp
DPlanar DBack -> negated
DPlanar DForward -> id
DDown -> const down
DForward -> id
DAbsolute e -> const $ toHeading e

-- | Mapping from heading to their corresponding cardinal directions.
Expand All @@ -143,9 +143,9 @@ toDirection v = M.lookup v cardinalDirs

-- | Example:
-- DWest `relativeTo` DSouth == DRight
relativeTo :: AbsoluteDir -> AbsoluteDir -> RelativeDir
relativeTo :: AbsoluteDir -> AbsoluteDir -> PlanarRelativeDir
relativeTo targetDir referenceDir =
[DForward, DLeft, DBack, DRight] !! indexDiff
toEnum indexDiff
where
enumCount = length (Util.listEnums :: [AbsoluteDir])
indexDiff = ((-) `on` fromEnum) targetDir referenceDir `mod` enumCount
Expand Down
7 changes: 4 additions & 3 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,8 @@ execConst c vs s k = do
if countByName "compass" inst >= 1
then Just $ DAbsolute entityDir
else case mh >>= toDirection of
Just (DAbsolute robotDir) -> Just $ DRelative $ entityDir `relativeTo` robotDir
Just (DAbsolute robotDir) ->
Just . DRelative . DPlanar $ entityDir `relativeTo` robotDir
_ -> Nothing -- This may happen if the robot is facing "down"
val = VDir $ fromMaybe (DRelative DDown) $ do
entLoc <- firstFound
Expand Down Expand Up @@ -2122,8 +2123,8 @@ execConst c vs s k = do
where
directionText = case d of
DRelative DDown -> "under"
DRelative DForward -> "ahead of"
DRelative DBack -> "behind"
DRelative (DPlanar DForward) -> "ahead of"
DRelative (DPlanar DBack) -> "behind"
_ -> directionSyntax d <> " of"

goAtomic :: HasRobotStepState sig m => m CESK
Expand Down
16 changes: 12 additions & 4 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Swarm.Language.Syntax (
Direction (..),
AbsoluteDir (..),
RelativeDir (..),
PlanarRelativeDir (..),
directionSyntax,
isCardinal,
allDirs,
Expand Down Expand Up @@ -153,20 +154,27 @@ instance FromJSONKey AbsoluteDir where
-- | A relative direction is one which is defined with respect to the
-- robot's frame of reference; no special capability is needed to
-- use them.
data RelativeDir = DLeft | DRight | DBack | DForward | DDown
data RelativeDir = DPlanar PlanarRelativeDir | DDown
deriving (Eq, Ord, Show, Read, Generic, Data, Hashable, ToJSON, FromJSON)

-- | Caution: Do not alter this ordering, as there exist functions that depend on it
-- (e.g. "nearestDirection" and "relativeTo").
data PlanarRelativeDir = DForward | DLeft | DBack | DRight
deriving (Eq, Ord, Show, Read, Generic, Data, Hashable, ToJSON, FromJSON, Enum, Bounded)

-- | The type of directions. Used /e.g./ to indicate which way a robot
-- will turn.
data Direction = DAbsolute AbsoluteDir | DRelative RelativeDir
deriving (Eq, Ord, Show, Read, Generic, Data, Hashable, ToJSON, FromJSON)

-- | Direction name is generated from Direction data constuctor
-- | Direction name is generated from Direction data constructor
-- e.g. DLeft becomes "left"
directionSyntax :: Direction -> Text
directionSyntax d = toLower . T.tail . from $ case d of
DAbsolute x -> show x
DRelative x -> show x
DRelative x -> case x of
DPlanar y -> show y
_ -> show x

-- | Check if the direction is absolute (e.g. 'north' or 'south').
isCardinal :: Direction -> Bool
Expand All @@ -175,7 +183,7 @@ isCardinal = \case
_ -> False

allDirs :: [Direction]
allDirs = map DAbsolute Util.listEnums <> map DRelative Util.listEnums
allDirs = map DAbsolute Util.listEnums <> map DRelative (DDown : map DPlanar Util.listEnums)

------------------------------------------------------------
-- Constants
Expand Down