Skip to content

Commit

Permalink
Towards #1238
Browse files Browse the repository at this point in the history
'act' command
  • Loading branch information
kostmo committed Oct 19, 2024
1 parent 050bde8 commit 9bdad8a
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 12 deletions.
11 changes: 11 additions & 0 deletions data/entities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,17 @@
- An extremely efficient solar panel, capable of generating sufficient power from ambient starlight alone. A robot powered by one of these can operate any time, including on cloudy days and at night.
capabilities: [power]
properties: [pickable]
- name: agency card
display:
attr: device
char: 'A'
description:
- Become a card-carrying member of the Nice Roboticists Agency.
- Philosophically speaking, this "agency" grants a robot the capacity to `act`.
- |
`act : Dir -> Cmd Unit` modifies an entity in said direction.
capabilities: [act]
properties: [pickable]
- name: drill
display:
attr: device
Expand Down
1 change: 1 addition & 0 deletions data/scenarios/Testing/00-ORDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Achievements
1207-scout-command.yaml
1218-stride-command.yaml
1234-push-command.yaml
1238-act-command.yaml
1256-halt-command.yaml
1271-wall-boundaries.yaml
1262-display-device-commands.yaml
Expand Down
86 changes: 86 additions & 0 deletions data/scenarios/Testing/1238-act-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
version: 1
name: Act command
creative: false
description: Open a gate
objectives:
- goal:
- Grab the flower on the other side of the gate.
condition: |
as base {has "flower"}
solution: |
move;
_k <- grab;
act forward;
move;
move;
grab;
robots:
- name: base
dir: [1, 0]
display:
char: Ω
attr: robot
devices:
- dictionary
- grabber
- toolkit
- logger
- treads
- calculator
- drill
- agency card
- welder
entities:
- name: fence
display:
attr: wood
char: '#'
description:
- Impassable barrier
properties: [known, unwalkable, boundary]
- name: gate key
display:
attr: iron
char: 'k'
description:
- Can open a closed gate
capabilities: [drill]
properties: [known, pickable]
- name: closed gate
display:
attr: wood
char: '|'
description:
- Cannot pass through this
properties: [known, unwalkable]
- name: open gate
display:
attr: wood
char: '/'
description:
- Can pass through this
properties: [known]
recipes:
- in:
- [1, closed gate]
out:
- [1, open gate]
required:
- [1, drill]
known: [flower]
world:
default: [blank]
palette:
'Ω': [grass, null, base]
'.': [grass]
'#': [grass, fence]
'|': [grass, closed gate]
'*': [grass, flower]
'k': [grass, gate key]
upperleft: [0, 0]
map: |
......
..###.
Ωk|*#.
..###.
......
1 change: 1 addition & 0 deletions editors/emacs/swarm-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"has"
"equipped"
"count"
"act"
"drill"
"use"
"build"
Expand Down
18 changes: 9 additions & 9 deletions example/list.sw
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,20 @@ def index: Int -> ListI -> Int
end

def for:a. Int -> Int -> (Int -> Cmd a) -> Cmd Unit
= \s. \e. \act.
if (s == e) {} {act s; for (s + 1) e act}
= \s. \e. \f.
if (s == e) {} {f s; for (s + 1) e f}
end

def for_each_i: Int -> ListI -> (Int -> Int -> Cmd Unit) -> Cmd Unit
= \i. \xs. \act.
= \i. \xs. \f.
if (xs == nil) {} {
let ht = headTail xs in act i (fst ht); for_each_i (i + 1) (snd ht) act
let ht = headTail xs in f i (fst ht); for_each_i (i + 1) (snd ht) f
}
end

def for_each: ListI -> (Int -> Cmd Unit) -> Cmd Unit
= \xs. \act.
for_each_i 0 xs (\i. act)
= \xs. \f.
for_each_i 0 xs (\i. f)
end


Expand All @@ -207,9 +207,9 @@ end
def assert = \b. \m. if b {} {log "FAIL:"; fail m} end

def assert_eq
= \exp. \act. \m.
if (exp == act) {} {
log ("FAIL: expected " ++ format exp ++ " actual " ++ format act);
= \exp. \actual. \m.
if (exp == actual) {} {
log ("FAIL: expected " ++ format exp ++ " actual " ++ format actual);
fail m
}
end
Expand Down
6 changes: 3 additions & 3 deletions example/recursive-containers.sw
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,9 @@ def formatS : Set k -> Text = \s. formatL $ inorder s end
def assert = \i. \b. \m. if b {log $ indent i ++ "OK: " ++ m} {log "FAIL:"; fail m} end

def assert_eq
= \i. \exp. \act. \m.
if (exp == act) {log $ indent i ++ "OK: " ++ m} {
log (indent i ++ "FAIL: expected " ++ format exp ++ " actual " ++ format act);
= \i. \exp. \actual. \m.
if (exp == actual) {log $ indent i ++ "OK: " ++ m} {
log (indent i ++ "FAIL: expected " ++ format exp ++ " actual " ++ format actual);
fail m
}
end
Expand Down
7 changes: 7 additions & 0 deletions src/swarm-engine/Swarm/Game/Step/Const.hs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ execConst runChildProg c vs s k = do
Time -> do
TickNumber t <- use $ temporal . ticks
return $ Out (VInt $ fromIntegral t) s k
Act -> case vs of
[VDir d] -> do
drillOut <- doDrill d
return $ case drillOut of
Out _ s' k' -> Out VUnit s' k'
_ -> Out VUnit s k
_ -> badConst
Drill -> case vs of
[VDir d] -> doDrill d
_ -> badConst
Expand Down
7 changes: 7 additions & 0 deletions src/swarm-lang/Swarm/Language/Syntax/Constants.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ data Const
Equipped
| -- | Sense how many of a certain item we have.
Count
| -- | Act upon an entity.
Act
| -- | Drill through an entity.
Drill
| -- | Use an entity with another.
Expand Down Expand Up @@ -608,6 +610,11 @@ constInfo c = case c of
(Set.singleton $ Mutation $ RobotChange BehaviorChange)
"Reprogram another robot with a new command."
$ ["The other robot has to be nearby and idle."]
Act ->
command 1 long . doc (Set.fromList [Mutation EntityChange, Mutation $ RobotChange InventoryChange]) "Act upon an entity." $
[ "This command transforms an entity in the world using a device."
, "It will automatically `use` the appropriate device required by a recipe that takes the target entity as input."
]
Drill ->
command 1 long
. doc
Expand Down
1 change: 1 addition & 0 deletions src/swarm-lang/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ inferConst c = case c of
Count -> [tyQ| Text -> Cmd Int |]
Reprogram -> [tyQ| Actor -> {Cmd a} -> Cmd Unit |]
Build -> [tyQ| {Cmd a} -> Cmd Actor |]
Act -> [tyQ| Dir -> Cmd Unit |]
Drill -> [tyQ| Dir -> Cmd (Unit + Text) |]
Use -> [tyQ| Text -> Dir -> Cmd (Unit + Text) |]
Salvage -> [tyQ| Cmd Unit |]
Expand Down
1 change: 1 addition & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ testScenarioSolutions rs ui key =
, testSolution Default "Testing/1207-scout-command"
, testSolution Default "Testing/1218-stride-command"
, testSolution Default "Testing/1234-push-command"
, testSolution Default "Testing/1238-act-command"
, testSolution Default "Testing/1681-pushable-entity"
, testSolution Default "Testing/1256-halt-command"
, testSolution Default "Testing/1295-density-command"
Expand Down

0 comments on commit 9bdad8a

Please sign in to comment.