UNPKG

expression-evaluation

Version:
33 lines (32 loc) 1.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProgramNode = void 0; const Node_js_1 = require("../Node.js"); const ConstantNode_js_1 = require("./ConstantNode.js"); const Type_js_1 = require("../Type.js"); class ProgramNode extends Node_js_1.Node { _subnodes; constructor(frame, _subnodes) { super(frame); this._subnodes = _subnodes; } get type() { return this._subnodes[this._subnodes.length - 1].type; } compile(type) { let constant = true; for (let i = 0, last = this._subnodes.length - 1; i < this._subnodes.length; ++i) { this._subnodes[i] = this._subnodes[i].compile(i < last ? Type_js_1.typeUnknown : type); constant &&= this._subnodes[i].constant; } return constant ? new ConstantNode_js_1.ConstantNode(this, this.evaluate()) : this; } evaluate() { return this._subnodes.map((s) => s.evaluate())[this._subnodes.length - 1]; } toString(ident = 0) { return `${super.toString(ident)} program node` + `, subnodes:\n${this._subnodes.map((s) => s.toString(ident + 1)).join('\n')}`; } } exports.ProgramNode = ProgramNode;