bolt/src/emitter.ts

38 lines
702 B
TypeScript
Raw Normal View History

2020-02-25 17:55:17 +01:00
2020-05-10 15:56:34 +02:00
import { Syntax, SyntaxKind, kindToString } from "./ast"
2020-02-25 17:55:17 +01:00
export class Emitter {
emit(node: Syntax) {
2020-05-10 18:21:44 +02:00
debug(node);
2020-02-25 17:55:17 +01:00
switch (node.kind) {
2020-05-10 18:21:44 +02:00
case SyntaxKind.JSReferenceExpression:
return node.name;
2020-05-10 15:56:34 +02:00
case SyntaxKind.JSSourceFile:
2020-02-25 17:55:17 +01:00
let out = ''
for (const element of node.elements) {
out += this.emit(element);
}
return out;
default:
2020-05-10 15:56:34 +02:00
throw new Error(`Could not emit source code for ${kindToString(node.kind)}`)
2020-02-25 17:55:17 +01: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);
}