55 lines
5.7 KiB
HTML
55 lines
5.7 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#f7f3eb"><meta name="description" content="Compiler embedding: the Ziran guide, rendered from docs/COMPILER_API.md."><title>Compiler embedding · Ziran guide</title><link rel="canonical" href="https://ziran-lang.org/guide/compiler-api"><link rel="icon" href="../assets/logo-capybara.png" type="image/png"><link rel="stylesheet" href="../styles.css"><script defer src="../site.js"></script></head>
|
|
<body><a class="skip-link" href="#main">Skip to content</a><header class="site-header"><div class="site-header-inner"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><button class="menu-button" type="button" aria-expanded="false" aria-controls="primary-nav">Menu</button><nav class="header-nav" id="primary-nav" aria-label="Primary"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html" aria-current="page">Docs</a><a href="../status.html">Status</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a><button class="theme-toggle" type="button" aria-label="Switch color theme"></button></nav></div></header>
|
|
<main id="main" class="article-layout"><div class="article-head"><p class="eyebrow"><a href="../docs.html#guide">Guide</a></p><h1>Compiler embedding</h1><p>Rendered from <a href="https://github.com/ziranlang/ziran/blob/master/docs/COMPILER_API.md">docs/COMPILER_API.md</a>. See the <a href="../docs.html#guide">documentation index</a> for the other guides.</p></div>
|
|
<div class="article-grid"><aside class="page-toc"><strong>On this page</strong><span>One section</span></aside><div class="article-body">
|
|
<section class="prose"><p><code>parse_file</code>, <code>parse_file_with_imports_defined</code>, and <code>parse_source_text</code> return
|
|
a program on success and <code>NULL</code> on a source-read or syntax error. Diagnostics
|
|
use the existing text or JSON format. A bad source file does not exit the host
|
|
process. Parser temporaries, open input streams, and partial trees are released
|
|
before the call returns; a subsequent parse can use the same thread normally.</p>
|
|
<p>Free successful results with <code>ProgramFree</code>. Source parsing and <code>ProgramRead</code>
|
|
automatically create a compilation context when no context is selected.
|
|
The result owns that context. Loading a module graph with <code>ProgramsLoad</code>
|
|
instead gives the graph one context, released by <code>ProgramsFree</code>.</p>
|
|
<p>To construct raw IR or keep multiple programs in one compilation region,
|
|
select an explicit context:</p>
|
|
<pre><code class="language-c">CompilerContext *context = CompilerContextOpen();
|
|
if(context == NULL) return 0;
|
|
CompilerContext *previous = CompilerContextSelect(context);
|
|
ZirProgram *program = parse_source_text("example.zi",
|
|
"Answer :: () -> s32 { return 42 }\n");
|
|
CompilerContextSelect(previous);
|
|
int valid = program != NULL && CheckPrograms(&program, 1);
|
|
ProgramFree(program);
|
|
if(!CompilerContextClose(context)) return 0;
|
|
return valid;
|
|
</code></pre>
|
|
<p><code>CompilerContextClose</code> refuses a selected context or one retained by live
|
|
programs. Restore the previous selection and free the programs first. Strings,
|
|
parameter views, field cursors, and source spans borrowed from a context must
|
|
not be used after that context is destroyed. Copies created by the linker
|
|
retain their dependencies automatically. Internal code that copies an IR
|
|
graph must use <code>ProgramInheritContexts</code> and assign copied modules the new
|
|
graph's lookup context.</p>
|
|
<p>Public checking, lowering, and VM entry points select their program's context
|
|
for the operation and restore the caller's selection afterward. Raw IR
|
|
construction and internal compiler helpers use the explicitly selected
|
|
context. Unscoped low-level construction uses the shared process context;
|
|
long-running hosts should select an explicit region instead.</p>
|
|
<p>Each mutable compilation graph has one compiling owner. Separate contexts can
|
|
parse and check on separate threads. Native backend configuration still has
|
|
process state, so embedding hosts must serialize native backend operations.
|
|
Independent VM instances may execute an immutable program concurrently while
|
|
that program and its contexts remain alive.
|
|
Closing a context or mutating or freeing its graph during those operations is
|
|
unsupported. Shared immutable snapshots and scratch-pool publication are
|
|
synchronized; scratch buffers have one borrower at a time.</p>
|
|
<p><code>ZirFunction.checked</code> records semantic validity. Native symbol planning and
|
|
body eligibility are separate operations. Native-only functions can be valid
|
|
even when the portable verifier rejects them. The source/saved-IR checks and
|
|
the portable verifier must still run before executable use.</p></section>
|
|
</div></div></main>
|
|
<footer><div class="footer-top"><a class="brand" href="../index.html"><img class="brand-icon" src="../assets/logo-capybara.png" alt="" width="36" height="36"><span>Ziran</span></a><nav aria-label="Footer"><a href="../get-started.html">Get started</a><a href="../examples.html">Examples</a><a href="../targets.html">Targets</a><a href="../modules.html">Modules</a><a href="../docs.html">Docs</a><a href="../status.html">Status</a><a href="../index.html#support">Support</a><a href="https://github.com/ziranlang/ziran">GitHub ↗</a></nav></div><div class="footer-bottom"><p>A language for useful software.</p><p>© 2026 Kryon Labs · <a href="https://waozi.xyz/">Made by Waozi</a></p></div></footer></body></html>
|