OpenGL and Vulkan examples; ptr(int)/int(ptr); trailing commas in calls; drop .fn hint

- examples/opengl.fch: OpenGL 3.3 core, shaders, VAO/VBO, a spinning triangle (checked offscreen)
- examples/vulkan.fch: instance, GPUs with properties/memory/queues, logical device and queue
- ptr(n) and int(p) conversions for C APIs that pass offsets as pointers
- .ptr works on string literals; a comma after the last call argument is allowed
- a missing header included by an imported header is now named in the error
- the old .fn extension hint is gone (Finch is new, there are no old files)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-10-08 19:37:34 +02:00
commit 8497251dc5
22 changed files with 357 additions and 26 deletions

View file

@ -341,8 +341,14 @@ CImports importHeaders(const std::vector<Import> &imports, const std::vector<std
if (clang_getDiagnosticSeverity(d) >= CXDiagnostic_Error) {
std::string msg = str(clang_getDiagnosticSpelling(d));
clang_disposeDiagnostic(d);
if (msg.find("file not found") != std::string::npos)
failAt(im.pos.file, im.pos.line, im.pos.col, "can't find the C header '" + im.path + "'");
if (msg.find("file not found") != std::string::npos) {
// "'GL/gl.h' file not found": it may be a header that this one includes
std::string missing = msg.substr(1, msg.find('\'', 1) - 1);
if (missing == im.path)
failAt(im.pos.file, im.pos.line, im.pos.col, "can't find the C header '" + im.path + "'");
failAt(im.pos.file, im.pos.line, im.pos.col, "can't find the C header '" + missing + "', which '" + im.path +
"' includes (is its library's development package installed?)");
}
failAt(im.pos.file, im.pos.line, im.pos.col, "C header '" + im.path + "' has an error: " + msg);
}
clang_disposeDiagnostic(d);

View file

@ -758,7 +758,9 @@ LRef Codegen::member(const MemberExpr &m, bool forWrite) {
if (!obj.isPlace) release(obj.val);
return {false, {}, {len, FType::I64}};
}
if (!obj.isPlace && obj.val.fresh) failAt(p.file, p.line, p.col, ".ptr of a temporary value would point to freed memory");
// a literal lives as long as the program; other temporaries are freed right after this expression
if (!obj.isPlace && obj.val.fresh && !isa<Constant>(obj.val.v))
failAt(p.file, p.line, p.col, ".ptr of a temporary value would point to freed memory");
FType pt = t.kind == FType::Str ? FType::ptrTo(FType::Char) : FType::ptrTo(*t.elem);
return {false, {}, {t.kind == FType::Str ? cstr(v) : b.CreateExtractValue(v, 0), pt}};
}

View file

@ -441,6 +441,8 @@ Value_ Codegen::convert(const FType &to, const std::vector<ExprPtr> &args, Pos,
}
if (to.kind == FType::Bool && from.isInt()) return {b.CreateICmpNE(v.v, ConstantInt::get(ty(from), 0)), to};
if (to.isPtr() && (from.isPtr() || from.kind == FType::Null)) return {v.v, to};
if (to.isPtr() && from.isInt()) return {b.CreateIntToPtr(intCast(v.v, from, FType::U64), b.getPtrTy()), to}; // an address or offset for C
if (to.isInt() && from.isPtr()) return {b.CreatePtrToInt(v.v, ty(to)), to};
if (to.isPtr() && from.kind == FType::Str) failAt(p.file, p.line, p.col, "use .ptr to get a str's address (it is only valid while the str lives)");
failAt(p.file, p.line, p.col, "can't turn " + from.show() + " into " + name);
}

View file

@ -68,12 +68,7 @@ struct Loader {
void load(const std::string &path, const std::string &module, Pos from) {
std::string text;
if (!readFile(path, text)) {
if (from.line) {
std::string old = path.substr(0, path.size() - 4) + ".fn";
if (sys::fs::exists(old))
failAt(from.file, from.line, from.col, "found " + old + ", but Finch files now end in .fch: rename it to " + path);
failAt(from.file, from.line, from.col, "can't find the module '" + module + "' (looked for " + path + ")");
}
if (from.line) failAt(from.file, from.line, from.col, "can't find the module '" + module + "' (looked for " + path + ")");
std::fprintf(stderr, "error: can't open '%s'\n", path.c_str());
std::exit(1);
}

View file

@ -456,6 +456,7 @@ private:
parenDepth++;
if (!at(Tok::RParen)) {
do {
if (at(Tok::RParen)) break; // a trailing comma is fine
std::string name;
if (at(Tok::Ident) && peekTok().kind == Tok::Colon) {
name = next().text;