UNPKG

expression-evaluation

Version:
36 lines (35 loc) 1.25 kB
import { Node } from '../Node.js'; import { ConstantNode } from './ConstantNode.js'; import { typeObject, typeString, typeUnknown } from '../Type.js'; export class ObjectNode extends Node { _subnodes; constructor(frame, _subnodes) { super(frame); this._subnodes = _subnodes; } get type() { return 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(typeString); constant &&= this._subnodes[i][0].constant; this._subnodes[i][1] = this._subnodes[i][1].compile(typeUnknown); constant &&= this._subnodes[i][1].constant; } return constant ? new 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')}`; } }