UNPKG

expression-evaluation

Version:
30 lines (29 loc) 952 B
import { Node } from '../Node.js'; import { ConstantNode } from './ConstantNode.js'; import { typeArray, typeUnknown } from '../Type.js'; export class ArrayNode extends Node { _subnodes; constructor(frame, _subnodes) { super(frame); this._subnodes = _subnodes; } get type() { return typeArray; } compile(type) { this.reduceType(type); let constant = true; for (let i = 0; i < this._subnodes.length; ++i) { this._subnodes[i] = this._subnodes[i].compile(typeUnknown); constant &&= this._subnodes[i].constant; } return constant ? new ConstantNode(this, this.evaluate()) : this; } evaluate() { return this._subnodes.map((s) => s.evaluate()); } toString(ident = 0) { return `${super.toString(ident)} array node` + `, subnodes:\n${this._subnodes.map((s) => s.toString(ident + 1)).join('\n')}`; } }