From 300d76712439446e5c21c6d620f0a7f77d0fb400 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 12 Nov 2026 23:30:05 +0100 Subject: [PATCH] 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 --- docs/02-values-and-types.md | 45 ++++ docs/04-functions.md | 47 ++++ docs/05-lists-and-maps.md | 62 ++++++ docs/10-modules-and-stdlib.md | 85 +++++++ docs/11-cheatsheet.md | 26 ++- docs/README.md | 4 +- editors/vim/syntax/rex.vim | 6 +- editors/vscode/syntaxes/rex.tmLanguage.json | 4 +- src/ast.rs | 14 ++ src/check.rs | 192 ++++++++++++++-- src/codegen_c.rs | 233 ++++++++++++++++++-- src/driver.rs | 38 +++- src/fmt.rs | 16 +- src/hir.rs | 57 ++++- src/lexer.rs | 91 +++++++- src/loader.rs | 23 +- src/lsp.rs | 17 +- src/main.rs | 75 +++++++ src/moves.rs | 15 +- src/parser.rs | 28 +++ src/runtime.c | 94 ++++++++ src/stdlib.rs | 9 +- tests/fmt.rs | 7 + tests/imports.rs | 48 ++++ tests/lsp.rs | 21 ++ tests/parse.rs | 12 +- tests/run.rs | 121 +++++++++- tests/test_cmd.rs | 48 ++++ 28 files changed, 1373 insertions(+), 65 deletions(-) create mode 100644 tests/test_cmd.rs diff --git a/docs/02-values-and-types.md b/docs/02-values-and-types.md index 4fb0058..081c11a 100644 --- a/docs/02-values-and-types.md +++ b/docs/02-values-and-types.md @@ -77,6 +77,51 @@ Rules worth remembering: - A number literal adapts to the type it is used with (`var b: u8 = 5`). - Dividing by zero stops the program with a clear message. - Converting a float to an integer cuts off the decimals. +- Whole numbers can be written in hex, binary or octal: `0xff`, `0b1010`, `0o755`. + `_` may separate digits: `1_000_000`. + +### Bits + +For flags, colors, hashes and file formats, whole numbers have bit operators: + +| | | | +|--|--|--| +| `a & b` | and | bits set in both | +| `a \| b` | or | bits set in either | +| `a ^ b` | xor | bits set in one but not the other | +| `~a` | not | every bit flipped | +| `a << n` | shift left | same as `a * 2ⁿ` (bits pushed out are lost) | +| `a >> n` | shift right | same as `a / 2ⁿ`, rounding down | + +Each has an assignment form too: `&= |= ^= <<= >>=`. + +```rex +const BOLD = 0b001 +const ITALIC = 0b010 +const UNDERLINE = 0b100 + +func main() { + var style = BOLD | UNDERLINE + print(style & ITALIC != 0, style & UNDERLINE != 0) + style ^= BOLD // switch bold off + print(style) + + var color: u32 = 0xff8800 + var red = color >> 16 & 0xff + var green = color >> 8 & 0xff + print(red, green, 1 << 10, ~0) +} +``` +```text +false true +4 +255 136 1024 -1 +``` + +- `&` and the shifts bind like `*`, `|` and `^` like `+`, so `x & 1 == 0` means `(x & 1) == 0`. +- They work on whole numbers only (`int`, `u8`, ...). For `bool` use `&&` and `||`. +- Shifting by the number's width or more gives 0 (or -1 for `>>` of a negative number); + shifting by a negative amount stops the program. ## Strings diff --git a/docs/04-functions.md b/docs/04-functions.md index 9e90911..250c053 100644 --- a/docs/04-functions.md +++ b/docs/04-functions.md @@ -129,4 +129,51 @@ func main() { 11 ``` +## `defer`: clean up at the end + +`defer` puts a line aside and runs it when the block it is in ends, however it ends: at the +closing `}`, at a `return`, at a `?` that passes an error up, or at `break` / `continue` in a +loop. Use it to keep the cleanup right next to the thing that needs it. + +```rex +func process(name: string): error { + print("open", name) + defer print("close", name) // runs last, whatever happens below + if name.ends_with(".tmp") { + return error("{name}: skipped") + } + print("work on", name) + return nil +} + +func main() { + var err = process("a.txt") + err = process("b.tmp") + print(err) +} +``` +```text +open a.txt +work on a.txt +close a.txt +open b.tmp +close b.tmp +b.tmp: skipped +``` + +- Several `defer`s run in reverse: the last one written runs first (close in the opposite + order you opened). +- `defer { ... }` runs a whole block. +- The deferred code sees the variables as they are **when it runs**, not when `defer` was written. +- In a loop body, it runs at the end of every round. +- `return`, `?` and `break` can't be used inside the deferred code itself: it runs while + the block is already finishing. +- It is most useful with things you must close by hand, like files from C libraries: + +```rex +var f = stdio.fopen(path, "r") +if f == nil { return error("can't open {path}") } +defer stdio.fclose(f) +``` + Next: [Lists and maps](05-lists-and-maps.md) diff --git a/docs/05-lists-and-maps.md b/docs/05-lists-and-maps.md index 53e80b8..1f26024 100644 --- a/docs/05-lists-and-maps.md +++ b/docs/05-lists-and-maps.md @@ -30,6 +30,68 @@ func main() { 61 ``` +### More list methods + +```rex +func main() { + var xs = [5, 3, 9] + xs.insert(0, 7) // put 7 at position 0; the rest move up + print(xs) + print(xs.remove(1), xs) // take out the item at position 1 and get it + print(xs.contains(9), xs.index_of(9), xs.index_of(4)) + xs.sort() + print(xs) + xs.reverse() + print(xs) + xs.clear() + print(xs, xs.len()) +} +``` +```text +[7, 5, 3, 9] +5 [7, 3, 9] +true 2 -1 +[3, 7, 9] +[9, 7, 3] +[] 0 +``` + +| | | +|--|--| +| `xs.push(v)` / `xs.pop()` | add at the end / take the last one | +| `xs.insert(i, v)` | put `v` at position `i` (`i` may be `xs.len()`, the end) | +| `xs.remove(i)` | take out the item at `i` and give it back | +| `xs.clear()` | remove everything | +| `xs.contains(v)` / `xs.index_of(v)` | is it there? / its first position, or `-1` | +| `xs.reverse()` | turn the list around | +| `xs.sort()` | smallest first (numbers, `char`s, strings A-Z) | +| `xs.sort_by(less)` | your own order: `less(a, b)` says whether `a` goes before `b` | + +`contains` and `index_of` work where `==` does (numbers, strings, `char`, `bool`, enums +without data). For structs, sort with `sort_by`: + +```rex +struct Player { + name: string + score: int +} + +func main() { + var ps = [Player{"ann", 30}, Player{"bob", 50}, Player{"cy", 30}] + ps.sort_by((a, b) => a.score > b.score) // highest first + for p in ps { + print(p.name, p.score) + } +} +``` +```text +bob 50 +ann 30 +cy 30 +``` + +Sorting is **stable**: items that compare equal keep their order (ann stays before cy). + Facts: - An empty list needs a type: `var xs: []int`. - Going outside the list (`nums[9]`) or popping an empty list stops the program with a message. diff --git a/docs/10-modules-and-stdlib.md b/docs/10-modules-and-stdlib.md index c51e082..feead5c 100644 --- a/docs/10-modules-and-stdlib.md +++ b/docs/10-modules-and-stdlib.md @@ -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 diff --git a/docs/11-cheatsheet.md b/docs/11-cheatsheet.md index 80f2689..1657c65 100644 --- a/docs/11-cheatsheet.md +++ b/docs/11-cheatsheet.md @@ -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. diff --git a/docs/README.md b/docs/README.md index e24e515..5e3e175 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,13 +12,13 @@ These notes teach it from zero. Read them in order, or jump to the | 1 | [First steps](01-first-steps.md) | Running a program, `print`, `main` | | 2 | [Values and types](02-values-and-types.md) | `var`, `const`, numbers, strings | | 3 | [Control flow](03-control-flow.md) | `if`, `for`, `match` | -| 4 | [Functions](04-functions.md) | Parameters, results, closures | +| 4 | [Functions](04-functions.md) | Parameters, results, closures, `defer` | | 5 | [Lists and maps](05-lists-and-maps.md) | Collections | | 6 | [Structs and enums](06-structs-and-enums.md) | Your own types, methods | | 7 | [Errors and nil](07-errors-and-nil.md) | `error`, `?`, `T?` | | 8 | [Memory](08-memory.md) | Copies, `shared`, `weak` | | 9 | [Generics and interfaces](09-generics-and-interfaces.md) | Code that works for many types | -| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time` | +| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time`, `rand`, tests | | 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page | | 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files | | 13 | [C and C++ libraries](13-c-libraries.md) | `import "raylib.h"`, your own C code | diff --git a/editors/vim/syntax/rex.vim b/editors/vim/syntax/rex.vim index 4b11aee..0aeee59 100644 --- a/editors/vim/syntax/rex.vim +++ b/editors/vim/syntax/rex.vim @@ -3,15 +3,15 @@ if exists("b:current_syntax") finish endif -syn keyword rexControl if else for in step match return break continue +syn keyword rexControl if else for in step match return break continue defer syn keyword rexDeclare func var const import struct enum interface pub syn keyword rexModifier shared weak syn keyword rexConstant true false nil syn keyword rexType int float bool char string error map i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 syn match rexTypeName "\<[A-Z][A-Za-z0-9_]*\>" -syn match rexNumber "\<[0-9][0-9_]*\(\.[0-9][0-9_]*\)\?\>" +syn match rexNumber "\<\(0[xX][0-9a-fA-F_]\+\|0[bB][01_]\+\|0[oO][0-7_]\+\|[0-9][0-9_]*\(\.[0-9][0-9_]*\)\?\)\>" syn match rexCall "\<[a-z_][A-Za-z0-9_]*\ze\s*(" -syn match rexOperator "=>\|\.\.\|==\|!=\|<=\|>=\|&&\|||\|[-+*/%]=\?\|[<>!?=]" +syn match rexOperator "=>\|\.\.\|==\|!=\|<<=\=\|>>=\=\|<=\|>=\|&&\|||\|[-+*/%&|^]=\?\|[<>!?=~]" syn match rexEscape "\\." contained syn region rexInterp matchgroup=rexInterpEdge start="{" end="}" contained contains=TOP syn region rexString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rexEscape,rexInterp diff --git a/editors/vscode/syntaxes/rex.tmLanguage.json b/editors/vscode/syntaxes/rex.tmLanguage.json index 3255b32..72c8201 100644 --- a/editors/vscode/syntaxes/rex.tmLanguage.json +++ b/editors/vscode/syntaxes/rex.tmLanguage.json @@ -62,7 +62,7 @@ }, "keywords": { "patterns": [ - { "name": "keyword.control.rex", "match": "\\b(if|else|for|in|step|match|return|break|continue)\\b" }, + { "name": "keyword.control.rex", "match": "\\b(if|else|for|in|step|match|return|break|continue|defer)\\b" }, { "name": "keyword.declaration.rex", "match": "\\b(func|var|const|import|struct|enum|interface|pub)\\b" }, { "name": "storage.modifier.rex", "match": "\\b(shared|weak)\\b" } ] @@ -73,7 +73,7 @@ }, "numbers": { "name": "constant.numeric.rex", - "match": "\\b[0-9][0-9_]*(?:\\.[0-9][0-9_]*)?\\b" + "match": "\\b(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|[0-9][0-9_]*(?:\\.[0-9][0-9_]*)?)\\b" }, "types": { "patterns": [ diff --git a/src/ast.rs b/src/ast.rs index 724b4d6..64ecd4b 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -151,6 +151,11 @@ pub enum AssignOp { Mul, Div, Rem, + BitAnd, + BitOr, + BitXor, + Shl, + Shr, } #[derive(Debug, Clone, PartialEq)] @@ -174,12 +179,16 @@ pub enum Stmt { Break(Span), Continue(Span), Block(Block), + /// `defer stmt`: run `stmt` when the enclosing block ends + Defer { body: Box, span: Span }, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum UnOp { Neg, Not, + /// `~x`: flip every bit + BitNot, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -197,6 +206,11 @@ pub enum BinOp { Mul, Div, Rem, + BitAnd, + BitOr, + BitXor, + Shl, + Shr, } #[derive(Debug, Clone, PartialEq)] diff --git a/src/check.rs b/src/check.rs index d953ad2..07f7124 100644 --- a/src/check.rs +++ b/src/check.rs @@ -47,7 +47,7 @@ enum Target { } const BUILTINS: &[&str] = &[ - "print", "int", "float", "char", "string", "error", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", + "print", "assert", "int", "float", "char", "string", "error", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", ]; /// The number type a conversion like `u8(x)` produces. @@ -148,6 +148,8 @@ struct Checker { next_id: u32, ret: Ty, loops: u32, + /// Inside the code of a `defer` (where `return`, `break` and `?` make no sense). + in_defer: u32, /// Optional variables known not to be `nil` at this point (after `if x != nil`). narrowed: HashSet, /// `import os`: the built-in modules this file may use. @@ -271,6 +273,7 @@ fn idents_stmt(s: &Stmt, out: &mut HashSet) { Stmt::Return { values, .. } => values.iter().for_each(|x| idents_expr(x, out)), Stmt::Break(_) | Stmt::Continue(_) => {} Stmt::Block(b) => idents_block(b, out), + Stmt::Defer { body, .. } => idents_stmt(body, out), } } @@ -377,6 +380,11 @@ fn op_str(op: BinOp) -> &'static str { BinOp::Mul => "*", BinOp::Div => "/", BinOp::Rem => "%", + BinOp::BitAnd => "&", + BinOp::BitOr => "|", + BinOp::BitXor => "^", + BinOp::Shl => "<<", + BinOp::Shr => ">>", } } @@ -530,6 +538,7 @@ impl Checker { next_id: 0, ret: Ty::Unit, loops: 0, + in_defer: 0, narrowed: HashSet::new(), imports: HashMap::new(), cur_mod: String::new(), @@ -1858,6 +1867,7 @@ impl Checker { self.next_id = 0; self.ret = sig.ret; self.loops = 0; + self.in_defer = 0; self.narrowed.clear(); self.cur_struct = owner.and_then(|o| self.type_name(o)); @@ -1978,7 +1988,12 @@ impl Checker { } Stmt::For { kind, body, span } => self.for_stmt(kind, body, *span), Stmt::Return { values, span } => self.return_stmt(values, *span), + Stmt::Defer { body, span } => self.defer_stmt(body, *span), Stmt::Break(span) | Stmt::Continue(span) => { + if self.in_defer > 0 && self.loops == 0 { + let w = if matches!(s, Stmt::Break(_)) { "break" } else { "continue" }; + return Err(Diag::new(format!("`{w}` can't jump out of a `defer`"), *span)); + } if self.loops == 0 { let w = if matches!(s, Stmt::Break(_)) { "break" } else { "continue" }; return Err(Diag::new(format!("`{w}` only makes sense inside a `for` loop"), *span)); @@ -1996,7 +2011,38 @@ impl Checker { Ok(if moves { h } else { self.own(h) }) } + /// `defer f()` / `defer { ... }`: checked where it is written, run when the block ends. + fn defer_stmt(&mut self, body: &Stmt, span: Span) -> PResult { + let saved_loops = std::mem::replace(&mut self.loops, 0); + self.in_defer += 1; + let r = match body { + Stmt::Block(b) => self.block(b), + Stmt::Expr(_) | Stmt::Assign { .. } | Stmt::If { .. } | Stmt::For { .. } | Stmt::Defer { .. } => { + self.scopes.push(HashMap::new()); + let r = self.stmt(body).map(|h| vec![h]); + self.scopes.pop(); + r + } + Stmt::Return { .. } => Err(Diag::new("`defer return` makes no sense: the function is already finishing", span)), + _ => Err(Diag::new("`defer` needs a call or a `{ ... }` block to run later", span)), + }; + self.in_defer -= 1; + self.loops = saved_loops; + let body = r?; + // the deferred code reads its variables when the block ends, so none of them may be + // handed over (moved out) by a `return` before that + walk_stmts(&body, &mut |_| {}, &mut |e| { + if let HExprKind::Local(id, _) = &e.kind { + self.no_move.insert(*id); + } + }); + Ok(HStmt::Defer(body)) + } + fn return_stmt(&mut self, values: &[Expr], span: Span) -> PResult { + if self.in_defer > 0 { + return Err(Diag::new("`return` can't be used inside a `defer`: the function is already finishing", span)); + } let ret = self.ret; match ret { Ty::Unit => match values.first() { @@ -2207,9 +2253,15 @@ impl Checker { AssignOp::Mul => BinOp::Mul, AssignOp::Div => BinOp::Div, AssignOp::Rem => BinOp::Rem, + AssignOp::BitAnd => BinOp::BitAnd, + AssignOp::BitOr => BinOp::BitOr, + AssignOp::BitXor => BinOp::BitXor, + AssignOp::Shl => BinOp::Shl, + AssignOp::Shr => BinOp::Shr, AssignOp::Set => unreachable!(), }; - let rhs = self.expr(value, Some(ty))?; + let shift = matches!(op, AssignOp::Shl | AssignOp::Shr); + let rhs = self.expr(value, if shift { None } else { Some(ty) })?; // `counts[w] += 1` on a key that isn't there yet starts from zero let read = match &place.kind { HExprKind::Index { base, index } if matches!(base.ty, Ty::Map(..)) => { @@ -2529,8 +2581,18 @@ impl Checker { ExprKind::Unary { op, expr } => { // a negated literal gets its type later, so `-128` can be an `i8` let literal = matches!(expr.kind, ExprKind::Int(_) | ExprKind::Float(_)); - let inner = self.expr(expr, if *op == UnOp::Neg && !literal { expect } else { None })?; + let inner_expect = match op { + UnOp::Neg if !literal => expect, + UnOp::BitNot => expect, + _ => None, + }; + let inner = self.expr(expr, inner_expect)?; match (op, inner.ty) { + (UnOp::BitNot, t) if t.is_int() => { + let ty = inner.ty; + Ok(mk(HExprKind::Unary { op: *op, expr: Box::new(inner) }, ty)) + } + (UnOp::BitNot, t) => Err(Diag::new(format!("`~` flips the bits of an integer, found `{t}`"), e.span)), (UnOp::Neg, t) if t.is_number() && !t.is_signed() => { Err(Diag::new(format!("a `{t}` can't be negative, so it can't be negated"), e.span)) } @@ -2576,9 +2638,13 @@ impl Checker { }) } ExprKind::Binary { op, lhs, rhs } => { - let arith = matches!(op, BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem); + let arith = matches!( + op, + BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor + ); + let shift = matches!(op, BinOp::Shl | BinOp::Shr); let sub = if arith { expect } else { None }; - let l = self.expr(lhs, sub)?; + let l = self.expr(lhs, if shift { expect } else { sub })?; // in `x != nil && x.ok`, the right side knows x is set (and `||` with `==`) let mut added: Vec = Vec::new(); if matches!(op, BinOp::And | BinOp::Or) { @@ -2670,6 +2736,12 @@ impl Checker { )), }, ExprKind::Try(inner) => { + if self.in_defer > 0 { + return Err(Diag::new( + "`?` can't be used inside a `defer`: the function is already finishing; handle the error there", + e.span, + )); + } let h = self.expr_raw(inner, None)?; let can_pass_up = match self.ret { Ty::Error => true, @@ -3183,7 +3255,36 @@ impl Checker { Ok(mk(HExprKind::Interp(out), Ty::Str)) } - fn binary_typed(&self, op: BinOp, mut l: HExpr, mut r: HExpr, span: Span) -> PResult { + /// Can two values of this type be compared with `==`? + fn eq_ok(&self, t: Ty) -> bool { + t.is_number() + || matches!(t, Ty::Bool | Ty::Str | Ty::Char | Ty::CPtr(_)) + || matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty())) + } + + /// Can values of this type be put in order with `<`? + fn ord_ok(t: Ty) -> bool { + t.is_number() || t == Ty::Str || t == Ty::Char + } + + fn binary_typed(&self, op: BinOp, mut l: HExpr, r: HExpr, span: Span) -> PResult { + // `x << n`: any integer type can say how far; the result has the type of `x` + if matches!(op, BinOp::Shl | BinOp::Shr) { + if !l.ty.is_int() || !r.ty.is_int() { + return Err(Diag::new( + format!("`{}` shifts the bits of an integer by an integer, found `{}` and `{}`", op_str(op), l.ty, r.ty), + span, + )); + } + let negative = matches!(&r.kind, HExprKind::Int(n) if *n < 0) + || matches!(&r.kind, HExprKind::Unary { op: UnOp::Neg, expr } if matches!(expr.kind, HExprKind::Int(n) if n > 0)); + if is_int_lit(&r) && negative { + return Err(Diag::new("can't shift by a negative amount", span)); + } + let ty = l.ty; + return Ok(mk(HExprKind::Binary { op, lhs: Box::new(l), rhs: Box::new(r) }, ty)); + } + let mut r = r; // `x * 2` where x is a float: the literal becomes a float if l.ty != r.ty { if is_int_lit(&l) && r.ty.is_number() || is_float_lit(&l) && r.ty.is_float() { @@ -3209,18 +3310,17 @@ impl Checker { let ok = match op { BinOp::Add => t.is_number() || t == Ty::Str, BinOp::Sub | BinOp::Mul | BinOp::Div => t.is_number(), - BinOp::Rem => t.is_int(), - BinOp::Eq | BinOp::Ne => { - t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char | Ty::CPtr(_)) - || matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty())) - } - BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => t.is_number() || t == Ty::Str || t == Ty::Char, + BinOp::Rem | BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => t.is_int(), + BinOp::Shl | BinOp::Shr => unreachable!(), + BinOp::Eq | BinOp::Ne => self.eq_ok(t), + BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => Self::ord_ok(t), BinOp::And | BinOp::Or => t == Ty::Bool, }; if !ok { let hint = match t { Ty::Struct(_) if matches!(op, BinOp::Eq | BinOp::Ne) => " (compare the fields one by one)", Ty::Enum(_) if matches!(op, BinOp::Eq | BinOp::Ne) => " (use `match` for enums that carry data)", + Ty::Bool if matches!(op, BinOp::BitAnd | BinOp::BitOr) => " (use `&&` or `||` for \"and\" / \"or\")", _ => "", }; return Err(Diag::new(format!("`{}` doesn't work on `{t}` values{hint}", op_str(op)), span)); @@ -3292,8 +3392,59 @@ impl Checker { self.require_variable(&recv, "pop", name_span)?; Ok(mk(HExprKind::ListPop(Box::new(recv)), *elem)) } + (Ty::List(elem), _) if ListOp::from_name(name).is_some() => { + let op = ListOp::from_name(name).unwrap(); + let elem = *elem; + let call = |recv: HExpr, args: Vec, ty: Ty| mk(HExprKind::ListCall { list: Box::new(recv), op, args }, ty); + if op.changes_list() { + self.require_variable(&recv, name, name_span)?; + } + match op { + ListOp::Insert => { + want_args(2)?; + let i = self.expr_as(&args[0], Ty::Int)?; + let v = self.expr_as_owned(&args[1], elem)?; + Ok(call(recv, vec![i, v], Ty::Unit)) + } + ListOp::Remove => { + want_args(1)?; + let i = self.expr_as(&args[0], Ty::Int)?; + Ok(call(recv, vec![i], elem)) + } + ListOp::Clear | ListOp::Reverse => { + want_args(0)?; + Ok(call(recv, vec![], Ty::Unit)) + } + ListOp::Contains | ListOp::IndexOf => { + want_args(1)?; + if !self.eq_ok(elem) { + return Err(Diag::new( + format!("`{name}` compares items with `==`, which doesn't work on `{elem}` values; write a loop instead"), + name_span, + )); + } + let x = self.expr_as(&args[0], elem)?; + Ok(call(recv, vec![x], if op == ListOp::Contains { Ty::Bool } else { Ty::Int })) + } + ListOp::Sort => { + want_args(0)?; + if !Self::ord_ok(elem) { + return Err(Diag::new( + format!("`sort` needs items that can be compared with `<`, not `{elem}`; use `sort_by((a, b) => ...)`"), + name_span, + )); + } + Ok(call(recv, vec![], Ty::Unit)) + } + ListOp::SortBy => { + want_args(1)?; + let f = self.expr_as(&args[0], Ty::func(vec![elem, elem], Ty::Bool))?; + Ok(call(recv, vec![f], Ty::Unit)) + } + } + } (Ty::List(_), _) => Err(Diag::new( - format!("lists have no method `{name}` (they have: len, push, pop)"), + format!("lists have no method `{name}` (they have: len, push, pop, {})", ListOp::NAMES), name_span, )), (Ty::Str, _) => Err(Diag::new( @@ -3553,6 +3704,19 @@ impl Checker { } } match name { + "assert" => { + if args.is_empty() || args.len() > 2 { + return Err(Diag::new("`assert` takes a condition, and maybe a message: `assert(x > 0, \"x is {x}\")`", span)); + } + let cond = self.expr_as(&args[0], Ty::Bool)?; + let msg = match args.get(1) { + Some(m) => self.expr_as(m, Ty::Str)?, + None => mk(HExprKind::Str(String::new()), Ty::Str), + }; + let file = if self.cur_mod.is_empty() { String::new() } else { format!("{}.rex ", self.cur_mod.replace('.', "/")) }; + let place = mk(HExprKind::Str(format!("{file}line {}", span.line)), Ty::Str); + Ok(mk(HExprKind::Builtin { c: "rex_assert", shape: BuiltinShape::Plain, args: vec![cond, place, msg] }, Ty::Unit)) + } "print" => { let mut hs = Vec::new(); for a in args { @@ -3693,12 +3857,14 @@ impl Checker { let saved_scopes = std::mem::replace(&mut self.scopes, vec![HashMap::new()]); let saved_ret = self.ret; let saved_loops = std::mem::replace(&mut self.loops, 0); + let saved_defer = std::mem::replace(&mut self.in_defer, 0); let saved_narrowed = std::mem::take(&mut self.narrowed); let saved_no_move = std::mem::take(&mut self.no_move); let r = self.closure_inner(&outer, params, &ptys, &mut ret_ty, body, span); self.scopes = saved_scopes; self.ret = saved_ret; self.loops = saved_loops; + self.in_defer = saved_defer; self.narrowed = saved_narrowed; self.no_move = saved_no_move; let (inner_caps, plocals, stmts) = r?; diff --git a/src/codegen_c.rs b/src/codegen_c.rs index cf9edd4..d42e3e7 100644 --- a/src/codegen_c.rs +++ b/src/codegen_c.rs @@ -52,6 +52,7 @@ impl Info { let mut found = false; walk_expr(e, &mut |_| {}, &mut |x| match &x.kind { HExprKind::ListPush { .. } | HExprKind::ListPop(_) | HExprKind::MapCall { op: MapOp::Remove, .. } => found = true, + HExprKind::ListCall { op, .. } if op.changes_list() => found = true, HExprKind::CCall { modes, .. } if modes.iter().any(|m| matches!(m, CArg::Addr | CArg::Buffer)) => found = true, HExprKind::MethodCall { owner, method, .. } if self.is_mut_method(owner, method) => found = true, _ => {} @@ -71,6 +72,17 @@ fn has_index(e: &HExpr) -> bool { } pub fn generate(p: &HProgram) -> String { + generate_with(p, None) +} + +/// For `rex test`: the program's own `main` is replaced by one that runs the test function +/// whose number is the first argument (`tests[n]`, a function without parameters that returns +/// nothing or an `error`). +pub fn generate_tests(p: &HProgram, tests: &[String]) -> String { + generate_with(p, Some(tests)) +} + +fn generate_with(p: &HProgram, tests: Option<&[String]>) -> String { let mut info = Info { types: HashMap::new(), mutating: HashSet::new(), borrowed: HashMap::new() }; let mut enums: HashMap = HashMap::new(); for t in &p.types { @@ -119,7 +131,7 @@ pub fn generate(p: &HProgram) -> String { } let wrappers = collect_wrappers(p); let main_fails = p.funcs.iter().any(|f| f.owner.is_none() && f.name == "main" && f.ret == Ty::Error); - let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new(), c_structs: HashMap::new() }; + let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new(), c_structs: HashMap::new(), helpers_done: HashSet::new() }; for t in &p.types { if let HTypeDef::Struct(HStruct { name, c: Some(c), .. }) = t { g.c_structs.insert(name.clone(), c.clone()); @@ -334,7 +346,21 @@ pub fn generate(p: &HProgram) -> String { g.out = g.out.replacen("/*CLOSURE_PROTOS*/", &protos, 1); let defs = std::mem::take(&mut g.closure_defs); g.out.push_str(&defs); - if main_fails { + if let Some(tests) = tests { + g.out.push_str("int main(int argc, char **argv) {\n rex_set_args(argc, argv);\n if (argc < 2) return 3;\n switch (atoi(argv[1])) {\n"); + for (i, name) in tests.iter().enumerate() { + let f = p.funcs.iter().find(|f| f.owner.is_none() && &f.name == name).expect("a test function"); + let call = func_cname(None, name); + if f.ret == Ty::Error { + g.out.push_str(&format!( + " case {i}: {{\n rex_err e = {call}();\n if (e.set) {{\n fflush(stdout);\n fprintf(stderr, \"error: %.*s\\n\", (int)e.msg.n, e.msg.p);\n rex_err_release(e);\n return 1;\n }}\n return 0;\n }}\n" + )); + } else { + g.out.push_str(&format!(" case {i}: {call}(); return 0;\n")); + } + } + g.out.push_str(" }\n return 3;\n}\n"); + } else if main_fails { g.out.push_str( "int main(int argc, char **argv) {\n rex_set_args(argc, argv);\n rex_err e = d_main();\n if (e.set) {\n fflush(stdout);\n fprintf(stderr, \"error: %.*s\\n\", (int)e.msg.n, e.msg.p);\n rex_err_release(e);\n return 1;\n }\n return 0;\n}\n", ); @@ -603,6 +629,11 @@ fn sym(op: BinOp) -> &'static str { BinOp::Mul => "*", BinOp::Div => "/", BinOp::Rem => "%", + BinOp::BitAnd => "&", + BinOp::BitOr => "|", + BinOp::BitXor => "^", + BinOp::Shl => "<<", + BinOp::Shr => ">>", } } @@ -646,7 +677,9 @@ struct Gen { cur_ret: Ty, /// Heap values owned by the variables of each open block, innermost last. /// They are freed when the block ends (or early, on `return`/`break`/`continue`). - scopes: Vec>, + /// A `defer` in a block is kept here too, in order with the variables, and its code is + /// written out at every way out of the block. + scopes: Vec>, /// Fresh heap values (call results, copies, ...) not yet given to an owner. /// Whoever takes one removes it; the rest are freed at the end of the statement. pending: Vec, @@ -658,6 +691,8 @@ struct Gen { closure_defs: String, /// Structs from C headers: table name -> how C spells the type. c_structs: HashMap, + /// Shared helper functions already written (by name). + helpers_done: HashSet, } #[derive(Clone)] @@ -666,6 +701,13 @@ struct Owned { ty: Ty, } +/// What happens when a block is left: free a variable, or run deferred code. +#[derive(Clone)] +enum Exit { + Drop(Owned), + Run(Vec), +} + struct LoopMark { scopes: usize, pending: usize, @@ -721,7 +763,7 @@ impl Gen { /// Variables own the heap values they hold until the end of their block. fn own_local(&mut self, name: String, ty: Ty) { if self.heap(ty) { - self.scopes.last_mut().expect("inside a function").push(Owned { name, ty }); + self.scopes.last_mut().expect("inside a function").push(Exit::Drop(Owned { name, ty })); } } @@ -747,32 +789,55 @@ impl Gen { self.emit_drops(items); } - /// Free the variables of the innermost block and close it. + /// Leave blocks: free variables and run deferred code, in the order given. + fn emit_exits(&mut self, items: Vec) { + for x in items { + match x { + Exit::Drop(o) => { + let l = self.drop_line(o.ty, &o.name); + self.line(&l); + } + Exit::Run(body) => { + // the deferred code can't jump anywhere, so it never looks at the open loops + let saved = std::mem::take(&mut self.loops); + self.line("{"); + self.nested(&body); + self.line("}"); + self.loops = saved; + } + } + } + } + + /// Free the variables of the innermost block (running its `defer`s) and close it. fn end_scope(&mut self) { - let sc = self.scopes.pop().expect("a scope is open"); - self.emit_drops(sc.into_iter().rev().collect()); + let sc = self.scopes.last().expect("a scope is open").clone(); + self.emit_exits(sc.into_iter().rev().collect()); + self.scopes.pop(); } /// Leaving the function early: free every fresh value and every variable - /// (except `skip`, which is being returned). Nothing is forgotten, the code - /// after still needs them. + /// (except `skip`, which is being returned), and run every `defer`. Nothing is + /// forgotten, the code after still needs them. fn drop_everything(&mut self, skip: &[String]) { - let mut items: Vec = self.pending.iter().rev().cloned().collect(); + let mut items: Vec = self.pending.iter().rev().cloned().map(Exit::Drop).collect(); for sc in self.scopes.iter().rev() { - items.extend(sc.iter().rev().filter(|o| !skip.contains(&o.name)).cloned()); + items.extend( + sc.iter().rev().filter(|x| !matches!(x, Exit::Drop(o) if skip.contains(&o.name))).cloned(), + ); } - self.emit_drops(items); + self.emit_exits(items); } /// `break` / `continue`: free what was created inside the loop body. fn drop_loop_exit(&mut self) { let m = self.loops.last().expect("inside a loop"); let (sc, pd) = (m.scopes, m.pending); - let mut items: Vec = self.pending[pd..].iter().rev().cloned().collect(); + let mut items: Vec = self.pending[pd..].iter().rev().cloned().map(Exit::Drop).collect(); for scope in self.scopes[sc..].iter().rev() { items.extend(scope.iter().rev().cloned()); } - self.emit_drops(items); + self.emit_exits(items); } /// C code that deep-copies the value `e` of type `ty` (or just reads it, if that is enough). @@ -1544,16 +1609,53 @@ static dstr {c}_str({c} m) {{ static {c} {c}_copy({c} l);\n\ static void {c}_drop({c} *l);\n\ static {c} {c}_slice({c} l, int64_t lo, int64_t hi);\n\ - static dstr {c}_str({c} l);\n" + static dstr {c}_str({c} l);\n\ + static void {c}_insert({c} *l, int64_t i, {t} v);\n\ + static {t} {c}_remove({c} *l, int64_t i);\n\ + static void {c}_clear({c} *l);\n\ + static void {c}_reverse({c} *l);\n" ) } + /// C code that says whether `a == b` for values of type `ty`, if `==` works on them. + fn eq_c(&self, ty: Ty, a: &str, b: &str) -> Option { + match ty { + Ty::Str => Some(format!("rex_streq({a}, {b})")), + Ty::Enum(n) if self.enums.get(n).map_or(false, |e| e.variants.iter().all(|v| v.fields.is_empty())) => { + Some(format!("({a}.tag == {b}.tag)")) + } + t if t.is_number() || matches!(t, Ty::Bool | Ty::Char | Ty::CPtr(_)) => Some(format!("({a} == {b})")), + _ => None, + } + } + + /// C code that says whether `a < b`, if `<` works on values of type `ty`. + fn less_c(ty: Ty, a: &str, b: &str) -> Option { + match ty { + Ty::Str => Some(format!("(rex_strcmp({a}, {b}) < 0)")), + t if t.is_number() || t == Ty::Char => Some(format!("({a} < {b})")), + _ => None, + } + } + fn list_bodies(&self, l: Ty) -> String { let Ty::List(elem) = l else { unreachable!() }; let (c, t) = (c_ty(l), c_ty(*elem)); let copy_i = self.copy_of(*elem, "l.p[i]"); let copy_lo = self.copy_of(*elem, "l.p[lo + i]"); let elem_str = to_str(*elem, "l.p[i]"); + // `index_of` / `contains` and `sort` only exist where `==` / `<` work on the items + let mut extra = String::new(); + if let Some(eq) = self.eq_c(*elem, "l->p[i]", "x") { + extra.push_str(&format!( + "\nstatic int64_t {c}_index_of(const {c} *l, {t} x) {{\n for (int64_t i = 0; i < l->len; i++) if ({eq}) return i;\n return -1;\n}}\n" + )); + } + if let Some(lt) = Self::less_c(*elem, &format!("*(const {t} *)a"), &format!("*(const {t} *)b")) { + extra.push_str(&format!( + "\nstatic bool {c}_less(void *ctx, const void *a, const void *b) {{\n (void)ctx;\n return {lt};\n}}\n" + )); + } let drop_elem = if self.info.needs_copy(*elem) { format!(" for (int64_t i = 0; i < l->len; i++) {}\n", self.drop_line(*elem, "l->p[i]")) } else { @@ -1611,6 +1713,33 @@ static {c} {c}_slice({c} l, int64_t lo, int64_t hi) {{ return r; }} +static void {c}_insert({c} *l, int64_t i, {t} v) {{ + if (i < 0 || i > l->len) rex_index_panic(i, l->len); + {c}_push(l, v); + memmove(&l->p[i + 1], &l->p[i], (size_t)(l->len - 1 - i) * sizeof({t})); + l->p[i] = v; +}} + +static {t} {c}_remove({c} *l, int64_t i) {{ + if (i < 0 || i >= l->len) rex_index_panic(i, l->len); + {t} v = l->p[i]; + memmove(&l->p[i], &l->p[i + 1], (size_t)(l->len - 1 - i) * sizeof({t})); + l->len--; + return v; +}} + +static void {c}_clear({c} *l) {{ +{drop_elem} l->len = 0; +}} + +static void {c}_reverse({c} *l) {{ + for (int64_t i = 0, j = l->len - 1; i < j; i++, j--) {{ + {t} x = l->p[i]; + l->p[i] = l->p[j]; + l->p[j] = x; + }} +}} +{extra} static dstr {c}_str({c} l) {{ dstr *parts = (dstr *)malloc(sizeof(dstr) * (size_t)(l.len * 2 + 2)); if (!parts) rex_panic("out of memory"); @@ -1882,6 +2011,9 @@ static dstr {c}_str({c} l) {{ self.nested(b); self.line("}"); } + HStmt::Defer(b) => { + self.scopes.last_mut().expect("inside a function").push(Exit::Run(b.clone())); + } } } @@ -2270,6 +2402,7 @@ static dstr {c}_str({c} l) {{ } UnOp::Neg => format!("(-{v})"), UnOp::Not => format!("(!{v})"), + UnOp::BitNot => format!("(({})(~{v}))", c_ty(e.ty)), } } HExprKind::Binary { op, lhs, rhs } => self.binary(*op, lhs, rhs), @@ -2555,6 +2688,7 @@ static dstr {c}_str({c} l) {{ let l = self.place_str(list); self.materialize(e.ty, format!("{}_pop(&{l})", c_ty(list.ty))) } + HExprKind::ListCall { list, op, args } => self.list_call(e, list, *op, args), HExprKind::Len(inner) => { let v = self.expr(inner); if inner.ty == Ty::Str { @@ -2716,6 +2850,70 @@ static dstr {c}_str({c} l) {{ /// Store a call's result in a temporary so it runs now, in order. /// A C lvalue for a map, whose address can be taken. + fn list_call(&mut self, e: &HExpr, list: &HExpr, op: ListOp, args: &[HExpr]) -> String { + let c = c_ty(list.ty); + let Ty::List(elem) = list.ty else { unreachable!() }; + let et = c_ty(*elem); + if !op.changes_list() { + // `contains` / `index_of` only read the list, which may be a temporary + let l = self.map_ref(list); + let v = self.expr(&args[0]); + let call = format!("{c}_index_of(&{l}, {v})"); + return if op == ListOp::Contains { format!("({call} >= 0)") } else { self.materialize(Ty::Int, call) }; + } + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + match op { + ListOp::Insert => { + if self.heap(*elem) { + self.take(&vs[1]); + } + let l = self.place_str(list); + self.line(&format!("{c}_insert(&{l}, {}, {});", vs[0], vs[1])); + String::new() + } + ListOp::Remove => { + let l = self.place_str(list); + self.materialize(e.ty, format!("{c}_remove(&{l}, {})", vs[0])) + } + ListOp::Clear | ListOp::Reverse => { + let l = self.place_str(list); + let name = if op == ListOp::Clear { "clear" } else { "reverse" }; + self.line(&format!("{c}_{name}(&{l});")); + String::new() + } + ListOp::Sort => { + let l = self.place_str(list); + self.line(&format!("rex_msort({l}.p, {l}.len, sizeof({et}), {c}_less, NULL);")); + String::new() + } + ListOp::SortBy => { + // the comparison is a function value: a small C function calls it for the sort + let fty = args[0].ty; + let fc = c_ty(fty); + let tramp = format!("SB_{fc}"); + if self.helpers_done.insert(tramp.clone()) { + let (ca, cb) = (self.copy_of(*elem, &format!("*(const {et} *)a")), self.copy_of(*elem, &format!("*(const {et} *)b"))); + self.closure_protos.push_str(&format!( + "static bool {tramp}(void *ctx, const void *a, const void *b) {{\n const {fc} *f = (const {fc} *)ctx;\n return f->fn(f->env, {ca}, {cb});\n}}\n" + )); + } + let f = if is_place(&args[0]) { + vs[0].clone() + } else { + let t = self.fresh(); + self.line(&format!("{fc} {t} = {};", vs[0])); + t + }; + self.line(&format!("if (!{f}.fn) rex_panic(\"called an empty function value\");")); + let l = self.place_str(list); + self.line(&format!("rex_msort({l}.p, {l}.len, sizeof({et}), {tramp}, (void *)&{f});")); + String::new() + } + ListOp::Contains | ListOp::IndexOf => unreachable!(), + } + } + fn map_ref(&mut self, map: &HExpr) -> String { if is_place(map) { self.place_str(map) @@ -2776,6 +2974,11 @@ static dstr {c}_str({c} l) {{ (BinOp::Add | BinOp::Sub | BinOp::Mul, Ty::Num(k)) if !k.is_float() => { format!("(({})({l} {} {r}))", num_c(k), sym(op)) } + // shifting by the width or more gives 0 (or -1 for `>>` of a negative number) + (BinOp::Shl, t) => format!("(({})rex_shl((uint64_t){l}, (int64_t){r}))", c_ty(t)), + (BinOp::Shr, t) if t.is_signed() => format!("(({})rex_shr_i((int64_t){l}, (int64_t){r}))", c_ty(t)), + (BinOp::Shr, t) => format!("(({})rex_shr_u((uint64_t){l}, (int64_t){r}))", c_ty(t)), + (BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor, t) => format!("(({})({l} {} {r}))", c_ty(t), sym(op)), (BinOp::Div, Ty::Int) => self.materialize(Ty::Int, format!("rex_div({l}, {r})")), (BinOp::Rem, Ty::Int) => self.materialize(Ty::Int, format!("rex_rem({l}, {r})")), (BinOp::Eq | BinOp::Ne, Ty::Enum(_)) => format!("({l}.tag {} {r}.tag)", sym(op)), diff --git a/src/driver.rs b/src/driver.rs index 330059c..5d736d5 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -77,7 +77,7 @@ fn check_project( let loaded = loaded.map_err(&render)?; let modules: Vec<(String, crate::ast::Program)> = loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); - let dir = Path::new(main_path).parent().map(Path::to_path_buf).unwrap_or_default(); + let dir = loader::project_dir(Path::new(main_path)); let libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?; if module_ok { check::check_module_c(&loaded.main, &modules, libs).map_err(&render) @@ -89,7 +89,7 @@ fn check_project( /// Compile the program whose main file is `path`; imports are found next to it. pub fn compile_file(path: &str) -> Result { let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; - let dir = Path::new(path).parent().map(Path::to_path_buf).unwrap_or_default(); + let dir = loader::project_dir(Path::new(path)); compile_project(path, &src, |key| { let file = dir.join(loader::key_file(key)); std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) @@ -100,7 +100,7 @@ pub fn compile_file(path: &str) -> Result { /// `compile_file`, a file without `main` is fine: it may be a module. pub fn check_file(path: &str) -> Result<(), String> { let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; - let dir = Path::new(path).parent().map(Path::to_path_buf).unwrap_or_default(); + let dir = loader::project_dir(Path::new(path)); let read = |key: &str| { let file = dir.join(loader::key_file(key)); std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) @@ -108,6 +108,38 @@ pub fn check_file(path: &str) -> Result<(), String> { check_project(path, &src, read, true).map(|_| ()) } +/// The program in `path` built for `rex test`: the C code, and the names of its test functions +/// (`func test_...()` in that file, which take nothing and return nothing or an `error`), in the +/// order they were written. The C program runs test number `n` when started with argument `n`. +pub fn compile_tests(path: &str) -> Result<(String, Vec), String> { + let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; + let dir = loader::project_dir(Path::new(path)); + let read = |key: &str| { + let file = dir.join(loader::key_file(key)); + std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) + }; + let hir = check_project(path, &src, read, true)?; + // in the order written in the file + let ast = parser::parse(&src).map_err(|d| d.render(path, &src))?; + let mut tests = Vec::new(); + for item in &ast.items { + let Item::Func(f) = item else { continue }; + if !f.name.starts_with("test_") { + continue; + } + let Some(h) = hir.funcs.iter().find(|h| h.owner.is_none() && h.name == f.name) else { continue }; + if !h.params.is_empty() || !matches!(h.ret, crate::types::Ty::Unit | crate::types::Ty::Error) { + let d = Diag::new( + format!("`{}` looks like a test, but a test takes nothing and returns nothing or an `error`", f.name), + f.span, + ); + return Err(d.render(path, &src)); + } + tests.push(f.name.clone()); + } + Ok((codegen_c::generate_tests(&hir, &tests), tests)) +} + fn unique_name(tag: &str, ext: &str) -> PathBuf { // the counter makes names differ even when two threads ask in the same nanosecond static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); diff --git a/src/fmt.rs b/src/fmt.rs index 0eac68e..6ada0ae 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -31,7 +31,7 @@ struct Piece { const KEYWORDS: &[&str] = &[ "import", "struct", "enum", "interface", "func", "var", "const", "if", "else", "for", "in", "match", "return", - "break", "continue", "pub", "shared", "weak", "step", + "break", "continue", "defer", "pub", "shared", "weak", "step", ]; /// Words that make a following `{` open a block (rather than a struct or map literal). @@ -39,6 +39,7 @@ const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", " const BINARY: &[&str] = &[ "+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=", "%=", "=>", + "&", "|", "^", "<<", ">>", "&=", "|=", "^=", "<<=", ">>=", ]; /// Format a whole file. Fails (with the problem) if the file doesn't parse. @@ -159,6 +160,12 @@ fn scan(src: &str) -> Vec { i += 1; kind = Kind::Punct; } + } else if ch == '0' && matches!(c.get(i + 1), Some('x' | 'X' | 'b' | 'B' | 'o' | 'O')) { + i += 2; + while i < c.len() && (c[i].is_ascii_alphanumeric() || c[i] == '_') { + i += 1; + } + kind = Kind::Num; } else if ch.is_ascii_digit() { while i < c.len() && (c[i].is_ascii_digit() || c[i] == '_') { i += 1; @@ -177,7 +184,10 @@ fn scan(src: &str) -> Vec { kind = Kind::Word; } else { let two: String = c[i..(i + 2).min(c.len())].iter().collect(); - if ["==", "!=", "<=", ">=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "=>", ".."].contains(&two.as_str()) { + let three: String = c[i..(i + 3).min(c.len())].iter().collect(); + if three == "<<=" || three == ">>=" { + i += 3; + } else if ["<<", ">>", "&=", "|=", "^=", "==", "!=", "<=", ">=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "=>", ".."].contains(&two.as_str()) { i += 2; } else { i += 1; @@ -344,7 +354,7 @@ fn print(p: &[Piece]) -> String { // is this `-` / `!` a prefix operator? let is_prefix = t.kind == Kind::Punct - && (t.text == "!" || (t.text == "-" && (line_start || prev.map_or(true, |l| !ends_value(l))))); + && (t.text == "!" || t.text == "~" || (t.text == "-" && (line_start || prev.map_or(true, |l| !ends_value(l))))); unary_before = is_prefix; if t.kind == Kind::LineComment && !line_start { diff --git a/src/hir.rs b/src/hir.rs index 5ec4247..5a66b1b 100644 --- a/src/hir.rs +++ b/src/hir.rs @@ -162,6 +162,50 @@ pub enum HStmt { Break, Continue, Block(Vec), + /// `defer ...`: these statements run when the enclosing block ends (also on `return`, + /// `break`, `continue` and `?`), the last `defer` first. + Defer(Vec), +} + +/// What a built-in method of a list does (besides `len`, `push` and `pop`). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ListOp { + /// `l.insert(i, v)` + Insert, + /// `l.remove(i)`: the item that was there + Remove, + Clear, + /// `l.contains(x)`: bool + Contains, + /// `l.index_of(x)`: the first position, or -1 + IndexOf, + Reverse, + /// `l.sort()`: smallest first (stable) + Sort, + /// `l.sort_by(less)`: `less(a, b)` says whether `a` goes before `b` (stable) + SortBy, +} + +impl ListOp { + pub const NAMES: &'static str = "insert, remove, clear, contains, index_of, reverse, sort, sort_by"; + + pub fn from_name(name: &str) -> Option { + Some(match name { + "insert" => ListOp::Insert, + "remove" => ListOp::Remove, + "clear" => ListOp::Clear, + "contains" => ListOp::Contains, + "index_of" => ListOp::IndexOf, + "reverse" => ListOp::Reverse, + "sort" => ListOp::Sort, + "sort_by" => ListOp::SortBy, + _ => return None, + }) + } + + pub fn changes_list(self) -> bool { + !matches!(self, ListOp::Contains | ListOp::IndexOf) + } } /// What a method of a map does. @@ -231,6 +275,8 @@ pub enum HExprKind { ListPush { list: Box, value: Box }, /// `list.pop()`; `list` is a place ListPop(Box), + /// `list.insert(i, v)`, `list.sort()`, ...; `list` is a place for the ones that change it + ListCall { list: Box, op: ListOp, args: Vec }, /// `list.len()`, `string.len()` or `map.len()` Len(Box), /// `{"a": 1}`: every key and value, in the order written @@ -372,6 +418,10 @@ pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn F walk_expr(map, on_stmt, on_expr); args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); } + HExprKind::ListCall { list, args, .. } => { + walk_expr(list, on_stmt, on_expr); + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); + } HExprKind::Index { base, index } => { walk_expr(base, on_stmt, on_expr); walk_expr(index, on_stmt, on_expr); @@ -409,7 +459,7 @@ pub fn walk_stmts(stmts: &[HStmt], on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mu walk_stmts(then, on_stmt, on_expr); walk_stmts(else_, on_stmt, on_expr); } - HStmt::Loop(b) | HStmt::Block(b) => walk_stmts(b, on_stmt, on_expr), + HStmt::Loop(b) | HStmt::Block(b) | HStmt::Defer(b) => walk_stmts(b, on_stmt, on_expr), HStmt::While { cond, body } => { walk_expr(cond, on_stmt, on_expr); walk_stmts(body, on_stmt, on_expr); @@ -466,6 +516,7 @@ pub fn expr_mutates_root(e: &HExpr, id: u32, is_mut_method: &dyn Fn(&str, &str) place_root(list).map(|(i, _)| i) == Some(id) } HExprKind::MapCall { map, op: MapOp::Remove, .. } => place_root(map).map(|(i, _)| i) == Some(id), + HExprKind::ListCall { list, op, .. } if op.changes_list() => place_root(list).map(|(i, _)| i) == Some(id), // a C function writing into a variable or a list HExprKind::CCall { args, modes, .. } => args .iter() @@ -555,6 +606,10 @@ pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) { f(map); args.iter_mut().for_each(|a| f(a)); } + HExprKind::ListCall { list, args, .. } => { + f(list); + args.iter_mut().for_each(|a| f(a)); + } HExprKind::Index { base, index } => { f(base); f(index); diff --git a/src/lexer.rs b/src/lexer.rs index 40b730e..69b5686 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -34,6 +34,7 @@ pub enum Tok { Return, Break, Continue, + Defer, Pub, Shared, Weak, @@ -74,6 +75,18 @@ pub enum Tok { StarEq, SlashEq, PercentEq, + /// bit operators: `&`, `|`, `^`, `~`, `<<`, `>>` and their `op=` forms + Amp, + Pipe, + Caret, + Tilde, + Shl, + Shr, + AmpEq, + PipeEq, + CaretEq, + ShlEq, + ShrEq, /// End of a statement (a real newline or `;`). Inserted like Go does. Newline, @@ -99,6 +112,7 @@ impl Tok { Return => "return", Break => "break", Continue => "continue", + Defer => "defer", Pub => "pub", Shared => "shared", Weak => "weak", @@ -137,6 +151,17 @@ impl Tok { StarEq => "*=", SlashEq => "/=", PercentEq => "%=", + Amp => "&", + Pipe => "|", + Caret => "^", + Tilde => "~", + Shl => "<<", + Shr => ">>", + AmpEq => "&=", + PipeEq => "|=", + CaretEq => "^=", + ShlEq => "<<=", + ShrEq => ">>=", _ => "", } } @@ -199,6 +224,7 @@ fn keyword(s: &str) -> Option { "return" => Tok::Return, "break" => Tok::Break, "continue" => Tok::Continue, + "defer" => Tok::Defer, "pub" => Tok::Pub, "shared" => Tok::Shared, "weak" => Tok::Weak, @@ -340,6 +366,39 @@ impl Lexer { } fn number(&mut self, span: Span) -> Result<(), Diag> { + // `0xff`, `0b1010`, `0o755` + let radix = match (self.peek(), self.peek2()) { + (Some('0'), Some('x' | 'X')) => 16, + (Some('0'), Some('b' | 'B')) => 2, + (Some('0'), Some('o' | 'O')) => 8, + _ => 10, + }; + if radix != 10 { + self.bump(); + self.bump(); + let mut s = String::new(); + while let Some(c) = self.peek() { + if c.is_ascii_alphanumeric() { + s.push(c); + self.bump(); + } else if c == '_' { + self.bump(); + } else { + break; + } + } + let kind = match radix { + 16 => "hexadecimal (0-9, a-f)", + 2 => "binary (0 and 1)", + _ => "octal (0-7)", + }; + if s.is_empty() || !s.chars().all(|c| c.is_digit(radix)) { + return Err(Diag::new(format!("invalid {kind} number"), span)); + } + let n = i64::from_str_radix(&s, radix).map_err(|_| Diag::new("number is too large", span))?; + self.push(Tok::Int(n), span); + return Ok(()); + } let mut s = String::new(); let digits = |lx: &mut Lexer, s: &mut String| { while let Some(c) = lx.peek() { @@ -520,6 +579,30 @@ impl Lexer { Tok::Bang } } + '<' if self.peek() == Some('<') => { + self.eat('<'); + if self.eat('=') { + Tok::ShlEq + } else { + Tok::Shl + } + } + '>' if self.peek() == Some('>') => { + self.eat('>'); + if self.eat('=') { + Tok::ShrEq + } else { + Tok::Shr + } + } + '^' => { + if self.eat('=') { + Tok::CaretEq + } else { + Tok::Caret + } + } + '~' => Tok::Tilde, '<' => { if self.eat('=') { Tok::Le @@ -572,15 +655,19 @@ impl Lexer { '&' => { if self.eat('&') { Tok::AndAnd + } else if self.eat('=') { + Tok::AmpEq } else { - return Err(Diag::new("a single `&` is not part of Rex; use `&&` for \"and\"", span)); + Tok::Amp } } '|' => { if self.eat('|') { Tok::OrOr + } else if self.eat('=') { + Tok::PipeEq } else { - return Err(Diag::new("a single `|` is not part of Rex; use `||` for \"or\"", span)); + Tok::Pipe } } other => return Err(Diag::new(format!("unexpected character `{other}`"), span)), diff --git a/src/loader.rs b/src/loader.rs index 55f5af8..4034994 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,5 +1,8 @@ //! Finds the files a program imports. `import utils` loads `utils.rex` from the -//! folder of the main file; `import a.b` loads `a/b.rex`; `import ..a` loads `../a.rex`. +//! folder of the project (the main file's folder, or the nearest one above it with a +//! `rex.toml`); `import a.b` loads `a/b.rex`; `import ..a` loads `../a.rex`. + +use std::path::{Path, PathBuf}; use crate::ast::{Item, Program}; use crate::error::{Diag, PResult}; @@ -15,6 +18,24 @@ pub fn key_file(key: &str) -> String { format!("{ups}{}.rex", rest.replace('.', "/")) } +/// The file that marks the top folder of a project. +pub const ROOT_FILE: &str = "rex.toml"; + +/// The folder imports are counted from for the Rex file `file`: the nearest folder above it +/// (or its own) that has a `rex.toml`, else the file's own folder. +pub fn project_dir(file: &Path) -> PathBuf { + let own = file.parent().map(Path::to_path_buf).unwrap_or_default(); + let start = if own.as_os_str().is_empty() { PathBuf::from(".") } else { own.clone() }; + let abs = std::fs::canonicalize(&start).unwrap_or_else(|_| start.clone()); + for d in abs.ancestors() { + if d.join(ROOT_FILE).is_file() { + // the file's own folder keeps its short spelling in error messages + return if d == abs { own } else { d.to_path_buf() }; + } + } + own +} + pub struct Module { /// The import path, like `utils` or `a.b`. pub key: String, diff --git a/src/lsp.rs b/src/lsp.rs index 5a08c02..54cd566 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -166,7 +166,7 @@ impl Server { /// Check the file and build the `publishDiagnostics` message. fn diagnostics(&self, uri: &str) -> Json { let text = &self.docs[uri]; - let dir: Option = uri_to_path(uri).and_then(|p| p.parent().map(PathBuf::from)); + let dir: Option = uri_to_path(uri).map(|p| crate::loader::project_dir(&p)); let docs = &self.docs; let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let here = dir.clone().unwrap_or_default(); @@ -359,7 +359,7 @@ impl Server { /// The C header behind an import, read the same way the compiler reads it. fn c_lib(&self, uri: &str, imp: &crate::index::Import) -> Option { let header = imp.header.as_ref()?; - let dir = uri_to_path(uri)?.parent()?.to_path_buf(); + let dir = crate::loader::project_dir(&uri_to_path(uri)?); crate::cheader::load(header, &dir, imp.link.as_deref()).ok() } @@ -398,7 +398,7 @@ impl Server { /// The source of the module `key` as seen from the file `uri`: open in the editor, or on disk. fn module_source(&self, uri: &str, key: &str) -> Option<(String, String)> { - let dir = uri_to_path(uri)?.parent()?.to_path_buf(); + let dir = crate::loader::project_dir(&uri_to_path(uri)?); let file = dir.join(crate::loader::key_file(key)); let target = path_to_uri(&file); for (u, t) in &self.docs { @@ -682,7 +682,10 @@ impl Server { } } } - for n in ["len", "push", "pop", "has", "get", "get_or", "remove", "keys", "values", "upgrade"] { + for n in ["len", "push", "pop", "has", "get", "get_or", "remove", "keys", "values", "upgrade"] + .into_iter() + .chain(crate::hir::ListOp::NAMES.split(", ").filter(|n| *n != "remove")) + { push(&mut items, n, 2, "built in"); } for n in stdlib::STR_METHODS.split(", ").chain(stdlib::CHAR_METHODS.split(", ")).chain(["join"]) { @@ -717,17 +720,17 @@ impl Server { } for k in [ "func", "var", "const", "import", "struct", "enum", "interface", "pub", "if", "else", "for", "in", "step", "match", - "return", "break", "continue", "shared", "weak", "nil", "true", "false", + "return", "break", "continue", "defer", "shared", "weak", "nil", "true", "false", ] { push(&mut items, k, 14, "keyword"); } for t in ["int", "float", "bool", "char", "string", "error", "map", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64"] { push(&mut items, t, 7, "type"); } - for f in ["print", "error"] { + for f in ["print", "error", "assert"] { push(&mut items, f, 3, "built in"); } - for m in ["os", "math", "time"] { + for m in stdlib::MODULES.iter().copied() { if !ix.imports.iter().any(|i| i.name == m) { push(&mut items, m, 9, "built-in module (needs `import`)"); } diff --git a/src/main.rs b/src/main.rs index f6de2a6..040e2c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ fn usage() -> ! { eprintln!(" rex build [-o out] compile to an executable"); eprintln!(" rex emit-c print the generated C code"); eprintln!(" rex check check for errors without building"); + eprintln!(" rex test [file|dir]... run the test_ functions (default: this folder)"); eprintln!(" rex fmt [--check] .. format code in place (--check only reports; `-` = stdin to stdout)"); eprintln!(" rex lsp run the language server (for editors)"); eprintln!(" rex header [name] show what Rex can use from a C header"); @@ -152,8 +153,82 @@ fn run_fmt(args: &[String]) -> ! { process::exit(0); } +/// `rex test [files-or-folders...]`: build each file that has `func test_...` and run every +/// test in a process of its own, so a failed `assert` or a panic only stops that test. +fn run_tests(args: &[String]) -> ! { + let paths: Vec = if args.is_empty() { vec![".".to_string()] } else { args.to_vec() }; + let mut files = Vec::new(); + for p in &paths { + let path = PathBuf::from(p); + if path.is_file() { + files.push(path); + } else if path.is_dir() { + rex_files(&path, &mut files); + } else { + die(&format!("error: {p} doesn't exist")); + } + } + let has_tests = |f: &PathBuf| fs::read_to_string(f).map_or(false, |s| s.contains("func test_")); + files.retain(has_tests); + if files.is_empty() { + die("no tests found (a test is a function named `test_...` in a .rex file)"); + } + let (mut passed, mut failed, mut broken) = (0, 0, 0); + for file in &files { + let shown = file.display().to_string(); + let shown = shown.strip_prefix("./").unwrap_or(&shown).to_string(); + let (c, tests) = match driver::compile_tests(&shown) { + Ok(r) => r, + Err(e) => { + eprintln!("{e}"); + broken += 1; + continue; + } + }; + if tests.is_empty() { + continue; + } + let exe = driver::temp_exe(); + if let Err(e) = driver::build_exe(&c, &exe) { + eprintln!("error: {e}"); + broken += 1; + continue; + } + for (i, name) in tests.iter().enumerate() { + match process::Command::new(&exe).arg(i.to_string()).output() { + Ok(o) if o.status.success() => { + passed += 1; + println!("ok {shown}: {name}"); + } + Ok(o) => { + failed += 1; + println!("FAIL {shown}: {name}"); + let text = format!("{}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + for line in text.lines() { + println!(" {line}"); + } + } + Err(e) => { + let _ = fs::remove_file(&exe); + die(&format!("error: cannot run the tests: {e}")); + } + } + } + let _ = fs::remove_file(&exe); + } + let mut summary = format!("\n{passed} passed, {failed} failed"); + if broken > 0 { + summary.push_str(&format!(", {broken} file{} didn't build", if broken == 1 { "" } else { "s" })); + } + println!("{summary}"); + process::exit(if failed > 0 || broken > 0 { 1 } else { 0 }); +} + fn main() { let args: Vec = env::args().skip(1).collect(); + if args.first().map(String::as_str) == Some("test") { + run_tests(&args[1..]); + } if args.first().map(String::as_str) == Some("lsp") { let stdin = std::io::stdin(); process::exit(lsp::run(stdin.lock(), std::io::stdout())); diff --git a/src/moves.rs b/src/moves.rs index 6f13521..bb264fb 100644 --- a/src/moves.rs +++ b/src/moves.rs @@ -6,7 +6,7 @@ //! mentions the variable, and no loop is in the way (a loop would run the copy again). use crate::hir::*; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; #[derive(Default)] struct Use { @@ -20,6 +20,8 @@ struct State { seq: u32, uses: HashMap, decl_depth: HashMap, + /// Variables used by a `defer`. + pinned: HashSet, } pub fn apply(body: &mut Vec) { @@ -94,6 +96,12 @@ fn scan(stmts: &[HStmt], depth: u32, st: &mut State) { unit_scan(&[iter], depth, st); scan(body, depth + 1, st); } + // the deferred code runs at the end of the block: its variables can't be moved before + HStmt::Defer(b) => walk_stmts(b, &mut |_| {}, &mut |x| { + if let HExprKind::Local(id, _) = &x.kind { + st.pinned.insert(*id); + } + }), HStmt::Return(None) | HStmt::Break | HStmt::Continue => {} } } @@ -111,7 +119,8 @@ fn movable(e: &mut HExpr, seq: u32, depth: u32, st: &State) { if let HExprKind::Copy(inner) = &e.kind { if let HExprKind::Local(id, _) = &inner.kind { let last = st.uses.get(id); - let ok = matches!(last, Some(u) if u.last_seq == seq && u.count == 1 && u.last_depth == depth) + let ok = !st.pinned.contains(id) + && matches!(last, Some(u) if u.last_seq == seq && u.count == 1 && u.last_depth == depth) && st.decl_depth.get(id) == Some(&depth); if ok { let HExprKind::Copy(inner) = std::mem::replace(&mut e.kind, HExprKind::Nil) else { unreachable!() }; @@ -136,6 +145,8 @@ fn rewrite(stmts: &mut [HStmt], depth: u32, st: &mut State) { } HStmt::Loop(b) => rewrite(b, depth + 1, st), HStmt::Block(b) => rewrite(b, depth, st), + // runs at the end of the block, possibly from inside a loop: never moves + HStmt::Defer(_) => {} HStmt::While { cond, body } => { unit_rewrite(&mut [cond], depth + 1, st); rewrite(body, depth + 1, st); diff --git a/src/parser.rs b/src/parser.rs index 89e0bed..b5feb7a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -499,6 +499,17 @@ impl Parser { self.bump(); Ok(Stmt::Continue(span)) } + Tok::Defer => { + self.bump(); + if matches!(self.peek(), Tok::Newline | Tok::RBrace | Tok::Eof) { + return self.unexpected("what to run later, like `defer f.close()` or `defer { ... }`"); + } + let body = self.stmt()?; + if matches!(body, Stmt::Var(_)) { + return Err(Diag::new("`defer` runs a call or a `{ ... }` block later; a `var` there would be useless", span)); + } + Ok(Stmt::Defer { body: Box::new(body), span }) + } _ => { let target = self.expr()?; let op = match self.peek() { @@ -508,6 +519,11 @@ impl Parser { Tok::StarEq => Some(AssignOp::Mul), Tok::SlashEq => Some(AssignOp::Div), Tok::PercentEq => Some(AssignOp::Rem), + Tok::AmpEq => Some(AssignOp::BitAnd), + Tok::PipeEq => Some(AssignOp::BitOr), + Tok::CaretEq => Some(AssignOp::BitXor), + Tok::ShlEq => Some(AssignOp::Shl), + Tok::ShrEq => Some(AssignOp::Shr), _ => None, }; match op { @@ -637,6 +653,7 @@ impl Parser { | Tok::LBracket | Tok::Minus | Tok::Bang + | Tok::Tilde | Tok::Nil | Tok::True | Tok::False @@ -657,11 +674,18 @@ impl Parser { Tok::Gt => (BinOp::Gt, 4), Tok::Le => (BinOp::Le, 4), Tok::Ge => (BinOp::Ge, 4), + // like Go: `|` and `^` go with `+`, `&` and the shifts with `*`, + // so `x & 1 == 0` means `(x & 1) == 0` Tok::Plus => (BinOp::Add, 5), Tok::Minus => (BinOp::Sub, 5), + Tok::Pipe => (BinOp::BitOr, 5), + Tok::Caret => (BinOp::BitXor, 5), Tok::Star => (BinOp::Mul, 6), Tok::Slash => (BinOp::Div, 6), Tok::Percent => (BinOp::Rem, 6), + Tok::Amp => (BinOp::BitAnd, 6), + Tok::Shl => (BinOp::Shl, 6), + Tok::Shr => (BinOp::Shr, 6), _ => return None, }) } @@ -692,6 +716,10 @@ impl Parser { self.bump(); ExprKind::Unary { op: UnOp::Not, expr: Box::new(self.unary()?) } } + Tok::Tilde => { + self.bump(); + ExprKind::Unary { op: UnOp::BitNot, expr: Box::new(self.unary()?) } + } Tok::Shared => { self.bump(); ExprKind::Shared(Box::new(self.unary()?)) diff --git a/src/runtime.c b/src/runtime.c index 2a67ad1..02c05c9 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -609,6 +609,100 @@ static void rex_time_sleep(double secs) { nanosleep(&ts, NULL); } +/* ---- bits ---- */ + +static inline uint64_t rex_shl(uint64_t x, int64_t s) { + if (s < 0) rex_panic("can't shift by a negative amount"); + return s >= 64 ? 0 : x << s; +} + +static inline int64_t rex_shr_i(int64_t x, int64_t s) { + if (s < 0) rex_panic("can't shift by a negative amount"); + return s >= 64 ? (x < 0 ? -1 : 0) : x >> s; +} + +static inline uint64_t rex_shr_u(uint64_t x, int64_t s) { + if (s < 0) rex_panic("can't shift by a negative amount"); + return s >= 64 ? 0 : x >> s; +} + +/* ---- sorting: a stable merge sort over items of `sz` bytes ---- */ + +static void rex_msort(void *base, int64_t n, size_t sz, bool (*less)(void *, const void *, const void *), void *ctx) { + if (n < 2) return; + char *a = (char *)base; + char *tmp = (char *)malloc((size_t)n * sz); + if (!tmp) rex_panic("out of memory"); + for (int64_t w = 1; w < n; w *= 2) { + for (int64_t lo = 0; lo < n; lo += 2 * w) { + int64_t mid = lo + w < n ? lo + w : n; + int64_t hi = lo + 2 * w < n ? lo + 2 * w : n; + int64_t i = lo, j = mid, k = lo; + while (i < mid && j < hi) { + /* take from the right only when it is strictly smaller: equal items keep their order */ + if (less(ctx, a + j * sz, a + i * sz)) memcpy(tmp + (k++) * sz, a + (j++) * sz, sz); + else memcpy(tmp + (k++) * sz, a + (i++) * sz, sz); + } + if (i < mid) memcpy(tmp + k * sz, a + i * sz, (size_t)(mid - i) * sz), k += mid - i; + if (j < hi) memcpy(tmp + k * sz, a + j * sz, (size_t)(hi - j) * sz); + } + memcpy(a, tmp, (size_t)n * sz); + } + free(tmp); +} + +/* ---- rand: splitmix64, seeded from the clock unless rand.seed is called ---- */ + +static uint64_t rex_rand_state; +static bool rex_rand_seeded; + +static uint64_t rex_rand_next(void) { + if (!rex_rand_seeded) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + rex_rand_state = (uint64_t)ts.tv_nsec ^ ((uint64_t)ts.tv_sec << 30) ^ ((uint64_t)getpid() << 48); + rex_rand_seeded = true; + } + uint64_t z = (rex_rand_state += 0x9E3779B97F4A7C15ull); + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; + return z ^ (z >> 31); +} + +static void rex_rand_seed(int64_t n) { + rex_rand_state = (uint64_t)n; + rex_rand_seeded = true; +} + +/* a whole number from lo up to (not including) hi, every one equally likely */ +static int64_t rex_rand_int(int64_t lo, int64_t hi) { + if (hi <= lo) { + fflush(stdout); + fprintf(stderr, "panic: rand.int(%lld, %lld): the second number must be bigger\n", (long long)lo, (long long)hi); + exit(2); + } + uint64_t span = (uint64_t)hi - (uint64_t)lo; + uint64_t limit = UINT64_MAX - UINT64_MAX % span; + uint64_t x; + do x = rex_rand_next(); while (x >= limit); + return (int64_t)((uint64_t)lo + x % span); +} + +/* from 0 up to (not including) 1 */ +static double rex_rand_float(void) { + return (double)(rex_rand_next() >> 11) * (1.0 / 9007199254740992.0); +} + +/* ---- assert ---- */ + +static void rex_assert(bool ok, dstr where, dstr msg) { + if (ok) return; + fflush(stdout); + if (msg.n) fprintf(stderr, "assertion failed: %.*s (%.*s)\n", (int)msg.n, msg.p, (int)where.n, where.p); + else fprintf(stderr, "assertion failed (%.*s)\n", (int)where.n, where.p); + exit(2); +} + /* ---- C libraries ---- */ static dstr rex_from_cstr(const char *p) { diff --git a/src/stdlib.rs b/src/stdlib.rs index 52add26..d295443 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -1,11 +1,11 @@ -//! The built-in modules (`os`, `math`, `time`) and the methods of strings and lists. +//! The built-in modules (`os`, `math`, `time`, `rand`) and the methods of strings and lists. //! This is only a table of names and types; the code is in `runtime.c`. use crate::hir::BuiltinShape; use crate::types::Ty; /// Modules that `import` knows without a file. -pub const MODULES: &[&str] = &["os", "math", "time"]; +pub const MODULES: &[&str] = &["os", "math", "time", "rand"]; pub struct BuiltinFn { /// The C function (or, for `Min`/`Max`/`Abs`, nothing). @@ -68,6 +68,10 @@ pub fn module_fn(module: &str, name: &str) -> Option { ("time", "now") => plain("rex_time_now", vec![], Float), ("time", "sleep") => plain("rex_time_sleep", vec![Float], Unit), + + ("rand", "int") => plain("rex_rand_int", vec![Int, Int], Int), + ("rand", "float") => plain("rex_rand_float", vec![], Float), + ("rand", "seed") => plain("rex_rand_seed", vec![Int], Unit), _ => None, } } @@ -95,6 +99,7 @@ pub fn module_names(module: &str) -> Vec<&'static str> { "floor", "ceil", "round", "trunc", "min", "max", "abs", "PI", "E", ], "time" => &["now", "sleep"], + "rand" => &["int", "float", "seed"], _ => &[], }; fns.to_vec() diff --git a/tests/fmt.rs b/tests/fmt.rs index 8c4c9bb..cb8541c 100644 --- a/tests/fmt.rs +++ b/tests/fmt.rs @@ -119,3 +119,10 @@ fn import_paths_going_up_keep_their_dots() { let src = "import ...engine.engine\nimport ..side\nimport a.b\n\nfunc main() {\n for i in 0..3 { print(i) }\n}\n"; assert_eq!(fmt(src), src); } + +#[test] +fn bit_operators_and_defer() { + let src = "func main() {\nvar a=0xff&~0b1|1<<4>>2^3\na<<=1\na>>=1\na&=3\na|=4\na^=5\ndefer print(a)\ndefer{\nprint(-a)\n}\n}\n"; + let want = "func main() {\n var a = 0xff & ~0b1 | 1 << 4 >> 2 ^ 3\n a <<= 1\n a >>= 1\n a &= 3\n a |= 4\n a ^= 5\n defer print(a)\n defer {\n print(-a)\n }\n}\n"; + assert_eq!(fmt(src), want); +} diff --git a/tests/imports.rs b/tests/imports.rs index 3ac61f0..7f1a228 100644 --- a/tests/imports.rs +++ b/tests/imports.rs @@ -64,3 +64,51 @@ fn one_dot_is_not_an_import_path() { assert!(!o.status.success()); let _ = std::fs::remove_dir_all(&dir); } + +#[test] +fn a_rex_toml_marks_the_project_root() { + let dir = tree(&[ + ("rex.toml", ""), + ("engine/engine.rex", "import engine.util\n\npub func hi(): int { return util.seven() }\n"), + ("engine/util.rex", "pub func seven(): int { return 7 }\n"), + ("game/app/main.rex", "import engine.engine\nimport game.common\n\nfunc main() {\n print(engine.hi(), common.two())\n}\n"), + ("game/common.rex", "pub func two(): int { return 2 }\n"), + ("game/deeper/x/main.rex", "import engine.engine\n\nfunc main() {\n print(engine.hi())\n}\n"), + ]); + // the same import works from any depth, and from any folder the command is run in + let o = rex(&dir, "game/app", "main.rex"); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + assert_eq!(String::from_utf8_lossy(&o.stdout), "7 2\n"); + let o = rex(&dir, "game/deeper/x", "main.rex"); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + assert_eq!(String::from_utf8_lossy(&o.stdout), "7\n"); + let o = Command::new(env!("CARGO_BIN_EXE_rex")) + .args(["run", dir.join("game/app/main.rex").to_str().unwrap()]) + .current_dir(std::env::temp_dir()) + .output() + .unwrap(); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn without_rex_toml_imports_count_from_the_main_file() { + let dir = tree(&[ + ("engine/engine.rex", "pub func hi(): int { return 7 }\n"), + ("game/main.rex", "import engine.engine\n\nfunc main() { }\n"), + ]); + let o = rex(&dir, "game", "main.rex"); + assert!(!o.status.success()); + assert!(String::from_utf8_lossy(&o.stderr).contains("cannot find module `engine.engine`")); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn project_dir_finds_the_nearest_root() { + use rex::loader::project_dir; + let dir = tree(&[("rex.toml", ""), ("a/b/main.rex", ""), ("a/rex.toml", ""), ("c/d/main.rex", "")]); + let canon = |p: PathBuf| std::fs::canonicalize(p).unwrap(); + assert_eq!(canon(project_dir(&dir.join("a/b/main.rex"))), canon(dir.join("a"))); + assert_eq!(canon(project_dir(&dir.join("c/d/main.rex"))), canon(dir.clone())); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/tests/lsp.rs b/tests/lsp.rs index 81a6272..ee7625c 100644 --- a/tests/lsp.rs +++ b/tests/lsp.rs @@ -287,3 +287,24 @@ fn imports_from_a_folder_above_are_found() { let hover = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap(); assert!(format!("{hover:?}").contains("twice"), "{hover:?}"); } + +#[test] +fn the_editor_counts_imports_from_the_project_root() { + let dir = std::env::temp_dir().join(format!("rex-lsp-root-{}", std::process::id())); + std::fs::create_dir_all(dir.join("engine")).unwrap(); + std::fs::create_dir_all(dir.join("game/app")).unwrap(); + std::fs::write(dir.join("rex.toml"), "").unwrap(); + std::fs::write(dir.join("engine/engine.rex"), "import engine.util\npub func twice(x: int): int { return util.dbl(x) }\n").unwrap(); + std::fs::write(dir.join("engine/util.rex"), "pub func dbl(x: int): int { return x * 2 }\n").unwrap(); + let main = format!("file://{}/game/app/main.rex", dir.display()); + let module = format!("file://{}/engine/engine.rex", dir.display()); + let src = "import engine.engine\nfunc main() {\n print(engine.twice(2))\n}\n"; + let mod_src = "import engine.util\npub func twice(x: int): int { return util.dbl(x) }\n"; + // both the main file and a module of the project are checked with the same root + let mut msgs = vec![open(&main, src), open(&module, mod_src)]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let _ = std::fs::remove_dir_all(&dir); + assert_eq!(out[0].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[0]); + assert_eq!(out[1].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[1]); +} diff --git a/tests/parse.rs b/tests/parse.rs index ec0bc20..0f97713 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -226,8 +226,16 @@ fn error_const_needs_value() { } #[test] -fn error_single_ampersand() { - assert!(err("func f() { var a = b & c }").msg.contains("&&")); +fn bit_operators_bind_like_go() { + // `&` and shifts go with `*`, `|` and `^` with `+`, all tighter than comparisons + let p = parse("func f() { var a = b & 1 == 0 }").unwrap(); + let text = format!("{p:?}"); + let eq = text.find("op: Eq").unwrap(); + let and = text.find("op: BitAnd").unwrap(); + assert!(eq < and, "`==` should be the outer operator: {text}"); + let p = parse("func f() { var a = b | c << 2 }").unwrap(); + let text = format!("{p:?}"); + assert!(text.find("op: BitOr").unwrap() < text.find("op: Shl").unwrap(), "{text}"); } #[test] diff --git a/tests/run.rs b/tests/run.rs index ec90ff9..bc6b565 100644 --- a/tests/run.rs +++ b/tests/run.rs @@ -666,8 +666,8 @@ fn error_push_on_temporary() { #[test] fn error_unknown_list_method() { - let m = check_err(&main_with("var a = [1]\na.reverse()")); - assert!(m.contains("no method `reverse`") && m.contains("len, push, pop"), "{m}"); + let m = check_err(&main_with("var a = [1]\na.shuffle()")); + assert!(m.contains("no method `shuffle`") && m.contains("len, push, pop, insert"), "{m}"); } #[test] @@ -2259,3 +2259,120 @@ fn string_chars_list() { ); assert_eq!(out, "9 é 世\n[a, b, c, d] 0\nx\ny\n"); } + +// ---------- bit operators ---------- + +#[test] +fn bit_operators() { + let out = out_of( + "func main() {\n var f = 0b0101 | 0x10\n print(f, f & 3, f ^ 1, ~0, 1 << 10, -16 >> 2, f & 1 == 1, 0o17)\n f &= 0xf\n f |= 0b1000\n f ^= 1\n f <<= 2\n f >>= 1\n print(f)\n var b: u8 = 200\n b <<= 1\n print(b, b >> 1, ~b)\n var x: u32 = 1\n var n = 40\n print(x << n, x << 31, -1 >> 70, 1 << 64)\n var h: u64 = 0xff\n print(h << 56 >> 60)\n}\n", + ); + assert_eq!(out, "21 1 20 -1 1024 -4 true 15\n24\n144 72 111\n0 2147483648 -1 0\n15\n"); +} + +#[test] +fn negative_shift_panics() { + let o = run_source("func main() {\n var n = -1\n print(1 << n)\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("negative"), "{}", o.stderr); +} + +#[test] +fn bit_operator_errors() { + assert!(check_err(&main_with("var a = 1.5 & 1.0")).contains("doesn't work on `float`")); + assert!(check_err(&main_with("var a = true | false")).contains("`||`")); + assert!(check_err(&main_with("var a = ~true")).contains("`~`")); + assert!(check_err(&main_with("var a = 1.0 << 2")).contains("shifts the bits")); + assert!(check_err(&main_with("var a = 1 << -2")).contains("negative")); + assert!(check_err(&main_with("var a = 0xfg")).contains("hexadecimal")); + assert!(check_err(&main_with("var a = 0b102")).contains("binary")); +} + +// ---------- defer ---------- + +#[test] +fn defer_runs_at_the_end_of_the_block() { + let out = out_of( + "func work(n: int): int {\n defer print(\"leave\", n)\n defer {\n print(\"second runs first\")\n }\n if n > 2 {\n return n * 10\n }\n for i in 0..4 {\n defer print(\" end\", i)\n if i == 1 {\n continue\n }\n if i == 2 {\n break\n }\n print(\" round\", i)\n }\n return n\n}\n\nfunc main() {\n print(work(1))\n print(work(5))\n}\n", + ); + assert_eq!( + out, + " round 0\n end 0\n end 1\n end 2\nsecond runs first\nleave 1\n1\nsecond runs first\nleave 5\n50\n" + ); +} + +#[test] +fn defer_sees_the_latest_values_and_runs_on_errors() { + let out = out_of( + "func fail(): error {\n return error(\"boom\")\n}\n\nfunc job(): error {\n var log: []string\n defer print(\"log:\", log)\n log.push(\"start\")\n fail()?\n log.push(\"never\")\n return nil\n}\n\nfunc keep(): []int {\n var xs = [1]\n defer print(\"returning\", xs)\n xs.push(2)\n return xs\n}\n\nfunc main() {\n var err = job()\n print(err)\n print(keep())\n var s = \"a\"\n {\n defer print(\"inner\", s)\n s = s + \"b\"\n }\n var t = s\n print(t)\n}\n", + ); + assert_eq!(out, "log: [start]\nboom\nreturning [1, 2]\n[1, 2]\ninner ab\nab\n"); +} + +#[test] +fn defer_errors() { + assert!(check_err(&main_with("defer return")).contains("defer return")); + assert!(check_err(&main_with("for { defer { break } }")).contains("can't jump out of a `defer`")); + assert!(check_err("func f(): error { return nil }\nfunc g(): error {\n defer f()?\n return nil\n}\nfunc main() {}\n").contains("`?` can't be used inside a `defer`")); + assert!(check_err(&main_with("defer var x = 1")).contains("`var`")); + assert!(check_err(&main_with("defer")).contains("what to run later")); + // a loop inside the deferred code may use break + assert_eq!(out_of(&main_with("defer {\n for { break }\n print(\"ok\")\n }")), "ok\n"); +} + +// ---------- list methods ---------- + +#[test] +fn list_methods() { + let out = out_of( + "func main() {\n var xs = [5, 3, 9, 1]\n xs.insert(0, 7)\n xs.insert(5, 8)\n print(xs)\n print(xs.remove(1), xs)\n xs.sort()\n print(xs, xs.contains(9), xs.index_of(9), xs.index_of(42))\n xs.reverse()\n print(xs)\n xs.clear()\n print(xs, xs.len())\n var names = [\"bob\", \"al\", \"cy\"]\n names.sort()\n names.insert(1, \"ann\")\n print(names, names.contains(\"al\"), names.remove(0), names)\n names.clear()\n names.push(\"z\")\n print(names, [1, 2].contains(2))\n}\n", + ); + assert_eq!( + out, + "[7, 5, 3, 9, 1, 8]\n5 [7, 3, 9, 1, 8]\n[1, 3, 7, 8, 9] true 4 -1\n[9, 8, 7, 3, 1]\n[] 0\n[al, ann, bob, cy] true al [ann, bob, cy]\n[z] true\n" + ); +} + +#[test] +fn sort_by_is_stable_and_frees_its_copies() { + let out = out_of( + "struct P {\n name: string\n tags: []string\n age: int\n}\n\nfunc main() {\n var ps = [P{\"a\", [\"x\"], 30}, P{\"b\", [], 20}, P{\"c\", [\"y\"], 30}, P{\"d\", [], 10}]\n ps.sort_by((p, q) => p.age < q.age)\n for p in ps {\n print(p.name, p.age)\n }\n var lists = [[3], [1, 2], []]\n var by_len = func(a, b: []int): bool { return a.len() < b.len() }\n lists.sort_by(by_len)\n print(lists)\n var words = [\"pear\", \"fig\", \"apple\"]\n words.sort_by((a, b) => a.len() > b.len())\n print(words)\n var e: []int\n e.sort()\n e.sort_by((a, b) => a < b)\n print(e)\n}\n", + ); + assert_eq!(out, "d 10\nb 20\na 30\nc 30\n[[], [3], [1, 2]]\n[apple, pear, fig]\n[]\n"); +} + +#[test] +fn list_method_panics_and_errors() { + let o = run_source("func main() {\n var xs = [1]\n xs.insert(3, 1)\n}"); + assert_eq!(o.code, 2); + let o = run_source("func main() {\n var xs = [1]\n xs.remove(1)\n}"); + assert_eq!(o.code, 2); + assert!(check_err("struct P { x: int }\nfunc main() {\n var ps = [P{1}]\n ps.sort()\n}").contains("sort_by")); + assert!(check_err("struct P { x: int }\nfunc main() {\n var ps = [P{1}]\n print(ps.contains(P{1}))\n}").contains("write a loop")); + assert!(check_err(&main_with("const XS = 1\n [1].sort()")).contains("not a temporary")); + assert!(check_err(&main_with("var xs = [1]\n xs.shuffle()")).contains("sort_by")); +} + +// ---------- rand and assert ---------- + +#[test] +fn rand_module() { + let out = out_of( + "import rand\n\nfunc main() {\n rand.seed(7)\n var a = rand.int(0, 1000)\n rand.seed(7)\n print(a == rand.int(0, 1000))\n var seen = [false, false, false]\n for i in 0..300 {\n var n = rand.int(-1, 2)\n assert(n >= -1 && n < 2)\n seen[n + 1] = true\n var f = rand.float()\n assert(f >= 0.0 && f < 1.0)\n }\n print(seen)\n}\n", + ); + assert_eq!(out, "true\n[true, true, true]\n"); + let o = run_source("import rand\nfunc main() {\n print(rand.int(3, 3))\n}"); + assert_eq!(o.code, 2); +} + +#[test] +fn assert_stops_the_program() { + let o = run_source("func main() {\n var x = 4\n assert(x == 4)\n print(\"a\")\n assert(x == 5, \"x is {x}\")\n print(\"b\")\n}"); + assert_eq!(o.stdout, "a\n"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("assertion failed: x is 4 (line 5)"), "{}", o.stderr); + let o = run_source("func main() {\n assert(false)\n}"); + assert!(o.stderr.contains("assertion failed (line 2)"), "{}", o.stderr); + assert!(check_err(&main_with("assert(1)")).contains("bool")); + assert!(check_err(&main_with("assert()")).contains("condition")); +} diff --git a/tests/test_cmd.rs b/tests/test_cmd.rs new file mode 100644 index 0000000..b0a30dd --- /dev/null +++ b/tests/test_cmd.rs @@ -0,0 +1,48 @@ +//! Tests of `rex test`. + +use std::process::Command; + +#[test] +fn runs_each_test_on_its_own() { + let dir = std::env::temp_dir().join(format!("rex-test-cmd-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("sub")).unwrap(); + std::fs::write( + dir.join("calc.rex"), + "func add(a, b: int): int {\n return a + b\n}\n\nfunc test_add() {\n assert(add(2, 2) == 4)\n}\n\nfunc test_wrong() {\n print(\"shown when it fails\")\n assert(add(2, 2) == 5, \"2 + 2 is {add(2, 2)}\")\n}\n\nfunc test_error(): error {\n return error(\"nope\")\n}\n\nfunc test_panic() {\n var xs: []int\n xs.pop()\n}\n\nfunc test_after_panic() {}\n\nfunc main() {\n print(\"main is not run\")\n}\n", + ) + .unwrap(); + // a file without tests is skipped; one without `main` is fine + std::fs::write(dir.join("sub/plain.rex"), "func main() {}\n").unwrap(); + std::fs::write(dir.join("sub/lib.rex"), "func test_ok(): error {\n return nil\n}\n").unwrap(); + let o = Command::new(env!("CARGO_BIN_EXE_rex")).arg("test").current_dir(&dir).output().unwrap(); + let out = String::from_utf8_lossy(&o.stdout).into_owned(); + assert_eq!(o.status.code(), Some(1), "{out}"); + for want in [ + "ok calc.rex: test_add", + "FAIL calc.rex: test_wrong", + " shown when it fails", + " assertion failed: 2 + 2 is 4 (line 11)", + "FAIL calc.rex: test_error", + " error: nope", + "FAIL calc.rex: test_panic", + " panic: pop from an empty list", + "ok calc.rex: test_after_panic", + "ok sub/lib.rex: test_ok", + "3 passed, 3 failed", + ] { + assert!(out.contains(want), "missing {want:?} in:\n{out}"); + } + assert!(!out.contains("main is not run"), "{out}"); + + // only one file, all passing + let o = Command::new(env!("CARGO_BIN_EXE_rex")).args(["test", "sub/lib.rex"]).current_dir(&dir).output().unwrap(); + assert_eq!(o.status.code(), Some(0), "{}", String::from_utf8_lossy(&o.stdout)); + + // a test with parameters is an error + std::fs::write(dir.join("sub/lib.rex"), "func test_bad(n: int) {}\n").unwrap(); + let o = Command::new(env!("CARGO_BIN_EXE_rex")).args(["test", "sub/lib.rex"]).current_dir(&dir).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + assert!(String::from_utf8_lossy(&o.stderr).contains("a test takes nothing")); + let _ = std::fs::remove_dir_all(&dir); +}