Files
rexlang/src/driver.rs
T
Your NameandClaude Opus 5.5 7cf9727bcd Rename the language from Dino to Rex
Files use .rex, the command is rex, and the crate, runtime prefixes,
docs, editor files and examples all use the new name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:51:23 +01:00

198 lines
8.7 KiB
Rust

//! Ties the compiler stages together and invokes the C compiler.
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::ast::{Item, Program};
use crate::error::{Diag, PResult};
use crate::{cheader, check, codegen_c, loader, parser};
/// Read the C headers imported by the program. `dir` is the folder of the main file.
pub fn load_c_headers(main: &Program, modules: &[(String, Program)], dir: &Path) -> PResult<Vec<cheader::Lib>> {
let mut libs: Vec<cheader::Lib> = Vec::new();
let all = std::iter::once(("", main)).chain(modules.iter().map(|(k, p)| (k.as_str(), p)));
for (key, p) in all {
// `import a.b` lives in a/b.rex: its headers are found next to it
let mut here = dir.to_path_buf();
let parts: Vec<&str> = key.split('.').collect();
for part in &parts[..parts.len().saturating_sub(1)] {
here.push(part);
}
for item in &p.items {
let Item::Import(i) = item else { continue };
if !i.c || libs.iter().any(|l| l.header == i.path) {
continue;
}
let lib = cheader::load(&i.path, &here, i.link.as_deref())
.map_err(|m| Diag::new(m, i.span).in_module(key))?;
libs.push(lib);
}
}
Ok(libs)
}
/// Rex source -> C source, for a program in a single file. C headers it imports are looked
/// for in the current folder and the system's.
pub fn compile_to_c(src: &str) -> PResult<String> {
let ast = parser::parse(src)?;
let libs = load_c_headers(&ast, &[], Path::new("."))?;
let hir = check::check_project_c(&ast, &[], libs)?;
Ok(codegen_c::generate(&hir))
}
/// A whole program: the main file and the files it imports. `read(key)` finds a module
/// (see `loader`). Errors come back already rendered, naming the file they are in.
pub fn compile_project(
main_path: &str,
main_src: &str,
mut read: impl FnMut(&str) -> Option<(String, String)>,
) -> Result<String, String> {
let mut seen: HashMap<String, (String, String)> = HashMap::new();
let loaded = loader::load(main_src, &mut |key| {
let found = read(key);
if let Some((path, src)) = &found {
seen.insert(key.to_string(), (path.clone(), src.clone()));
}
found
});
let render = |d: Diag| -> String {
match d.file.as_deref().and_then(|k| seen.get(k)) {
Some((path, src)) => d.render(path, src),
None if d.file.is_some() => d.render(d.file.as_deref().unwrap(), ""),
None => d.render(main_path, main_src),
}
};
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 libs = load_c_headers(&loaded.main, &modules, &dir).map_err(&render)?;
let hir = check::check_project_c(&loaded.main, &modules, libs).map_err(&render)?;
Ok(codegen_c::generate(&hir))
}
/// 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();
compile_project(path, &src, |key| {
let file = dir.join(format!("{}.rex", key.replace('.', "/")));
std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s))
})
}
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);
let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
std::env::temp_dir().join(format!("rex-{}-{}-{}-{}{}", std::process::id(), nanos, n, tag, ext))
}
/// A path for a throw-away executable.
pub fn temp_exe() -> PathBuf {
unique_name("exe", if cfg!(windows) { ".exe" } else { "" })
}
/// Compile C source to an executable with the system C compiler (`$CC`, else `cc`).
pub fn build_exe(c_code: &str, out: &Path) -> Result<(), String> {
build_exe_with(c_code, out, &["-O2", "-std=c11", "-fwrapv", "-w"])
}
/// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings).
/// The generated code says which C libraries it needs in `// rex-...:` lines at the top.
pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> {
let (mut cflags, mut libs, mut sources) = (Vec::new(), Vec::new(), Vec::new());
for line in c_code.lines().take_while(|l| l.starts_with("// rex-")) {
let (kind, value) = line["// rex-".len()..].split_once(": ").unwrap_or(("", ""));
match kind {
"cflag" => cflags.push(value.to_string()),
"lib" => libs.push(value.to_string()),
"source" => sources.push(PathBuf::from(value)),
_ => {}
}
}
let uses_c = c_code.contains("\n#include \"");
// C headers often use GNU extensions
let flags: Vec<&str> = flags.iter().map(|f| if uses_c && *f == "-std=c11" { "-std=gnu11" } else { *f }).collect();
let cc = cheader::cc();
// C++ files are compiled on their own, then linked in with the C++ library
let mut objects: Vec<PathBuf> = Vec::new();
let mut c_sources: Vec<PathBuf> = Vec::new();
let cleanup = |objects: &[PathBuf]| {
for o in objects {
let _ = std::fs::remove_file(o);
}
};
for src in &sources {
let ext = src.extension().and_then(|e| e.to_str()).unwrap_or("");
if ext == "c" {
c_sources.push(src.clone());
continue;
}
let obj = unique_name("cpp", ".o");
let cxx = std::env::var("CXX").unwrap_or_else(|_| "c++".to_string());
let r = Command::new(&cxx).args(["-c", "-O2", "-w"]).args(&cflags).arg(src).arg("-o").arg(&obj).output();
match r {
Err(e) => {
cleanup(&objects);
return Err(format!("cannot run the C++ compiler `{cxx}` for {}: {e}", src.display()));
}
Ok(o) if !o.status.success() => {
cleanup(&objects);
return Err(format!("{} does not compile:\n{}", src.display(), String::from_utf8_lossy(&o.stderr)));
}
Ok(_) => objects.push(obj),
}
}
let c_file = unique_name("prog", ".c");
std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?;
let mut cmd = Command::new(&cc);
cmd.args(&flags).args(&cflags).arg("-o").arg(out).arg(&c_file);
for s in &c_sources {
// the user's own C file: warnings are theirs to see, but not ours to fail on
cmd.arg(s);
}
cmd.args(&objects).args(&libs).arg("-lm"); // the math functions
if !objects.is_empty() {
cmd.arg("-lstdc++");
}
let result = cmd.output();
let _ = std::fs::remove_file(&c_file);
cleanup(&objects);
match result {
Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")),
Ok(o) if !o.status.success() => {
let err = String::from_utf8_lossy(&o.stderr);
if err.contains("undefined reference") || err.contains("cannot find -l") || err.contains("Undefined symbols") || err.contains("ld returned") {
Err(format!(
"linking with the C library failed:\n{err}\nIf a function is missing, name its library in the import: `import \"header.h\" link \"name\"` (for libname.so)."
))
} else if uses_c {
Err(format!("the C compiler rejected the program:\n{err}"))
} else {
Err(format!("the C compiler rejected the generated code (this is a bug in Rex):\n{err}"))
}
}
Ok(_) => Ok(()),
}
}
/// Check a whole program without building it, for editors: `main_src` is the text of the main
/// file (it may differ from what is on disk) and `read(key)` finds the other files.
/// The error's `file` says which module it is in (`None`: the main file).
pub fn diagnose(
main_src: &str,
dir: &Path,
mut read: impl FnMut(&str) -> Option<(String, String)>,
) -> Result<(), Diag> {
let loaded = loader::load(main_src, &mut |key| read(key))?;
let modules: Vec<(String, crate::ast::Program)> =
loaded.modules.into_iter().map(|m| (m.key, m.program)).collect();
let libs = load_c_headers(&loaded.main, &modules, dir)?;
check::check_project_c(&loaded.main, &modules, libs)?;
Ok(())
}