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

4.2 KiB Raw
import { KeyNotFoundError } from 'zena:core';
import { Node, NodeId } from './ast.zena';
import { HashMap } from 'zena:collections';

export final class NodeMap<V> {
  #map = new Map<NodeId, V>();

  operator []=(node: Node, value: V): void {
    this.#map[node.id] = value;
  }

  operator [](node: Node): V {
    return this.#map[node.id];
  }

  get(node: Node): inline (true, V) | inline (false, _) {
    return this.#map.get(node.id);
  }

  has(node: Node): boolean {
    return this.#map.has(node.id);
  }

  size: i32 {
    get { return this.#map.size; }
  }

  map: HashMap<NodeId, V> {
    get { return this.#map; }
  }
}

/**
 * A node-keyed table for a side table that has an entry for most nodes
 * of one module: `parents`, `nodeTypes`.
 *
 * A hash map costs about 37 bytes per entry in wasmtime's GC heap — a
 * 32-byte `MapEntry` plus its share of the bucket array — and a table
 * with an entry per node of a large program is tens of MiB that the
 * collector copies on every cycle (issue #199). A module's node ids
 * are one contiguous range (`Module.lastNodeId`), so this stores a
 * value at `id - first` in one array: 4 bytes per node, present or
 * not, and no entry objects at all.
 *
 * A node minted after parsing — by the scope builder or the checker —
 * has an id outside the range and goes in an ordinary map, created on
 * first need. `V` is a reference type, so an empty slot is null.
 */
export final class DenseNodeMap<V extends anyref> {
  var #first: i32;
  var #slots: FixedArray<V | null>;
  var #count = 0;
  var #overflow: HashMap<NodeId, V> | null = null;

  /** A table for the ids `first..last` inclusive; `last < first` is empty. */
  new(first: NodeId, last: NodeId)
    : #first = first as i32,
      #slots = new FixedArray<V | null>(
          if ((last as i32) < (first as i32)) 0 else (last as i32) - (first as i32) + 1,
          null);

  /**
   * Gives a table built before its module's range was known the
   * range, moving anything already stored that falls inside it out of
   * the overflow map.
   */
  reserve(first: NodeId, last: NodeId): void {
    if (this.#slots.length > 0) { return; }
    let length = (last as i32) - (first as i32) + 1;
    if (length <= 0) { return; }
    this.#first = first as i32;
    this.#slots = new FixedArray<V | null>(length, null);
    let overflow = this.#overflow;
    if (overflow == null) { return; }
    let moved = new GrowableArray<NodeId>();
    for (let entry in overflow) {
      let i = (entry.key as i32) - this.#first;
      if (i >= 0 && i < length) {
        this.#slots[i] = entry.value;
        moved.push(entry.key);
      }
    }
    for (let key in moved) {
      overflow.delete(key);
    }
  }

  operator []=(node: Node, value: V): void {
    let i = (node.id as i32) - this.#first;
    if (i >= 0 && i < this.#slots.length) {
      if (this.#slots[i] == null) { this.#count += 1; }
      this.#slots[i] = value;
      return;
    }
    let overflow = this.#overflow ?? new Map<NodeId, V>();
    this.#overflow = overflow;
    if (!overflow.has(node.id)) { this.#count += 1; }
    overflow[node.id] = value;
  }

  /** The value for `node`. Throws KeyNotFoundError if it has none. */
  operator [](node: Node): V {
    if (let (true, value) = this.get(node)) {
      return value;
    }
    throw new KeyNotFoundError();
  }

  get(node: Node): inline (true, V) | inline (false, _) {
    let i = (node.id as i32) - this.#first;
    if (i >= 0 && i < this.#slots.length) {
      let value = this.#slots[i];
      if (value != null) {
        return (true, value);
      }
      return (false, _);
    }
    let overflow = this.#overflow;
    if (overflow == null) { return (false, _); }
    return overflow.get(node.id);
  }

  has(node: Node): boolean {
    if (let (true, _) = this.get(node)) { return true; }
    return false;
  }

  /** How many nodes have a value. */
  size: i32 {
    get { return this.#count; }
  }

  /**
   * The ids stored outside the module's range — the ones a
   * `ModelIndex` has to record one by one, since the range answers for
   * the rest.
   */
  overflowKeys(): Iterator<NodeId> {
    let overflow = this.#overflow;
    if (overflow == null) {
      return new GrowableArray<NodeId>().[Iterable.iterator]();
    }
    return overflow.keys();
  }
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19