Add bit operators, defer, list methods, rand, assert and rex test
- Bit operators & | ^ ~ << >> and their op= forms (Go precedence),
plus hex, binary and octal literals (0xff, 0b1010, 0o755)
- defer stmt / defer { ... }: runs when the block ends, last first,
also on return, ?, break and continue
- List methods insert, remove, clear, contains, index_of, reverse,
sort and sort_by (stable merge sort)
- rand module: int(a, b), float(), seed(n)
- assert(cond[, msg]) and `rex test`, which runs each test_ function
in its own process
- rex.toml marks a project's top folder: imports count from there
- Docs, formatter, LSP completions and editor grammars updated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
910bde692a
commit
300d767124
28 files changed
+1373
-65
No files matched your search
@@ -42,6 +42,26 @@ func main() {
|
||||
- Every file has its own names, so two files can both define `helper`.
|
||||
- Import loops and missing files are errors. An error message names the file it is in.
|
||||
|
||||
## Bigger projects
|
||||
|
||||
When a project has several folders, put an empty file named `rex.toml` in its top folder.
|
||||
Every import is then counted from that folder, in every file, wherever the file sits:
|
||||
|
||||
```text
|
||||
Klein/
|
||||
rex.toml
|
||||
engine/engine.rex
|
||||
game/app/main.rex
|
||||
```
|
||||
|
||||
```rex
|
||||
import engine.engine // the same line works in game/app/main.rex and in any other file
|
||||
```
|
||||
|
||||
Rex finds the nearest `rex.toml` above the file you run (and above each file you open in an
|
||||
editor). Without one, imports are counted from the folder of the file you run, as above. With
|
||||
one, even a file next to your main file is imported with its full path (`import game.app.util`).
|
||||
|
||||
## Built-in modules
|
||||
|
||||
Import them like your own files: `import os`.
|
||||
@@ -79,6 +99,33 @@ func main() {
|
||||
|
||||
`time.now()` gives seconds as a float. `time.sleep(seconds)` waits.
|
||||
|
||||
### rand
|
||||
|
||||
| | |
|
||||
|--|--|
|
||||
| `rand.int(a, b)` | a whole number from `a` up to, **not including**, `b` (like `a..b`) |
|
||||
| `rand.float()` | a number from 0 up to, not including, 1 |
|
||||
| `rand.seed(n)` | start from a fixed point: the same seed gives the same numbers every run |
|
||||
|
||||
Without `rand.seed`, every run gives different numbers.
|
||||
|
||||
```rex
|
||||
import rand
|
||||
|
||||
func main() {
|
||||
var die = rand.int(1, 7) // 1 to 6
|
||||
print(die >= 1 && die <= 6)
|
||||
var hit = rand.float() < 0.25 // true one time in four
|
||||
print(hit == true || hit == false)
|
||||
}
|
||||
```
|
||||
```text
|
||||
true
|
||||
true
|
||||
```
|
||||
|
||||
Not for passwords or keys: it is fast, not secure.
|
||||
|
||||
### Strings
|
||||
|
||||
`upper lower trim split lines fields replace repeat contains starts_with ends_with
|
||||
@@ -104,6 +151,44 @@ xxx hello a+b
|
||||
|
||||
A list of strings can be joined: `["a", "b"].join("-")`.
|
||||
|
||||
## Testing
|
||||
|
||||
`assert(condition)` stops the program when the condition is false, with the line it is on.
|
||||
Add a message as a second argument: `assert(n > 0, "n is {n}")`.
|
||||
|
||||
Write tests as functions whose names start with `test_`, in any `.rex` file. They take
|
||||
nothing and return nothing, or an `error` (a returned error fails the test).
|
||||
|
||||
```rex
|
||||
func add(a, b: int): int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
func test_add() {
|
||||
assert(add(2, 2) == 4)
|
||||
assert(add(-1, 1) == 0, "adding opposites")
|
||||
}
|
||||
|
||||
func test_parse(): error {
|
||||
var n = "42".to_int()?
|
||||
assert(n == 42)
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
`rex test` runs the tests in every `.rex` file in this folder and the folders below it
|
||||
(or `rex test file.rex`, `rex test folder`):
|
||||
|
||||
```text
|
||||
ok calc.rex: test_add
|
||||
ok calc.rex: test_parse
|
||||
|
||||
2 passed, 0 failed
|
||||
```
|
||||
|
||||
Every test runs on its own, so one that fails (or stops with a panic) doesn't stop the rest.
|
||||
What a failed test printed is shown under it. A file's `main` is not run by `rex test`.
|
||||
|
||||
## A program that reads input
|
||||
|
||||
```rex
|
||||
|
||||
Reference in new issue
Block a user