stancalc
Interactive, customizable calculation engine
Interactive, customizable calculation engine
License: MIT License
Interactive, customizable calculation engine
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;
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.