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]}`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 11:58:07 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|