UNPKG

expression-evaluation

Version:
40 lines (39 loc) 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectNode = void 0; const Node_js_1 = require("../Node.js"); const ConstantNode_js_1 = require("./ConstantNode.js"); const Type_js_1 = require("../Type.js"); class ObjectNode extends Node_js_1.Node { _subnodes; constructor(frame, _subnodes) { super(frame); this._subnodes = _subnodes; } get type() { return Type_js_1.typeObject; } compile(type) { this.reduceType(type); let constant = true; for (let i = 0; i < this._subnodes.length; ++i) { this._subnodes[i][0] = this._subnodes[i][0].compile(Type_js_1.typeString); constant &&= this._subnodes[i][0].constant; this._subnodes[i][1] = this._subnodes[i][1].compile(Type_js_1.typeUnknown); constant &&= this._subnodes[i][1].constant; } return constant ? new ConstantNode_js_1.ConstantNode(this, this.evaluate()) : this; } evaluate() { const obj = {}; for (const [key, value] of this._subnodes) { obj[key.evaluate()] = value.evaluate(); } return obj; } toString(ident = 0) { return `${super.toString(ident)} object node` + `, subnodes:\n${this._subnodes.map(([k, v]) => `${k.toString(ident + 1)}:\n${v.toString(ident + 1)}`).join('\n')}`; } } exports.ObjectNode = ObjectNode;