SHARE
TWEET

Untitled

a guest Mar 26th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. function BinOperation(a, b) {
  4.     this.a = a;
  5.     this.b = b;
  6.  
  7.     this.evaluate = (...args) => this.evaluateImpl(a.evaluate(...args), b.evaluate(...args));
  8.     this.toString = () => a.toString() + " " + b.toString() + " " + this.Operation;
  9. }
  10.  
  11. function Add(a, b) {
  12.     BinOperation.call(this, a, b);
  13.  
  14.     this.Operation = "+";
  15.     this.evaluateImpl = (a, b) => a + b;
  16.     this.diff = (name) => new Add(this.a.diff(name), this.b.diff(name));
  17. }
  18.  
  19. function Subtract(a, b) {
  20.     BinOperation.call(this, a, b);
  21.  
  22.     this.Operation = "-";
  23.     this.evaluateImpl = (a, b) => a - b;
  24.     this.diff = (name) => new Subtract(this.a.diff(name), this.b.diff(name));
  25. }
  26.  
  27. function Multiply(a, b) {
  28.     BinOperation.call(this, a, b);
  29.  
  30.     this.Operation = "*";
  31.     this.evaluateImpl = (a, b) => a * b;
  32.     this.diff = (name) => new Add(
  33.         new Multiply(this.a.diff(name), this.b),
  34.         new Multiply(this.a, this.b.diff(name))
  35.     );
  36. }
  37.  
  38. function Divide(a, b) {
  39.     BinOperation.call(this, a, b);
  40.  
  41.     this.Operation = "/";
  42.     this.evaluateImpl = (a, b) => a / b;
  43.  
  44.     this.diff = (name) => new Divide(
  45.         new Subtract(
  46.             new Multiply(this.a.diff(name), this.b),
  47.             new Multiply(this.a, this.b.diff(name))
  48.         ),
  49.         new Multiply(this.b, this.b)
  50.     );
  51. }
  52.  
  53.  
  54. function Variable(name) {
  55.     this.name = name;
  56.  
  57.     this.evaluate = function(...args) {
  58.         if (this.name === 'x') return args[0];
  59.         if (this.name === 'y') return args[1];
  60.         if (this.name === 'z') return args[2];
  61.     }
  62.  
  63.     this.toString = () => this.name;
  64.  
  65.     this.diff = (name) => this.name === name ? new Const(1) : new Const(0);
  66. }
  67.  
  68. function Const(val) {
  69.     this.val = val;
  70.  
  71.     this.evaluate = () => this.val;
  72.     this.toString = () => this.val.toString();
  73.  
  74.     this.diff = (name) => new Const(0);
  75. }
  76.  
  77. function Negate(a) {
  78.     this.a = a;
  79.  
  80.     this.evaluate = (...args) => (-1) * this.a.evaluate(...args);
  81.     this.toString = () => a.toString() + " negate";
  82.  
  83.     this.diff = (name) => new Negate(this.a.diff(name));
  84. }
  85.  
  86. const operationToArity = {
  87.     'negate': 1,
  88.     '+' : 2,
  89.     '-' : 2,
  90.     '*' : 2,
  91.     '/' : 2,
  92. };
  93.  
  94. const parse = function(expression) {
  95.     let stack = [];
  96.     let tokens = expression.split(' ').filter((word) => word !== '');
  97.     for (let token of tokens) {
  98.         let arity = token in operationToArity ? operationToArity[token] : 0;
  99.         let args = [];
  100.         for (let i = 0; i < arity; i++) {
  101.             args.push(stack.pop());
  102.         }
  103.         args = args.reverse();
  104.         //stack.push(operationToFuncrion[token](...args));
  105.         stack.push(makeOperation(token, ...args));
  106.     }
  107.     return stack.pop();
  108. }
  109.  
  110. const makeOperation = function (token, ...args) {
  111.     switch (token) {
  112.         case 'negate': return new Negate(...args);
  113.         case "+": return new Add(...args);
  114.         case "-": return new Subtract(...args);
  115.         case "*": return new Multiply(...args);
  116.         case "/": return new Divide(...args);
  117.         case "x": return new Variable("x");
  118.         case "y": return new Variable("y");
  119.         case "z": return new Variable("z");
  120.     }
  121.     return new Const(Number.parseInt(token));
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top