Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function BinOperation(a, b) {
- this.a = a;
- this.b = b;
- this.evaluate = (...args) => this.evaluateImpl(a.evaluate(...args), b.evaluate(...args));
- this.toString = () => a.toString() + " " + b.toString() + " " + this.Operation;
- }
- function Add(a, b) {
- BinOperation.call(this, a, b);
- this.Operation = "+";
- this.evaluateImpl = (a, b) => a + b;
- this.diff = (name) => new Add(this.a.diff(name), this.b.diff(name));
- }
- function Subtract(a, b) {
- BinOperation.call(this, a, b);
- this.Operation = "-";
- this.evaluateImpl = (a, b) => a - b;
- this.diff = (name) => new Subtract(this.a.diff(name), this.b.diff(name));
- }
- function Multiply(a, b) {
- BinOperation.call(this, a, b);
- this.Operation = "*";
- this.evaluateImpl = (a, b) => a * b;
- this.diff = (name) => new Add(
- new Multiply(this.a.diff(name), this.b),
- new Multiply(this.a, this.b.diff(name))
- );
- }
- function Divide(a, b) {
- BinOperation.call(this, a, b);
- this.Operation = "/";
- this.evaluateImpl = (a, b) => a / b;
- this.diff = (name) => new Divide(
- new Subtract(
- new Multiply(this.a.diff(name), this.b),
- new Multiply(this.a, this.b.diff(name))
- ),
- new Multiply(this.b, this.b)
- );
- }
- function Variable(name) {
- this.name = name;
- this.evaluate = function(...args) {
- if (this.name === 'x') return args[0];
- if (this.name === 'y') return args[1];
- if (this.name === 'z') return args[2];
- }
- this.toString = () => this.name;
- this.diff = (name) => this.name === name ? new Const(1) : new Const(0);
- }
- function Const(val) {
- this.val = val;
- this.evaluate = () => this.val;
- this.toString = () => this.val.toString();
- this.diff = (name) => new Const(0);
- }
- function Negate(a) {
- this.a = a;
- this.evaluate = (...args) => (-1) * this.a.evaluate(...args);
- this.toString = () => a.toString() + " negate";
- this.diff = (name) => new Negate(this.a.diff(name));
- }
- const operationToArity = {
- 'negate': 1,
- '+' : 2,
- '-' : 2,
- '*' : 2,
- '/' : 2,
- };
- const parse = function(expression) {
- let stack = [];
- let tokens = expression.split(' ').filter((word) => word !== '');
- for (let token of tokens) {
- let arity = token in operationToArity ? operationToArity[token] : 0;
- let args = [];
- for (let i = 0; i < arity; i++) {
- args.push(stack.pop());
- }
- args = args.reverse();
- //stack.push(operationToFuncrion[token](...args));
- stack.push(makeOperation(token, ...args));
- }
- return stack.pop();
- }
- const makeOperation = function (token, ...args) {
- switch (token) {
- case 'negate': return new Negate(...args);
- case "+": return new Add(...args);
- case "-": return new Subtract(...args);
- case "*": return new Multiply(...args);
- case "/": return new Divide(...args);
- case "x": return new Variable("x");
- case "y": return new Variable("y");
- case "z": return new Variable("z");
- }
- return new Const(Number.parseInt(token));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement