Coder Social home page Coder Social logo

gaussik's Introduction

Gaussik is a tiny math parser

About 2K non-minified, non-gzipped. Processes the input string with regular expressions (not a single AST!) to allow calling math functions without object prefixes. Rewrites specified infix operators into function calls to allow for x^y exponentiation. Outputs a callable js function that runs almost as fast as native code. Highly configurable. Nice, minimal API. Runs in browser and in node.

API guide

  • parser(scope, infix) creates a parser instance. Properties of the scope object can be accessed as globals in the input. Any key in infix object is converted into infix[key](arg1, arg2). Returns a function. Both arguments are optional and default to empty objects. Examples:
var parseReal = parser(Math, {'^': 'pow'}); // usually you want this
var parseCos = parser({cos: Math.cos}, {});
var plainParse = parser();
var arithmParse = parser({}, {'^': pow});
  • <parser instance>(expr, args) converts expr string into a js function with argument order specified with args, an optional (no arguments assumed) comma-separated list or an array. Examples:
parseReal('(2 + x)^2 + sin(y)', 'x, y')
// --> function(x, y) { return Math.pow(2 + x, 2) + Math.sin(y); }
parseCos('cos(x) + Math.cos(x)', 'x');
// --> function(x) { return Math.cos(x) + Math.cos(x); }
plainParse('2 + 2');
// --> function() { return 2 + 2; }

Disclaimers:

  • Garbage in -- garbage out. No input validation. When an invalid string is supplied, the paser may crash on compilation. Wrap with try / catch.
  • Limited infix operator support. All operators have the same precedence and are left-associative.
  • No default configuration. See this API for useful setups.
  • (Almost) no sugar. Processing only affects scoping and infix operators. sin x for function application and x y for product are invalid.
  • String literals may go wrong. Processing also affects them.
  • Scoping rules imitate js. Arguments override parser scope, parser scope overrides global scope.
  • Scope is stored by reference. If it mutates, the functions use the new values. If properties are added or removed, things can go wrong. Object.seal suggested for scope object in ES5 environments.

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.