Files
DiceCloud/app/imports/parser/parseTree/IndexNode.js
2020-09-18 22:13:12 +02:00

28 lines
708 B
JavaScript

import ParseNode from '/imports/parser/parseTree/ParseNode.js';
export default class IndexNode extends ParseNode {
constructor({array, index}) {
super(...arguments);
this.array = array;
this.index = index;
}
resolve(fn, scope, context){
let index = this.index[fn](scope, context);
if (index.isInteger){
let selection = this.array.values[index.value - 1];
if (selection){
let result = selection[fn](scope, context);
return result;
}
}
return new IndexNode({
index,
array: this.array[fn](scope, context),
previousNodes: [this],
});
}
toString(){
return `${this.array.toString()}[${this.index.toString()}]`;
}
}