Commit Graph

731 Commits

Author SHA1 Message Date
Brent Yorgey
b62d27e566
Use a new opaque type for robots instead of strings (#303)
The basic idea of this change is to create a new `robot` type and use it to identify robots instead of `string` names.  Internally, a `robot` value is just a (unique) `Int`. 

Closes #212 .

This ended up turning into a sort of constellation of related changes.

- Add the `robot` type and change the type of various built-in functions which used to take a robot name so they now take a `robot` (`give`, `install`, `reprogram`, `view`, `upload`) and change `build` so it returns a `robot`.
- All internal data structures that store robots are now keyed by a unique (`Int`) robot ID rather than by name.
- Add a `setname` command for setting a robot's display name (which no longer needs to uniquely identify a robot).
- Import a big list of words which we can use to randomly pick names for robots, just for fun.  This is why the diff says "+31,050  -265"; I did not write 31 thousand lines of code.
- Add constants `base`, `parent`, and `self` for getting a `robot` value referring to the base, one's parent, and one's self, respectively.
- Top-level binders like `r <- build {move}` now export a variable binding which can be used in later expressions entered at the REPL; additionally, unlike Haskell, a binder can now appear as the last statement in a block.
- Fix the pretty-printer for `Value` by doubling down on our current strategy of injecting `Value`s back into `Term`s and then pretty-printing the result.  I am now convinced this is the Right Way (tm) to do this; it only required adding a couple additional kinds of `Term` which represent internal results of evaluation and cannot show up in the surface language (`TRef`, `TRobot`).
- Update the tutorial.
- While updating the tutorial, I noticed that #294 had introduced a bug, where the inventory display no longer updated when 0 copies of an entity are added to the inventory (as with `scan` + `upload`), so I fixed that by changing the way inventory hashes are computed.

I tried running the benchmarks both before & after this change.  I was hoping that it might speed things up to be using `IntMap` and `IntSet` instead of looking things up by `Text` keys in a `Map` all the time.  However, if I'm interpreting the results correctly, it seems like it didn't really make all that much difference, at least for the particular benchmarks we have.
2022-03-02 03:00:44 +00:00
noahyor
47e1e7d074
Hide zero inventory (#301)
Add a `0` key that toggles whether entities with count 0 are shown in the inventory.  Closes #282.
2022-02-19 07:40:28 -06:00
noahyor
ceaa949455
Make seed robots self-destruct when an exception is raised (#298)
I have added a try statement on line 551 of Step.hs to self-destruct even if place command is obstructed.
2022-01-29 21:21:35 +00:00
Brent Yorgey
b1f0e316fd
Challenge mode (#285)
Add a "challenge mode" where the player tries to achieve some specified goal from specific starting conditions.
2022-01-27 17:00:00 -06:00
Brent Yorgey
b267441403 Merge branch 'main' of github.com:byorgey/swarm 2022-01-24 21:43:30 -06:00
Brent Yorgey
74ea395c83
homomorphic hashing for inventories (#294)
The hash for an inventory is now the sum of the hashes of its contents. As discussed in #229 , the point of this is that inventory hashes can now be maintained incrementally, which could be a big win if there are robots with big inventories. It is less "secure" but we don't really care about that.

Closes #229.
2022-01-24 17:43:34 -06:00
Brent Yorgey
875da9674f ignore some files 2022-01-22 15:46:52 -06:00
Brent Yorgey
f6a1931b23 parse and pretty-print tuples properly
Tuples are now parsed as parens containing terms separated by commas,
instead of just treating the comma as a binary infix operator.  This
fixes #225 --- we now properly parse tuples containing lambdas.
Tuples bigger than pairs are treated as syntax sugar for right-nested
pairs, e.g. (1,2,3) is (1,(2,3)) (and also pretty-prints as the former).
2021-12-15 17:06:59 -06:00
Brent Yorgey
b1366b7c2a
add format function and string concatenation operator ++ (#293)
Adds a new function `format : a -> string` and an operator `++ : string -> string -> string`.

Closes #248.
2021-12-04 23:19:19 +00:00
Brent Yorgey
64819cc3bb bit of refactoring in TUI.Controller 2021-12-03 11:44:08 -06:00
Brent Yorgey
bb53a9fc71 clean up some error messages 2021-12-03 11:43:59 -06:00
Alexander Block
b066b3eb45
Wrap errors shown in game (#274)
Closes #241 .

This PR implements two changes:
- The error message window uses as much horizontal space as necessary now, wrapping lines when no further horizontal expansion is possible. 
- The parser groups "built-in user functions" and "direction constants" under labels of the same names. This leads to more concise parse error messages.
2021-12-03 17:05:38 +00:00
Brent Yorgey
cbad51911c
allow only whitespace when reading a term (#291)
- The user can now enter only whitespace (including comments) at the
  REPL.
- A new file which is blank / only comments is no longer invalid from
  the perspective of LSP.

Closes #275.
2021-12-02 22:07:09 +00:00
Brent Yorgey
b2546b13ce
generalize scan to return () + string (#290)
Players can ignore the return value until they have the capability to
deal with sum types.  `scan` still has the effect of adding
information to one's inventory.

Closes #267.
2021-12-02 21:59:59 +00:00
Brent Yorgey
f37ce109af activate reprogrammed robots
Fixes #287.
2021-11-18 11:27:47 -06:00
Ondřej Šebek
a0e65d9e66
Add vscode editor integration (#255)
- adds `editors` directory
- migrates [xsebek/swarm-vscode](https://github.com/xsebek/swarm-vscode) repo to `editors/`
- updates the highlighting (drill, sum types,...)
- adds some notes how to test and build the extension
- part of #100 
- closes #123
2021-11-11 16:15:17 +00:00
Ondřej Šebek
e5166e31c9
Hide unknown entities with ? (#276)
- closes #49 

**TODO:**
- [x] update TUTORIAL.md
- [x] add a creative mode check
2021-11-07 15:03:20 +00:00
Ondřej Šebek
89e613c678
Add the CI file check to mergify (#283)
I made a mistake in #279, for PRs that modify both .hs an .md the dummy would pass 🙄

Copying the logic to .mergify.yml should fix this. 🤞
2021-11-07 15:55:03 +01:00
Ondřej Šebek
95f2247657
Filter paths that run CI (#279)
* Filter paths that run CI
* Add dummy CI to check
2021-11-07 14:39:10 +01:00
Brent Yorgey
e94418e5c6
Don't make detonator a standard device (#266)
Closes #260.
2021-11-04 18:26:56 +00:00
Ondřej Šebek
fb79910c20
Add location to type matching errors (#270)
- closes #268 
- computes the location for `SBind` and operators
2021-11-04 17:09:24 +00:00
Ondřej Šebek
05dad31bff
Store history in XDG data directory (#253)
- fixes #136 
- changes the history file format to simple text line per `REPLEntry`
- refactors out a `REPLHistory` type and its pure functions
  - adds tests for REPL logic (move to different previous entry,...)
2021-11-04 14:59:17 +00:00
Alexander Block
8deb10f3b7
allow ' in variable names (#273)
Closes #269 

- The parser now accepts  `'` in a variable name as long as it does not start with `'` (to rule out potentially problematic variable names like `'a'`).
- Unit tests for this are added.
2021-10-30 22:50:57 +00:00
Brent Yorgey
5c8c17d787
Add has and count commands (#265)
Closes #88.

Currently, these commands require no capability and no specific device (you already have an inventory, so why wouldn't you be able to look in it to see what you have?).  However, that makes it impossible to discover these commands without reading the source or tutorial.  Maybe we should make an "inventory" device?  I'm not sure.  Suggestions welcome.  Or maybe once we implement #84 it won't matter since players can discover commands that way.
2021-10-29 19:47:36 +00:00
Brent Yorgey
a990406828 bug fix: initialize store when making item from inventory panel
We used to call `initMachine` with `emptyStore` but this was wrong.
It meant that the store was reset every time you hit Enter on an
inventory item to `make` it.  So e.g. if you had made a `def` and
executed it at least once, then made something from the inventory,
trying to execute the same definition again would yield a fatal
"reference to unknown memory cell" error.
2021-10-28 13:46:29 -05:00
Brent Yorgey
731bf65bfa make scanner provide sensehere capability
4a0d338324
was buggy since I forgot to do this.
2021-10-28 12:56:38 -05:00
Brent Yorgey
4168aa9d3a seed robot program should not have delay brackets!
The delay brackets mean the entire seed robot program simply becomes a
delayed value in a single step, and the seed robot then becomes
inactive, because it has successfully evaluated its program.  This
took me an embarrassingly long time to figure out.

Fixes #264.
2021-10-28 12:31:39 -05:00
Brent Yorgey
f6a222092b
Add some simple property-based tests for arithmetic and comparison (#263) 2021-10-27 11:56:02 +00:00
Ondřej Šebek
bc480a045d
Add list example (#261)
- encodes `list int` inside `int`
- has unit tests written in swarm language
2021-10-26 19:54:04 +00:00
Brent Yorgey
c982e81790
Delay type (#223)
Make explicit in the type system when evaluation of a computation should be delayed.  This gives the user fine-grained control over selective laziness (for example, once we have sum types and recursive types, one could use this to define lazy infinite data structures).  It also allows us to guarantee that certain commands such as `build` and `reprogram` delay evaluation of their arguments, and lets the user e.g. define their own modified versions of `build` without compromising those guarantees.

- Delay is indicated by curly braces both at the value and type levels, that is, if `t : ty` then `{t} : {ty}`.
- `force : {ty} -> ty` is now exposed in the surface language.
- Change from a CEK machine to a CESK machine. Recursive `let` and `def` delay via allocating a cell in the store.  For now, there is no other way to allocate anything in the store, but see discussion at #150 for some possible future directions.
- change the types of `build` and `reprogram` to require a delayed program, e.g. `build : string -> {cmd a} -> cmd string`
- `if` and `try` also require delayed arguments.
- don't elaborate Build and Reprogram with extra Delay wrappers since one is now required by the type
- Division by zero, negative exponents, and bad comparisons now throw exceptions.

Closes #150.  Closes #226.
2021-10-25 13:28:41 +00:00
Brent Yorgey
4f33b22611 remove redundant import 2021-10-22 11:58:10 -05:00
Brent Yorgey
cdd3aff892 remove https://github.com/jtdaugherty/brick/issues/337 workaround
This issue is fixed in `brick-0.64.2`.
2021-10-22 11:55:32 -05:00
Paul Brauner
b72e09edb1
Use fused-effects for the game logic (#256)
Resolves #228.

I've redefined the missing lens operators in `Utils.hs` in order to keep the diff of the function bodies minimal. Hopefully that will make merges and rebases less painful. Most of the type signatures are modified though, we can't avoid this.
2021-10-21 22:44:56 +00:00
Alexander Block
969b0d191c
Only append entry to REPL history if it is not the same as last entry (#252)
Closes #75. 
A REPL entry is only appended to the REPL history if it is not a duplicate of the last entry.
2021-10-20 21:13:40 +00:00
Ondřej Šebek
400d101736
Add ($) operator (#239)
- add `$` operator to avoid multiple parentheses
2021-10-20 06:54:08 +00:00
Ondřej Šebek
0327595a9e Fix back direction 2021-10-19 21:23:44 +02:00
Ondřej Šebek
fcc915ac18
Cap ticks in model (#246)
- fixes  #245
2021-10-19 19:20:06 +00:00
Ishan Bhanuka
150dc9b396
Test example .sw files to see if they type check (#233)
Closes #180 

Add `Integration.hs` which reads files from `example` folder and runs `processTerm` on the contents. The test passes if creating a term is successful.

Currently only one example i.e. `zigzag.sw` is failing.
2021-10-19 02:49:32 +00:00
Brent Yorgey
22a8de35b6
Add sum types (#224)
Adds sum types (e.g. `int + bool`) along with constants `inl : a -> a + b`, `inr : b -> a + b`, and `case : a + b -> (a -> c) -> (b -> c) -> c`.

I considered whether to automatically wrap the arguments to `case` in `delay`, like the arguments to `if`, but they are already functions anyway so we get a lot of laziness for free.  It would only make a difference if you wrote something like `case foo (let x = blah in \y. ...) (...)` in which case the `let x = blah` is going to be evaluated eagerly, *before* we know which branch we are going to take.  I don't think it's worth bothering about.  In the normal case that you write `case foo (\y. ...) (\z. ...)` then only one of the two functions gets run at all, without us having to do anything special with `delay`.

I also considered whether to make special syntax for case expressions, like `case foo of { inl x -> ... ; inr y -> ... }` but doing it as a simple built-in function is both easier to implement and feels like it fits better with the aesthetic of the language so far.

Closes #46 .
2021-10-16 23:17:42 +00:00
Ondřej Šebek
c30b710a84
Fix operator parsing (#237)
- add check that operator is not followed by another "operator"

Fixes #236
2021-10-16 14:39:49 +00:00
Brent Yorgey
8677861b38 TUTORIAL: fix salvager instructions 2021-10-16 08:30:21 -05:00
Ondřej Šebek
d21cbd6c43 Fix zigzag example 2021-10-16 14:52:39 +02:00
Ishan Bhanuka
07d93a9d49
Refactor robot context information into a single record (#198)
Closes #190
2021-10-16 11:04:29 +00:00
Ishan Bhanuka
4265233637
Generate direction helper methods from consolidated direction information in DirInfo records (#230)
Closes #183
2021-10-16 10:40:16 +00:00
Brent Yorgey
db46f60cb7 center all recipe requirements 2021-10-14 14:42:44 -05:00
Ondřej Šebek
e8f8c61273
Add drill command (#186)
- adds drill capability (from drill entity) (Closes #143)
- adds drilling recipes
- adds (non-portable) entities:
  - mountain tunnel (shows on top of robots)
  - copper vein (blocking, is "inside mountain") 
  - copper mine (Closes #18)
- adds time parameter to (drilling) recipes
- fixes a bug showing seed robots on top of entities
2021-10-14 03:07:21 +00:00
Brent Yorgey
a52bc0557c activate the base when making a recipe from the inventory list
Since the base was not put into the active robots set, it never made
any progress on making the recipe.

Fixes #227.  This just goes to prove @polux's point in #217.
2021-10-13 21:39:58 -05:00
Ishan Bhanuka
a80d2904b4
Refactor getx and gety into whereami command (#220)
Closes #178
2021-10-12 20:19:38 +00:00
Brent Yorgey
50e5345222 emacs syntax highlighting: get // ... and /* ... */ both to work
Learned how to do this from
http://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Flags.html .
2021-10-12 15:12:50 -05:00
Brent Yorgey
5d9f302e03
basic syntax highlighting for emacs mode (#219)
There are still some things missing (e.g. operators) but this is a start.  Some remaining issues:

* We should look into auto-generating the lists of keywords so we don't have to manually update every time we add new commands.  Or (fancier) maybe the swarm executable can have a special flag to make it spit out lists of command names, so the emacs mode can dynamically find out what to highlight?
* I am not sure how to get it to highlight both single-line and multi-line comments at the same time.  It must be possible since e.g. `java-mode` does it.  We could also change the comment syntax, I really don't care that much.
2021-10-12 20:07:04 +00:00