Parser now uses context to store details of the computation
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user