Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. function BinOperation(operToken, calcFunction, firstOper, secondOper) {
  4.     this.operToken = operToken;
  5.     this.calcFunction = calcFunction;
  6.     this.firstOper = firstOper;
  7.     this.secondOper = secondOper;
  8.  
  9.     this.toString = () => firstOper.toString() + ' ' + secondOper.toString() + ' ' + this.operToken;
  10.     this.evaluate = (...args) => this.calcFunction(firstOper.evaluate(...args), secondOper.evaluate(...args));
  11. }
  12.  
  13. function Add(...opers) {
  14.     BinOperation.call(this, '+', (a, b) => a + b, ...opers);
  15. }
  16.  
  17. function Subtract(...opers) {
  18.     BinOperation.call(this, '-', (a, b) => a - b, ...opers);
  19. }
  20.  
  21. function Multiply(...opers) {
  22.     BinOperation.call(this, '*', (a, b) => a * b, ...opers);
  23. }
  24.  
  25. function Divide(...opers) {
  26.     BinOperation.call(this, '/', (a, b) => a / b, ...opers);
  27. }
  28.  
  29. function Negate(oper) {
  30.     this.oper = oper;
  31.  
  32.     this.toString = () => oper.toString() + ' negate';
  33.     this.evaluate = (...args) => (-1) * oper.evaluate(...args);
  34. }
  35.  
  36. function Const(val) {
  37.     this.val = val;
  38.  
  39.     this.toString = () => val.toString();
  40.     this.evaluate = () => val;
  41. }
  42.  
  43. function Variable(varName){
  44.     this.varName = varName;
  45.  
  46.     this.toString = () => varName;
  47.     this.evaluate = (...args) => {
  48.         if (varName === 'x') return args[0];
  49.         if (varName === 'y') return args[1];
  50.         if (varName === 'z') return args[2];
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement