Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function BinOperation(operToken, calcFunction, firstOper, secondOper) {
- this.operToken = operToken;
- this.calcFunction = calcFunction;
- this.firstOper = firstOper;
- this.secondOper = secondOper;
- this.toString = () => firstOper.toString() + ' ' + secondOper.toString() + ' ' + this.operToken;
- this.evaluate = (...args) => this.calcFunction(firstOper.evaluate(...args), secondOper.evaluate(...args));
- }
- function Add(...opers) {
- BinOperation.call(this, '+', (a, b) => a + b, ...opers);
- }
- function Subtract(...opers) {
- BinOperation.call(this, '-', (a, b) => a - b, ...opers);
- }
- function Multiply(...opers) {
- BinOperation.call(this, '*', (a, b) => a * b, ...opers);
- }
- function Divide(...opers) {
- BinOperation.call(this, '/', (a, b) => a / b, ...opers);
- }
- function Negate(oper) {
- this.oper = oper;
- this.toString = () => oper.toString() + ' negate';
- this.evaluate = (...args) => (-1) * oper.evaluate(...args);
- }
- function Const(val) {
- this.val = val;
- this.toString = () => val.toString();
- this.evaluate = () => val;
- }
- function Variable(varName){
- this.varName = varName;
- this.toString = () => varName;
- this.evaluate = (...args) => {
- if (varName === 'x') return args[0];
- if (varName === 'y') return args[1];
- if (varName === 'z') return args[2];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement