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,4 +1,5 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class IfNode extends ParseNode {
constructor({condition, consequent, alternative}){
@@ -11,25 +12,24 @@ export default class IfNode extends ParseNode {
let {condition, consequent, alternative} = this;
return `${condition.toString()} ? ${consequent.toString()} : ${alternative.toString()}`
}
compile(scope){
return this.resolve('compile', scope);
}
roll(scope){
return this.resolve('roll', scope);
}
reduce(scope){
return this.resolve('reduce', scope);
}
resolve(fn, scope){
let condition = this.condition[fn](scope);
if (condition.value){
let consequent = this.consequent[fn](scope);
consequent.inheritDetails([condition, this]);
return this.consequent[fn](scope);
if (condition instanceof ConstantNode){
if (condition.value){
let consequent = this.consequent[fn](scope);
consequent.inheritDetails([condition, this]);
return this.consequent[fn](scope);
} else {
let alternative = this.alternative[fn](scope);
alternative.inheritDetails([condition, this]);
return alternative;
}
} else {
let alternative = this.alternative[fn](scope);
alternative.inheritDetails([condition, this]);
return alternative;
return new IfNode({
condition: condition,
consequent: this.consequent,
alternative: this.alternative,
});
}
}
}