123456789101112131415161718192021222324252627282930313233343536 |
- import { 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;
-
- return result.join(this.prettyPrint ? '\n' : '');
- }
- }
|