Code Monkey home page Code Monkey logo

angu's Introduction

Angu

A small, zero-dependency library that can be used to build and evaluate mini-languages in the browser or in NodeJS. You have complete control over every operation performed, and this library takes care of the nitty gritty of the parsing and such. Comes with TypeScript typings (usage optional).

We can use this to create a simple in-browser calculator to evaluate things like:

10 + 4 / 2 * (3 - 1)

Or we can use it to manipulate table cells by evaluating something like:

MEAN(A1:A5) + SUM(C1:D2) - 10

Or we can build a small expression-based language that looks like:

foo = 2;
bar = 4;
wibble = foo * bar + pow(2, 10);
foo + bar + wibble

Or a range of other things.

In each case, we define the operators (optionally with precedence and associativity) and functions available to the program and exactly what they do with their arguments, and this library takes care of the rest.

Complete examples can be found here.

Installation

You can install the latest release from npm:

npm install angu

Basic Usage

First, you define a Context which determines how expressions will be evaluated. For a simple calculator, we might define something like the following:

import { evaluate } from 'angu'

const ctx = {
    // We provide these operators:
    scope: {
        '-': (a, b) => a.eval() - b.eval(),
        '+': (a, b) => a.eval() + b.eval(),
        '/': (a, b) => a.eval() / b.eval(),
        '*': (a, b) => a.eval() * b.eval(),
    },
    // And define the precedence to be as expected
    // (first in array => evaluated first)
    precedence: [
        ['/', '*'],
        ['-', '+']
    ]
}

Then, you can evaluate expressions in this context:

const r1 = evaluate('2 + 10 * 4', ctx)
assert.equal(r1.value, 42)

We can also provide locals at eval time:

const r1 = evaluate('2 + 10 * four', ctx, { four: 4 })
assert.equal(r1.value, 42)

If something goes wrong evaluating the provided string, an error will be returned. All errors returned contain position information ({ pos: { start, end}, ... }) describing the beginning and end of the string that contains the error. Specific errors contain other information depending on their kind.

More examples can be found here.

Details

Primitives

