compiler 0.0.2-preview.1

The Zena self-hosted compiler

Readme Files Dependencies Versions

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.json manifest 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

2.0 KiB Raw
/**
 * Contract clauses (docs/design/contracts.md): the check a `requires`
 * or `invariant` clause runs.
 *
 * A check evaluates the clause's condition and, when it is false, calls
 * the standard library's `$contractFailed` with a message naming the
 * clause. That function does not return, so what a violated contract
 * does is the library's to decide, and a check costs the function it
 * sits in one branch and one call.
 */
import { ContractClause, StringLiteral, contractFailureMessage } from '../../ast.zena';
import { IrRefFunc } from './ir.zena';
import { lowerExpr } from './lowering.zena';
import { LoweringContext, noValues, oneValue } from './lowering-context.zena';
import { lowerStringLiteral } from './templates.zena';

/** Emits the check for one clause at the current position. */
export function lowerContractClause(cx: LoweringContext, clause: ContractClause): void {
  // A `debug` clause in a build that leaves them out emits nothing.
  if (!cx.wasm.runsContractClause(clause)) { return; }
  if (!cx.reachable) { return; }
  let condition = lowerExpr(cx, clause.condition, null);
  if (!cx.reachable) { return; }

  let holds = cx.b.newBlock();
  let fails = cx.b.newBlock();
  cx.b.brIf(condition, holds, noValues(), fails, noValues());

  cx.b.switchTo(fails);
  // Reachability roots the function and registers the message for
  // every clause it walks (`noteContractClause`).
  let failed = cx.wasm.getStdlibFunc('core', '$contractFailed');
  if (failed == null) {
    cx.fail('contract clause: $contractFailed was not reached');
  }
  let message = lowerStringLiteral(cx,
      new StringLiteral(contractFailureMessage(clause), clause.loc));
  cx.b.callFunc(cx.b.addRef(new IrRefFunc(failed)), oneValue(message), null);
  cx.b.unreachable();

  cx.b.switchTo(holds);
}

/** Emits the checks for a function's or a loop's clauses, in source order. */
export function lowerContractClauses(
  cx: LoweringContext,
  clauses: GrowableArray<ContractClause> | null
): void {
  if (clauses == null) { return; }
  for (let clause in clauses) {
    lowerContractClause(cx, clause);
  }
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19