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
7.2 KiB
Raw
/**
* Cross-module lookup helpers: declaration resolution against the
* wasm module's discovery state and value-export collection. Kept
* out of codegen-utils to avoid an import cycle (wasm-module imports
* codegen-utils).
*/
import {
ClassDeclaration, DeclareFunction, EnumDeclaration, ExportAllDeclaration, ExportFromDeclaration, IdentifierPattern, Node, VariableDeclaration, FunctionDeclaration,
} from '../ast.zena';
import {
ArrayType, ClassType, FunctionType, InterfaceType, Type,
TypeParameterType,
} from '../types.zena';
import { Symbol } from '../scope/symbol.zena';
import { Program, CompilationUnit } from '../program.zena';
import { WasmModule } from './wasm-module.zena';
import { dirName, normalizePath } from '../module-resolver.zena';
import { getTypeUniqueKey } from './codegen-utils.zena';
import { HashMap } from 'zena:collections';
function matchTypes(t1: Type, t2: Type): boolean {
if (t1 is TypeParameterType) {
return true;
}
match (t1) {
case ArrayType {elementType}: {
if (t2 is ArrayType) {
return matchTypes(elementType, t2.elementType);
}
return false;
}
case ClassType as ct1: {
if (t2 is ClassType) {
if (ct1.name != t2.name) { return false; }
let {typeArguments} = ct1;
if (typeArguments != null && t2.typeArguments != null) {
let args1 = typeArguments;
let args2 = t2.typeArguments as Array<Type>;
if (args1.length != args2.length) { return false; }
var i = 0;
while (i < args1.length) {
if (!matchTypes(args1[i], args2[i])) { return false; }
i += 1;
}
}
return true;
}
return false;
}
case InterfaceType as it1: {
if (t2 is InterfaceType) {
if (it1.name != t2.name) { return false; }
let {typeArguments} = it1;
if (typeArguments != null && t2.typeArguments != null) {
let args1 = typeArguments;
let args2 = t2.typeArguments as Array<Type>;
if (args1.length != args2.length) { return false; }
var i = 0;
while (i < args1.length) {
if (!matchTypes(args1[i], args2[i])) { return false; }
i += 1;
}
}
return true;
}
return false;
}
case _: {
return getTypeUniqueKey(t1) == getTypeUniqueKey(t2);
}
}
}
function matchOverload(t1: Type, t2: Type): boolean {
if (t1 is FunctionType && t2 is FunctionType) {
if (t1.parameters.length != t2.parameters.length) {
return false;
}
var i = 0;
while (i < t1.parameters.length) {
if (!matchTypes(t1.parameters[i], t2.parameters[i])) {
return false;
}
i += 1;
}
return true;
}
return matchTypes(t1, t2);
}
export function findDeclaration(wasm: WasmModule, sym: Symbol, expectedType: Type | null): DeclareFunction | null {
let resolved = sym.resolveTarget();
let decl = resolved.declaration;
if (decl == null) return null;
let prog = wasm.program;
if (prog != null && resolved.modulePath != "") {
let targetUnit = prog.unitFor(resolved.modulePath);
if (targetUnit != null) {
let importedName = resolved.name;
// Keyed by the unit this compile linked, not by the path the
// symbol was declared under. For a virtual library those differ
// the moment a check result is shared between targets: the
// symbol names the implementation its own compile resolved, and
// this compile has another library under that name.
let key = `${targetUnit.ast.path}::${importedName}`;
if (let (true, list) = prog.exportedFunctions.get(key)) {
var fallback: DeclareFunction | null = null;
for (let df in list) {
if (expectedType == null) {
return df;
}
let nodeType = targetUnit.model.getNodeType(df);
if (nodeType != null && matchOverload(nodeType, expectedType)) {
return df;
}
fallback = df;
}
if (fallback != null) {
return fallback;
}
}
}
}
if (decl is DeclareFunction) {
return decl;
}
return null;
}
export function collectValueExports(program: Program, unit: CompilationUnit, result: HashMap<String, Symbol>): void {
for (let stmt in unit.ast.body) {
match (stmt) {
case VariableDeclaration {pattern, exported}: {
if (exported && pattern is IdentifierPattern) {
let sym = unit.model.resolveDeclaration(pattern);
if (sym != null) {
result[pattern.name] = sym;
}
}
}
case FunctionDeclaration {name as namePat, exported}: {
if (exported) {
let sym = unit.model.resolveDeclaration(namePat as Node);
if (sym != null) {
result[namePat.name] = sym;
}
}
}
case ClassDeclaration {name, exported}: {
if (exported) {
let sym = unit.model.resolveDeclaration(name);
if (sym != null) {
result[name.name] = sym;
}
}
}
case EnumDeclaration {name, exported}: {
if (exported) {
let sym = unit.model.resolveDeclaration(name);
if (sym != null) {
result[name.name] = sym;
}
}
}
case DeclareFunction {name, exported}: {
if (exported) {
let sym = unit.model.resolveDeclaration(name);
if (sym != null) {
result[name.name] = sym;
}
}
}
case ExportFromDeclaration {specifiers, moduleSpecifier}: {
let specifier = moduleSpecifier.value;
var canonicalPath = specifier;
if (specifier.startsWith("./") || specifier.startsWith("../")) {
let dir = dirName(unit.ast.path);
canonicalPath = normalizePath(`${dir}/${specifier}`);
}
if (program.unitsByPath.has(canonicalPath)) {
let targetUnit = program.unitsByPath[canonicalPath];
for (let spec in specifiers) {
let importedName = spec.imported.name;
let localName = spec.local.name;
let targetExports = new Map<String, Symbol>();
collectValueExports(program, targetUnit, targetExports);
if (targetExports.has(importedName)) {
result[localName] = targetExports[importedName];
}
}
}
}
case ExportAllDeclaration {moduleSpecifier}: {
let specifier = moduleSpecifier.value;
var canonicalPath = specifier;
if (specifier.startsWith("./") || specifier.startsWith("../")) {
let dir = dirName(unit.ast.path);
canonicalPath = normalizePath(`${dir}/${specifier}`);
}
if (program.unitsByPath.has(canonicalPath)) {
let targetUnit = program.unitsByPath[canonicalPath];
collectValueExports(program, targetUnit, result);
}
}
case _: {}
}
}
}
export function isArrayIntrinsic(name: String): boolean {
return name == "__array_len" || name == "__byte_array_length" ||
name == "__array_get" || name == "__byte_array_get" ||
name == "__array_set" || name == "__byte_array_set" ||
name == "__array_new" || name == "__array_new_empty" ||
name == "__byte_array_new" || name == "__byte_array_copy";
}
- 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 |
|
|