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
+21
-5
@@ -5,14 +5,15 @@
|
||||
var x = 1 // changeable; type inferred
|
||||
var y: float = 2 // with a type
|
||||
const MAX = 100 // fixed
|
||||
x += 1 // also -= *= /= %=
|
||||
x += 1 // also -= *= /= %= &= |= ^= <<= >>=
|
||||
print("x is {x}", y) // interpolation with { }
|
||||
```
|
||||
|
||||
## Types
|
||||
`int float bool char string error` · sized: `i8 i16 i32 u8 u16 u32 u64 f32` · `[]T` list ·
|
||||
`map[K, V]` map · `T?` optional · `shared T` · `weak T` · `func(A, B): R` function.
|
||||
Convert numbers with `float(x)`, `int(x)`, `u8(x)`.
|
||||
Convert numbers with `float(x)`, `int(x)`, `u8(x)`. Literals: `1_000` `0xff` `0b1010` `0o755`.
|
||||
Bits: `a & b` `a | b` `a ^ b` `~a` `a << n` `a >> n` (`&` `<<` `>>` bind like `*`; `|` `^` like `+`).
|
||||
|
||||
## Control flow
|
||||
```rex
|
||||
@@ -35,6 +36,8 @@ var n, err = f()
|
||||
var n = f()? // inside a function that returns error
|
||||
var g = func(x: int): int { return x * 2 }
|
||||
var h = x => x * 2 // where the type is known
|
||||
defer f.close() // runs when the block ends (last defer first)
|
||||
defer { ... }
|
||||
```
|
||||
|
||||
## Lists
|
||||
@@ -42,6 +45,9 @@ var h = x => x * 2 // where the type is known
|
||||
var xs = [1, 2, 3] var e: []int
|
||||
xs.push(4) xs.pop() xs.len()
|
||||
xs[0] xs[0] = 9 xs[1..3]
|
||||
xs.insert(i, v) xs.remove(i) xs.clear() xs.reverse()
|
||||
xs.contains(v) xs.index_of(v) // -1 if missing
|
||||
xs.sort() xs.sort_by((a, b) => a.age < b.age) // stable
|
||||
```
|
||||
|
||||
## Maps
|
||||
@@ -116,10 +122,19 @@ var s: Shape = Circle{1.0}
|
||||
import utils // utils.rex next to this file
|
||||
import tools.fs // tools/fs.rex, used as fs.name
|
||||
import ..shared // ../shared.rex (...x = two folders up)
|
||||
import os // built-in: os math time
|
||||
// rex.toml in the top folder: imports count from there, in every file
|
||||
import os // built-in: os math time rand
|
||||
pub func f() {} // visible to other files
|
||||
```
|
||||
|
||||
## Testing
|
||||
```rex
|
||||
assert(x > 0) assert(x > 0, "x is {x}") // stops the program if false
|
||||
func test_add() { assert(add(1, 2) == 3) } // `rex test` runs every test_ function
|
||||
func test_load(): error { ... } // a returned error fails the test
|
||||
rand.int(1, 7) rand.float() rand.seed(42) // import rand; 1..6, 0..1
|
||||
```
|
||||
|
||||
## C libraries
|
||||
```rex
|
||||
import "zlib.h" // zlib.crc32(...), zlib.Z_OK, types like zlib.gzFile
|
||||
@@ -130,8 +145,9 @@ lib.fill(buf, n) // T* parameters take a variable or a list
|
||||
```
|
||||
|
||||
## Commands
|
||||
`rex run f.rex args…` · `rex build f.rex -o out` · `rex check f.rex` · `rex emit-c f.rex` · `rex fmt path` (`--check` to only report) · `rex lsp` (editor server) · `rex header x.h [word]` (what a C header offers)
|
||||
`rex run f.rex args…` · `rex build f.rex -o out` · `rex check f.rex` · `rex test [path]` · `rex emit-c f.rex` · `rex fmt path` (`--check` to only report) · `rex lsp` (editor server) · `rex header x.h [word]` (what a C header offers)
|
||||
|
||||
## Things that stop the program (exit code 2)
|
||||
Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing
|
||||
map key with `m[k]` · using a `nil` or gone `weak` value · calling a `nil` function or interface.
|
||||
map key with `m[k]` · using a `nil` or gone `weak` value · calling a `nil` function or interface ·
|
||||
a failed `assert` · shifting by a negative amount · `insert`/`remove` out of range.
|
||||
Reference in new issue
Block a user