Coder Social home page Coder Social logo

interpreter's Introduction

Rather

Rather is a simple expression evaluator.

Syntax documentation and reference can be found in the file rather.md.


Installation

npm i rather

Usage

Import Rather from the library.

import Rather from 'rather';

Instantiate a new instance.

const rather = new Rather();

Evaluate an expression.

const result = rather.eval('4 <= 5');
console.log(result); // logs 'true'

Data and Rule Sets

Add user specified data to the evaluation context. Reference the data in expressions using dot notation or bracket syntax.

const rather = new Rather({ some: 'foo', data: 'bar', here: 'baz', a: { thing: 2 } });
const result = rather.eval('some = "foo"'); // returns true
const result1 = rather.eval('a["thing"] = 2'); // returns true

Use rules to allow for and store more complex expressions.

const rather = new Rather(
  { some: 'foo', data: 'bar', here: 'baz' },
  { rule1: 'not (some = data)', rule2: ':rule1 and (here = "baz")' }
);
const result = rather.eval(':rule1 and :rule2'); // returns true

Rules and data are public properties. Set them to your desired objects without creating a new instance.

rather.rules = { rule1: 'not (some = data)', rule2: ':rule1 and (here = "baz")' };
rather.data = { here: "is", some: "data" };

Repl

Rather comes packaged with a basic repl to test and evaluate expressions.

To run the repl, navigate to the lib's directory and run the following command:

npm start

To exit the repl type exit or press ^C.

To add a rule set to the repl compile the rules in a JSON object and type the following command:

>> repl.data = <JSON string of rules>

To assign data to the repl execution context, compile the data as a JSON string and type the following command at the repl prompt:

>> repl.data = <JSON string of data>

Unit Tests

Unit tests use Node.js's native test runner from Node.js 22 or later to report coverage.

Run the test suite using the following command:

npm test

Acknowledgements

Much of the code here is produced with reference to Robert Nystrom's Crafting Interpreters and Thorsten Ball's Writing an Interpreter in Go.

interpreter's People

Contributors

jeremiahlangner avatar

Watchers

 avatar

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.