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

'Be' command for unprivileged 'As' #1530

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"installkeyhandler"
"teleport"
"as"
"be"
"robotnamed"
"robotnumbered"
"knows"
Expand Down
2 changes: 1 addition & 1 deletion editors/vim/swarm.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syn keyword Keyword def end let in require
syn keyword Builtins self parent base if inl inr case fst snd force undefined fail not format chars split charat tochar key
syn keyword Command noop wait selfdestruct move backup push stride turn grab harvest ignite place give equip unequip make has equipped count drill use build salvage reprogram say listen log view appear create halt time scout whereami waypoint detect resonate density sniff chirp watch surveil heading blocked scan upload ishere isempty meet meetall whoami setname random run return try swap atomic instant installkeyhandler teleport as robotnamed robotnumbered knows
syn keyword Command noop wait selfdestruct move backup push stride turn grab harvest ignite place give equip unequip make has equipped count drill use build salvage reprogram say listen log view appear create halt time scout whereami waypoint detect resonate density sniff chirp watch surveil heading blocked scan upload ishere isempty meet meetall whoami setname random run return try swap atomic instant installkeyhandler teleport as be robotnamed robotnumbered knows
syn keyword Direction east north west south down forward left back right
syn keyword Type int text dir bool cmd void unit actor

Expand Down
2 changes: 1 addition & 1 deletion editors/vscode/syntaxes/swarm.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
{
"name": "keyword.other",
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|backup|push|stride|turn|grab|harvest|ignite|place|give|equip|unequip|make|has|equipped|count|drill|use|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|waypoint|detect|resonate|density|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|robotnamed|robotnumbered|knows)\\b"
"match": "\\b(?i)(self|parent|base|if|inl|inr|case|fst|snd|force|undefined|fail|not|format|chars|split|charat|tochar|key|noop|wait|selfdestruct|move|backup|push|stride|turn|grab|harvest|ignite|place|give|equip|unequip|make|has|equipped|count|drill|use|build|salvage|reprogram|say|listen|log|view|appear|create|halt|time|scout|whereami|waypoint|detect|resonate|density|sniff|chirp|watch|surveil|heading|blocked|scan|upload|ishere|isempty|meet|meetall|whoami|setname|random|run|return|try|swap|atomic|instant|installkeyhandler|teleport|as|be|robotnamed|robotnumbered|knows)\\b"
}
]
},
Expand Down
41 changes: 21 additions & 20 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,26 +1479,8 @@ execConst c vs s k = do
_ -> badConst
Atomic -> goAtomic
Instant -> goAtomic
As -> case vs of
[VRobot rid, prog] -> do
-- Get the named robot and current game state
r <- robotWithID rid >>= (`isJustOrFail` ["There is no actor with ID", from (show rid)])
g <- get @GameState

-- Execute the given program *hypothetically*: i.e. in a fresh
-- CESK machine, using *copies* of the current store, robot
-- and game state. We discard the state afterwards so any
-- modifications made by prog do not persist. Note we also
-- set the copied robot to be a "system" robot so it is
-- capable of executing any commands; the As command
-- already requires "God" capability.
v <-
evalState @Robot (r & systemRobot .~ True) . evalState @GameState g $
runCESK (Out prog s [FApp (VCApp Force []), FExec])

-- Return the value returned by the hypothetical command.
return $ Out v s k
_ -> badConst
As -> impersonateCommand True
Be -> impersonateCommand False
RobotNamed -> case vs of
[VText rname] -> do
r <- robotWithName rname >>= (`isJustOrFail` ["There is no robot named", rname])
Expand Down Expand Up @@ -2093,6 +2075,25 @@ execConst c vs s k = do
DRelative (DPlanar DBack) -> "behind"
_ -> directionSyntax d <> " of"

impersonateCommand forceSystemRobot = case vs of
[VRobot rid, prog] -> do
-- Get the named robot and current game state
r <- robotWithID rid >>= (`isJustOrFail` ["There is no actor with ID", from (show rid)])
g <- get @GameState

let modifiedRobot = applyWhen forceSystemRobot (\x -> x & systemRobot .~ True) r
-- Execute the given program *hypothetically*: i.e. in a fresh
-- CESK machine, using *copies* of the current store, robot
-- and game state. We discard the state afterwards so any
-- modifications made by prog do not persist.
v <-
evalState @Robot modifiedRobot . evalState @GameState g $
runCESK (Out prog s [FApp (VCApp Force []), FExec])

-- Return the value returned by the hypothetical command.
return $ Out v s k
_ -> badConst

goAtomic :: HasRobotStepState sig m => m CESK
goAtomic = case vs of
-- To execute an atomic block, set the runningAtomic flag,
Expand Down
1 change: 1 addition & 0 deletions src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ constCaps = \case
-- ----------------------------------------------------------------
-- Some God-like abilities.
As -> Just CGod
Be -> Just CGod
RobotNamed -> Just CGod
RobotNumbered -> Just CGod
Create -> Just CGod
Expand Down
7 changes: 5 additions & 2 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,10 @@ data Const

-- | Teleport a robot to the given position.
Teleport
| -- | Run a command as if you were another robot.
| -- | Run a command as if you were another robot, and with "system robot" privileges.
As
| -- | Run a command as if you were another robot.
Be
| -- | Find an actor by name.
RobotNamed
| -- | Find an actor by number.
Expand Down Expand Up @@ -762,7 +764,8 @@ constInfo c = case c of
, "The second argument is a function to handle keyboard inputs."
]
Teleport -> command 2 short "Teleport a robot to the given location."
As -> command 2 Intangible "Hypothetically run a command as if you were another robot."
As -> command 2 Intangible "Hypothetically run a command as if you were another robot, with system privileges."
Be -> command 2 Intangible "Hypothetically run a command as if you were another robot."
RobotNamed -> command 1 Intangible "Find an actor by name."
RobotNumbered -> command 1 Intangible "Find an actor by number."
Knows -> command 1 Intangible "Check if the robot knows about an entity."
Expand Down
1 change: 1 addition & 0 deletions src/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ inferConst c = case c of
InstallKeyHandler -> [tyQ| text -> (key -> cmd unit) -> cmd unit |]
Teleport -> [tyQ| actor -> (int * int) -> cmd unit |]
As -> [tyQ| actor -> {cmd a} -> cmd a |]
Be -> [tyQ| actor -> {cmd a} -> cmd a |]
RobotNamed -> [tyQ| text -> cmd actor |]
RobotNumbered -> [tyQ| int -> cmd actor |]
Knows -> [tyQ| text -> cmd bool |]
Expand Down
Loading