compiler 0.0.2-preview.1
The Zena self-hosted compiler
compiler#
The Zena compiler, written in Zena. It parses, type-checks, and compiles Zena source code to WebAssembly (Wasm GC) and WebAssembly Text (WAT).
Usage#
Parsing a Single File#
Use compiler/parser to parse Zena source text into an AST (Module):
import { parse } from 'compiler/parser';
let ast = parse('export let answer = 42;', 'example.zena');
Loading and Checking a Program#
Use compiler (createCompiler and checkCompilation) with a CompilerHost to resolve imports across a module graph and type-check a program:
import {
createCompiler, checkCompilation, buildStdlibConfig
} from 'compiler';
import { FsCompilerHost } from 'compiler/fs-host';
let stdlibRoot = '/stdlib';
let host = new FsCompilerHost(stdlibRoot);
let packages = buildStdlibConfig(stdlibRoot, 'host');
let compiler = createCompiler(host, {
stdlibRoot,
packages,
});
let result = await compiler.compile({
entry: '/workspace/main.zena',
target: 'host',
});
let checked = checkCompilation(result);
Compiling a File to WebAssembly#
Use compiler/compile-file to compile an entry file on disk to a .wasm or .wat output file:
import { compileFile } from 'compiler/compile-file';
let ok = await compileFile({
entry: 'src/main.zena',
outPath: 'out/main.wasm',
stdlibRoot: '/stdlib',
});
Libraries#
compiler/compiler/compiler— compiler entry point (Compiler,createCompiler,checkCompilation,ProgramCheckResult)compiler/compile-file— end-to-end file compilation (compileFile,CompileFileOptions)compiler/tokenizer— lexer and token definitions (tokenize,Token,TokenType)compiler/parser— recursive-descent parser (parse,Parser)compiler/ast— AST node definitions (Node,Module,Statement,Expression,TypeAnnotation)compiler/visitor— AST traversal helpers (walk,Visitor)compiler/scope— lexical scope and symbol resolution (ScopeBuilder,ScopeResult,Symbol)compiler/checker— static type checker (checkModule,CheckResult)compiler/semantic-model— semantic queries over a checked module (SemanticModel)compiler/types— compiler type representations (Type,ClassType,FunctionType,typeToString)compiler/diagnostics— compiler diagnostics and source spans (Diagnostic,DiagnosticCode,DiagnosticSeverity,SourceSpan)compiler/package-manifest—zena-package.jsonmanifest and workspace loader (loadWorkspace,loadPackageMap)compiler/module-resolver— package and relative module specifier resolution (ModuleResolver,PackageConfig)compiler/library-loader— module graph loader (LibraryLoader,CompilerHost,SourceFile)compiler/codegen/binary-generator— WebAssembly GC binary emitter (BinaryGenerator)compiler/codegen/wat-generator— WebAssembly Text (WAT) emitter (WatGenerator)
License#
MIT
1.9 KiB
Raw
/**
* A WIT root's document, read through the compiler host.
*
* The WIT parser takes source text and reads nothing. The root of a
* WIT-backed package, or of a `--wit` world, is a path: one `.wit`
* file, or a directory of them. This reads it the way the compiler
* reads modules, through `CompilerHost`, so a host that keeps its files
* in memory serves WIT exactly as it serves source.
*/
import {
WitFile, WitFileStart, WitDocument, joinWitDocument
} from 'zena-lang:wit-parser/document';
import { CompilerHost } from './library-loader.zena';
import { growable } from 'zena:core';
/**
* The WIT document at `path`: one file, or a directory's `.wit` files
* joined by `joinWitDocument`.
*
* A directory's files are named by their path rather than by the bare
* name the host listed, so a diagnostic against the document names a
* file the reader can open. Every name under one directory shares its
* prefix, so this does not change the order they are joined in.
*
* A single file is its own document, taken verbatim: `--wit` names it
* directly, so it is read whatever it is called.
*/
export async function readWitDocument(host: CompilerHost, path: String): Future<WitDocument> {
let names = await host.listDir(path);
if (names == null) {
let starts = growable<WitFileStart>([new WitFileStart(path, 0)]);
return new WitDocument(await host.readFile(path), starts);
}
let files = new GrowableArray<WitFile>();
for (let name in names as GrowableArray<String>) {
if (name.endsWith(".wit")) {
files.push(new WitFile(`${path}/${name}`, await host.readFile(`${path}/${name}`)));
}
}
if (files.length == 0) {
throw new Error("No .wit files in directory: " + path);
}
return joinWitDocument(files);
}
/**
* `readWitDocument`'s source alone, for a caller with nothing to report
* against it.
*/
export async function readWitSource(host: CompilerHost, path: String): Future<String> {
return (await readWitDocument(host, path)).source;
}
- zena-lang:wit-parser wit-parser.zena-lang.pkg.zena-lang.dev ~0.0.1-preview.1
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.2-preview.1 | 2026-11-19 |
|
|