Added not operator to the parser

This commit is contained in:
Stefan Zermatten
2020-11-05 15:32:01 +02:00
parent 4dad2c41e5
commit 1f0678b50b
4 changed files with 39 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
import ParseNode from '/imports/parser/parseTree/ParseNode.js';
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
export default class NotOperatorNode extends ParseNode {
constructor({right}) {
super(...arguments);
this.right = right;
}
resolve(fn, scope, context){
let rightNode = this.right[fn](scope, context);
if (!(rightNode instanceof ConstantNode)){
return new NotOperatorNode({
right: rightNode,
});
}
let right = rightNode.value;
let result = !right;
return new ConstantNode({
value: result,
type: typeof result,
});
}
toString(){
let {right} = this;
return `!${right.toString()}`;
}
}

View File

@@ -13,7 +13,7 @@ export default class OperatorNode extends ParseNode {
let leftNode = this.left[fn](scope, context);
let rightNode = this.right[fn](scope, context);
let left, right;
if (leftNode.type !== 'number' || rightNode.type !== 'number'){
if (!(leftNode instanceof ConstantNode) || !(rightNode instanceof ConstantNode)){
return new OperatorNode({
left: leftNode,
right: rightNode,