bolt/src/emitter.ts

33 lines
604 B
TypeScript
Raw Normal View History

2020-02-25 17:55:17 +01:00
import { Syntax, SyntaxKind, isJSNode } from "./ast"
export class Emitter {
emit(node: Syntax) {
switch (node.kind) {
case SyntaxKind.SourceFile:
let out = ''
for (const element of node.elements) {
out += this.emit(element);
}
return out;
default:
throw new Error(`Could not emit source code for ${SyntaxKind[node.kind]}`)
}
}
}
/**
* A wrapper around `Emitter` for quick emission of AST nodes with sane defaults.
*/
export function emit(node: Syntax) {
const emitter = new Emitter();
return emitter.emit(node);
}