Angu supports the following literals:

  • booleans ('true' or 'false')
  • numbers (eg +1.2, -3, .9, 100, 10.23, -100.4, 10e2). Numbers have a string version of themselves stored (as well as a numeric one) so that we can wrap things like big number libraries if we like. The string version applies some normalisation which can help other libraries consume the numbers:
    • The exponent is normalised to a lowercase 'e'.
    • Leading '+' is removed.
    • A decimal point, if provided, is always prefixed with a number ('0' if no number is given)
  • strings (strings can be surrounded in ' or ", and \'s inside a string escape the delimiter and themselves)

Otherwise, it relies on operators and function calls to transform and manipulate them.

Operators

Any of the following characters can be used to define an operator:

!£$%^&*@#~?<>|/+=;:.-

Operators can be binary (taking two arguments) or unary. Unary operators cannot have a space between themselves and the expression that they are operating on.

Operators not defined in scope will not be parsed. This helps the parser properly handle multiple operators (eg binary and unary ops side by side), since it knows what it is looking for.

Some valid operator based function calls (assuming the operators are in scope):

1+2/3
1 + 2
1 + !2

Functions/variables

Functions/variables must start with an ascii letter, and can then contain an ascii letter, number or underscore.

Some valid function calls:

foo()
foo(bar)
foo(1,2)

If the function takes exactly two arguments, and is also listed in the precedence list, it can be used infix too, like so (there must be spaces separating the function name from the expressions on either side):

1 foo 2

All values passed to functions on scope have the Value type. One can call .eval() on them to evaluate them and return the value that that results in. Some other methods are also available:

  • Value.kind(): Return the kind of the Value ("string" | "number" | "variable" | "bool" | "functioncall").
  • Value.pos(): Return the start and end index of the original input string that this Value was parsed from.
  • Value.toString(): (or String(Value)) gives back a string representation of the value, useful for debugging.
  • Value.name(): Gives back the "name" of the value. This is the function/variable name if applicable, else true/false for bools, the string contents for strings, or the numeric representation for numbers.

See the examples for more, particularly workingWithVariables.ts.

angu's People

Contributors

0xflotus avatar dependabot[bot] avatar jsdw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

0xflotus smt-pi

angu's Issues

Nested "if" code results in exponential execution time

I've defined a simple language with some operators and when testing a really awful nested if condition code, the execution time jumps from just 3ms (great) to over a second.

For example:

if(answer(8)=="test1" and numeric(7)<3, res0,
if(answer(8)=="test1" and numeric(7)==3, res1,
if(answer(8)=="test1" and numeric(7)==4, res1,
if(answer(8)=="test2" and numeric(7)==1, res0,
if(answer(8)=="test2" and numeric(7)==2, res1,
if(answer(8)=="test2" and numeric(7)==3, res2,
if(answer(8)=="test2" and numeric(7)==4, res2,
if(answer(8)=="test3" and numeric(7)==1, res1,
if(answer(8)=="test3" and numeric(7)==2, res2,
if(answer(8)=="test3" and numeric(7)==3, res3,
if(answer(8)=="test3" and numeric(7)==4, res3,
if(answer(8)=="test4" and numeric(7)==1, res1,
if(answer(8)=="test4" and numeric(7)==2, res3,
if(answer(8)=="test4" and numeric(7)>2, res4, res0))))))))))))))

The answer and numeric calls just lookup the number in a dict/map object defined in the scope.
And the operators are defined as follows:

Screen Shot 2022-07-25 at 18 50 29

Screen Shot 2022-07-25 at 18 52 15

With the following helper function:

const numberFunDefault = (a, b, def, fun) => {
  let va = Number(a?.eval());
  let vb = Number(b?.eval());

  if (isNaN(va)) {
    va = def;
  }
  if (isNaN(vb)) {
    vb = def;
  }

  return fun(va, vb);
};

When debugging in chrome, looks like a lot of time is lost in GC:
Screen Shot 2022-07-25 at 18 54 39

Any ideas what could be improved? Note that these kind of nested ifs will actually not be written like this, but rather as a lookup table which runs really quick, but while testing this seemed like an issue.

Custom String Operators

Would it be possible to allow the use of any string as a custom operator?

A cool use case I'd like to explore would be to use this library to write a simple "query" language for some stuff

import angu from "angu";
import _ from "lodash";

const releases = [
  {
    id: 1,
    tags: ["a", "b", "c"]
  },
  {
    id: 2,
    tags: ["a", "1"]
  },
  {
    id: 3,
    tags: ["b", "2"]
  },
  {
    id: 4,
    tags: ["c", "3"]
  }
];

const ctx = {
  scope: {
    "&": (a, b) => _.unionWith([...a.eval(), ...b.eval()], x => x.id),
    "|": (a, b) => _.uniqBy([...a.eval(), ...b.eval()], x => x.id),
    "?": (a, b) => releases.filter(r => r[a.eval()].includes(b.eval()))
  },
  precedence: [["?"], ["&"], ["|"]]
};

const resultsA = angu.evaluate("'tags' ? 'a'", ctx);
console.log("A: ", JSON.stringify(resultsA.value));

const resultsBandC = angu.evaluate("'tags' ? 'b' & 'tags' ? 'c'", ctx);
console.log("B AND C's: ", JSON.stringify(resultsBandC.value));

const results1or3 = angu.evaluate("'tags' ? '1' | 'tags' ? '3'", ctx);
console.log("1 OR 3's: ", JSON.stringify(results1or3.value));

I'd love to be able to use
"INCLUDES" instead of "?"
"AND" instead of "&"
"OR" instead of "|"

I can run the strings through a 'variable' replacement step beforehand, but I think it'd be awesome if the library could support this out of the box.

Example JSON query language

A comment on reddit asked how one might create a simple example JSON query language. I think that this would be a great example to have.

Offhand, I suspect I would define the . operator so that you can write things like foo.bar.0.lark in the simplest case to define field/array acceses. I'd have to look at some of the abilities that something like jq provides to see where to go from there!

Helper to return user-friendly errors?

Just wondering, what's the best way to return user-friendly errors? The errors are a bit cryptic and sometimes even deeply nested in recursive structures.

For example, this is not enough:

const getErrorLine = (expr, start, end) => {
  let text = expr.slice(start, start + end + 8) || 'char 0';
  return text;
};

const raiseError = (expr, res) => {
  let err = res.value;
  if (err.kind === 'EVAL_THROW') {
    throw new Error(`The code evaluation threw an error`);
  } else if (
    err.kind === 'FUNCTION_NOT_DEFINED' ||
    err.kind === 'NOT_A_FUNCTION'
  ) {
    throw new Error(`Function not defined`);
  } else if (err.pos?.start !== undefined && err.pos?.end !== undefined) {
    let text = getErrorLine(expr, err.pos.start, err.pos.end);
    throw new Error(
      `The code has syntax errors or invalid variable names near: ${text}`
    );
  } else {
    throw new Error(`The code has syntax errors.`);
  }
};

Because for EVAL_THROW, the error is sometimes nested like this:

Screen Shot 2022-07-25 at 15 40 42

arrays?

hey does it support array literal or object literal syntaxes?
is it possible to write one as a scope function?

charlie

P.S. this library is awesome, been building with it for a while

Maintained?

@jsdw hey, are you still maintaining this? Further, does this use eval or any other unsafe primitive that may fail CSP policies?

Thanks.

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.