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

34 lines
894 B
JavaScript

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