A Lisp style s-expression data structure for JavaScript.
npm i clone git+https://github.com/vicjohnson1213/js-expression.git
The s-expression created from the string passesd into the constructor.
Used to create a new instance of a JSExpression.
Arguments
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.
Used to determine whether or not a JSExpression matches a specific template.
Arguments
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
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')];
*/
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;
*/
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';
*/
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;
*/
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';
*/