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:
Your NameandClaude Opus 5.5 committed 2026-11-12 23:30:05 +01:00
1 parent 910bde692a
commit 300d767124
28 files changed
+1373 -65

No files matched your search

+35 -3
View File
@@ -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<String, 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));
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<String, String> {
/// `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>), 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);