Giter Club home page Giter Club logo

js-expression's Introduction

JS-Expression

Build Status

A Lisp style s-expression data structure for JavaScript.

npm i clone git+https://github.com/vicjohnson1213/js-expression.git

Fields

expression

The s-expression created from the string passesd into the constructor.

Instance Methods

constructor(stringExpression)

Used to create a new instance of a JSExpression.

Arguments

  1. stringExpression (string): The string to build the JSExpression from.

Example:

var jsExp = new JSExpression('(+ 1 2)');
// Returns a new JSExpression object with the s-expression created from the string passed in.

match(template)

Used to determine whether or not a JSExpression matches a specific template.

Arguments

  1. template (JSExpression): The template JSExpression.

Template Structure

A template should be any JSExpression with all legal tokens of a regular JSExpression along with an additional few to alow for more general matching.

The additional symbols are as follows:

  • NUMBER: Match any number (integer, floating point, scientific notation, etc.).
  • STRING: Match any string enclosed in double quotation marks.
  • BOOLEAN: Match either of the words true or false.
  • SYMBOL: Match any legal variable name preceded by a single quote.
  • ANY: Match anything, including additional expressions.
  • ...: Match the previous template type for an arbitrary number of tokens (may only be done with the last token of an expression.

Examples:

var template1 = new JSExpression('(func NUMBER NUMBER)');
var badTemplate1 = new JSExpression('(func NUMBER STRING)');
var exp1 = new JSExpression('(func 1 2)');

exp1.match(template1); // Returns true.
exp1.match(badTemplate1); // Returns false.

var template2 = new JSExpression('(func STRING NUMBER ...)')
var exp2 = new JSExpression('(func "some string" 1 2 3 4 5)');
var bigExp2 = new JSEpression('(func "str" 1 2 3 4 5 6 7 8 9 10)')

exp2.match(template2); // Returns true.
bigExp2.match(template2); // Still returns true.

var template3 = new JSExpression('(func ANY)');
var exp3 = new JSExpression('(func 123)');
var exp4 = new JSExpression('(func true)');
var exp5 = new JSExpression('(func (+ 1 2))');

exp3.match(template3); // Returns true
exp4.match(template3); // Returns true
exp5.match(template3); // Returns true

toArray()

Used to convert a JSExpression to an array of JSExpressions.

Returns an array of JSExpression.

Example:

var exp = new JSExpression('(+ 1 2)');
exp.toArray();

/*
 * returns [new JSExpression('+'),
 *            new JSExpression('1'),
 *            new JSExpression('2')];
 */

toNumber()

Used to convert a JSExpression to a number.

Returns a number or throws an error of the expression is not a number.

Example:

var exp = new JSExpression('1');
exp.toNumber();

/*
 * returns 1;
 */

toString()

Used to convert a JSExpression to a string.

Returns a string or throws an error of the expression is not a string.

Example:

var exp = new JSExpression('"string"');
exp.toString();

/*
 * returns 'string';
 */

toBoolean()

Used to convert a JSExpression to a boolean.

Returns a boolean or throws an error of the expression is not a boolean.

Example:

var exp = new JSExpression('true');
exp.toBoolean();

/*
 * returns true;
 */

toSymbol()

Used to convert a JSExpression to a symbol.

Returns a symbol (string) or throws an error of the expression is not a symbol.

Example:

var exp = new JSExpression('\'sym');
exp.toSymbol();

/*
 * returns '\'sym';
 */

js-expression's People

Contributors

vicjohnson1213 avatar

Watchers

 avatar  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.