import * as AST from './ast'; import Env, { EnvError } from './env'; export interface CompilerOpts { prettyPrint?: boolean; env?: Env; } export default class Compiler { private tree: AST.Node[]; private prettyPrint: boolean; private env: Env; public constructor( parserResult: { tree: AST.Node[]; module?: string }, opts: CompilerOpts ) { this.tree = parserResult.tree; this.prettyPrint = opts.prettyPrint || false; this.env = opts.env || new Env(); } public compile(): string | EnvError { const result = this.tree.map((node) => node.compile(this.env, { depth: 0, prettyPrint: this.prettyPrint, }) ); const resultError = result.find((el) => el instanceof EnvError); if (resultError !== undefined) return resultError; const output = result.join(this.prettyPrint ? '\n' : ''); return this.prettyPrint ? output.trim() : output; } }