Add C and C++ library support
import "zlib.h" reads the header (cc -E + a small C declaration parser), links via pkg-config or link "name", and builds .c/.cpp files next to the program. C structs become Dino structs, pointers become pointer types (nil = NULL), T* parameters take variables or lists. Adds dino header, editor completion/hover for C names, docs chapter 13, a raylib example, and tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b42e2a3bd
commit
2f44f09b6a
26 files changed
+3418
-70
No files matched your search
+57
@@ -12,6 +12,7 @@ fn usage() -> ! {
|
||||
eprintln!(" dino check <file.dino> check for errors without building");
|
||||
eprintln!(" dino fmt [--check] <file|dir>.. format code in place (--check only reports; `-` = stdin to stdout)");
|
||||
eprintln!(" dino lsp run the language server (for editors)");
|
||||
eprintln!(" dino header <file.h> [name] show what Dino can use from a C header");
|
||||
eprintln!(" dino parse <file.dino> print the syntax tree");
|
||||
eprintln!(" dino tokens <file.dino> print the tokens");
|
||||
process::exit(2);
|
||||
@@ -35,6 +36,59 @@ fn dino_files(path: &std::path::Path, out: &mut Vec<PathBuf>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// `dino header zlib.h [name] [--link z]`: what Dino code can use from a C header.
|
||||
fn run_header(args: &[String]) -> ! {
|
||||
let mut header: Option<&str> = None;
|
||||
let mut filter: Option<&str> = None;
|
||||
let mut link: Option<&str> = None;
|
||||
let mut it = args.iter();
|
||||
while let Some(a) = it.next() {
|
||||
match a.as_str() {
|
||||
"--link" => link = it.next().map(String::as_str),
|
||||
_ if header.is_none() => header = Some(a),
|
||||
_ => filter = Some(a),
|
||||
}
|
||||
}
|
||||
let Some(header) = header else { usage() };
|
||||
let dir = env::current_dir().unwrap_or_default();
|
||||
let lib = dino::cheader::load(header, &dir, link).unwrap_or_else(|e| die(&format!("error: {e}")));
|
||||
let shown = |n: &str| filter.map_or(true, |f| n.to_lowercase().contains(&f.to_lowercase()));
|
||||
let mut text = format!("import \"{header}\" // use the names below as {}.name\n", lib.prefix);
|
||||
if !lib.libs.is_empty() || !lib.cflags.is_empty() {
|
||||
text.push_str(&format!("// builds with: {}\n", lib.cflags.iter().chain(&lib.libs).cloned().collect::<Vec<_>>().join(" ")));
|
||||
}
|
||||
let mut section = |title: &str, lines: Vec<String>| {
|
||||
if !lines.is_empty() {
|
||||
text.push_str(&format!("\n// {title}\n"));
|
||||
for l in lines {
|
||||
text.push_str(&l);
|
||||
text.push('\n');
|
||||
}
|
||||
}
|
||||
};
|
||||
section("types", lib.structs.iter().filter(|s| shown(&s.name)).map(|s| lib.describe(&s.name).unwrap()).collect());
|
||||
section(
|
||||
"pointer types (pass them around, compare them with nil)",
|
||||
lib.handles.iter().filter(|h| shown(h)).map(|h| format!("{}.{h}", lib.prefix)).collect(),
|
||||
);
|
||||
section(
|
||||
"functions",
|
||||
lib.funcs.iter().filter(|f| f.problem.is_none() && shown(&f.name)).map(|f| format!("func {}", lib.signature(f))).collect(),
|
||||
);
|
||||
section("constants", lib.consts.iter().filter(|c| shown(&c.name)).map(|c| lib.describe(&c.name).unwrap()).collect());
|
||||
section(
|
||||
"functions Dino can't call yet",
|
||||
lib.funcs
|
||||
.iter()
|
||||
.filter(|f| shown(&f.name))
|
||||
.filter_map(|f| f.problem.as_ref().map(|p| format!("{}: it {p}", f.name)))
|
||||
.collect(),
|
||||
);
|
||||
// a closed pipe (`| head`) is not an error
|
||||
let _ = std::io::Write::write_all(&mut std::io::stdout(), text.as_bytes());
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
/// `dino fmt [--check] files-or-folders...`
|
||||
fn run_fmt(args: &[String]) -> ! {
|
||||
// `dino fmt -`: text in on stdin, tidy text out on stdout (for editors)
|
||||
@@ -107,6 +161,9 @@ fn main() {
|
||||
if args.first().map(String::as_str) == Some("fmt") {
|
||||
run_fmt(&args[1..]);
|
||||
}
|
||||
if args.first().map(String::as_str) == Some("header") {
|
||||
run_header(&args[1..]);
|
||||
}
|
||||
if args.len() < 2 {
|
||||
usage();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user