Parser now uses context to store details of the computation

This commit is contained in:
Stefan Zermatten
2020-09-18 10:14:53 +02:00
parent b69ad6c306
commit 06f17a6d33
12 changed files with 112 additions and 108 deletions

View File

@@ -1,30 +1,29 @@
export default class ParseNode {
constructor({previousNodes, detail}){
this.inheritDetails(previousNodes);
if (detail) this.pushDetails([detail]);
}
inheritDetails(nodes){
if (!nodes || !nodes.length) return;
nodes.forEach(node => this.pushDetails(node.details));
}
pushDetails(details){
if (!details || !details.length) return;
if (!this.details) this.details = [];
details.forEach(detail => this.details.push(detail));
}
compile(){
// Returns a ParseNode, a ConstantNode if possible
throw new Meteor.Error('Compile not implemented on ' + this.constructor.name);
}
toString(){
throw new Meteor.Error('toString not implemented on ' + this.constructor.name);
}
compile(scope, context){
// Returns a ParseNode, a ConstantNode if possible
if(this.resolve) {
return this.resolve('compile', scope, context);
} else {
throw new Meteor.Error('Compile not implemented on ' + this.constructor.name);
}
}
// Compile, but turn rolls into arrays
roll(scope){
return this.compile(scope);
roll(scope, context){
if (this.resolve){
return this.resolve('roll', scope, context);
} else {
return this.compile(scope, context);
}
}
// Compile, turn rolls into arrays, and reduce those arrays into single values
reduce(scope){
return this.roll(scope);
reduce(scope, context){
if (this.resolve){
return this.resolve('reduce', scope, context);
} else {
return this.roll(scope, context);
}
}
}