Git Product home page Git Product logo

stancalc's Introduction

stancalc

Interactive, customizable calculation engine

stancalc's People

Contributors

jrwalt4 avatar

Watchers

 avatar  avatar

stancalc's Issues

Expression Parser

define(['jsep'], function(jsep) {

  const defaultContext = {
    sqrt: Math.sqrt,
    log: Math.log,
    log10: Math.log10,
    sin: Math.sin,
    cos: Math.cos,
    tan: Math.tan,
    e: Math.E
  };
  class Expression {
    constructor(expr) {
      this.src = expr;
      this.tree = jsep(expr);
      this.root = buildNode(this.tree);
    }
    evaluate(context) {
      var result = this.root.evaluate(Object.assign({}, defaultContext, context));
      console.log(`${this.src} = ${result.toFixed(2)}`, context);
      return result;
    }
    static eval(expr, context) {
      return (new Expression(expr)).evaluate(context);
    }
  }
  
  function buildNode(node) {
    switch (node.type) {
      case 'BinaryExpression': {
        let left = buildNode(node.left);
        let right = buildNode(node.right);
        return new ExpOperator(node.operator, [left, right]);
      }
      //case 'UnaryExpression'
      case 'Literal': {
        return new ExpConstant(node.value);
      }
      case 'Identifier': {
        return new ExpVariable(node.name);
      }
      case 'CallExpression': {
        return new ExpOperator(node.callee.name, node.arguments.map(buildNode));
      }
      case 'Compound': {
        let value = buildNode(node.body[0]);
        return new ExpOperator(node.body[1].operator, [value]);
      }
    }
  }
  
  class ExpNode {
    constructor() {
      
    }
    evaluate(context) {
      throw new Error("Abstract method ExpNode#evaluate");
    }
  }
  
  class ExpConstant extends ExpNode {
    constructor(value) {
      super();
      this.value = value;
    }
    evaluate(context) {
      return this.value;
    }
  }
  
  class ExpVariable extends ExpNode {
    constructor(name) {
      super();
      this.name = name;
    }
    evaluate(context) {
      return context[this.name];
    }
  }
  
  let operators = {
    addition: (a,b)=>a+b,
    subtraction: (a,b)=>a-b,
    multiplication: (a,b)=>a*b,
    division: (a,b)=>a/b,
    power: (a,b) => a**b,
    factorial: (n) => n === 0 ? 1 : n * operators.factorial(n-1),
    noop: (a,b) => a
  };
  
  function getOperator(op, context) {
    if (context[op]) {
      if(typeof context[op] === 'function') {
        return context[op];
      }
      throw new TypeError("Operator '"+op+"' is not a function");
    }
    switch(op) {
      case '+':
        return operators.addition;
      case '-':
        return operators.subtraction;
      case '*':
        return operators.multiplication;
      case '\\':
      case '/':
        return operators.division;
      case '^':
        return operators.power;
      case '!':
        return operators.factorial;
      default:
    }
    throw new Error("Could not find operator '"+op+"'");
  }
  
  class ExpOperator extends ExpNode {
    constructor(operator, args) {
      super();
      this.operator = operator;
      this.args = args;
    }
    
    evaluate(context) {
      return getOperator(this.operator, context).apply(null, this.args.map(arg=>arg.evaluate(context)));
    }
  }
  
  return Expression;
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.