Skip to content

Commit

Permalink
Updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
amitayh committed Aug 21, 2015
1 parent 15b4aa7 commit 454ca23
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
# lispjs [![Build Status](https://travis-ci.org/amitayh/lispjs.svg?branch=master)](https://travis-ci.org/amitayh/lispjs)

Simple Lisp implementation in JavaScript

## Example programs

```javascript
var lisp = require('./lisp');

// Fibonacci
var fib = [
// Define fib function
['define', 'fib',
['lambda', ['n'],
['if', ['<', 'n', 2],
1,
['+',
['fib', ['-', 'n', 1]],
['fib', ['-', 'n', 2]]]]]],

// Call function
['fib', 5]
];
console.log(lisp.run(fib)); // Prints 8

// Factorial
var fact = [
// Define fact function
['define', 'fact',
['lambda', ['n'],
['if', ['<', 'n', 2],
1,
['*', 'n', ['fact', ['-', 'n', 1]]]]]],

// Call function
['fact', 5]
];
console.log(lisp.run(fact)); // Prints 120

// Map
var map = [
// Define map function
['define', 'map',
['lambda', ['f', 'coll'],
['if', ['empty', 'coll'],
'coll',
['cons',
['f', ['car', 'coll']],
['map', 'f', ['cdr', 'coll']]]]]],

// Define unary increment function
['define', 'inc',
['lambda', ['num'],
['+', 'num', 1]]],

// Define some collection
['define', 'coll', ['quote', [1, 2, 3]]],

// Map collection with 'inc'
['map', 'inc', 'coll']
];
console.log(lisp.run(map)); // Prints [2, 3, 4]
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"test": "mocha specs/",
"repl": "node src/repl.js"
},
"main": "./src/lisp.js",
"repository": {
"type": "git",
"url": "https://github.com/amitayh/lispjs.git"
Expand Down
8 changes: 4 additions & 4 deletions specs/lisp-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ describe('lispjs', function () {
['define', 'foo', ['lambda', ['arg'], 'arg']],
['foo', 'bar']
];
assert.equal(lisp.getResultMulti(prog, {}), 'bar');
assert.equal(lisp.run(prog), 'bar');
});

it('should support lexical scope', function () {
Expand All @@ -128,7 +128,7 @@ describe('lispjs', function () {
['define', 'bar', ['lambda', [], 'foo']],
['bar']
];
assert.equal(lisp.getResultMulti(prog, {}), 'bar');
assert.equal(lisp.run(prog), 'bar');
});

it('should give local scope higher priority', function () {
Expand All @@ -137,7 +137,7 @@ describe('lispjs', function () {
['define', 'bar', ['lambda', ['foo'], 'foo']],
['bar', 'baz']
];
assert.equal(lisp.getResultMulti(prog, {}), 'baz');
assert.equal(lisp.run(prog), 'baz');
});
});

Expand Down Expand Up @@ -213,7 +213,7 @@ describe('lispjs', function () {
['map', 'inc', 'coll']
];

assert.deepEqual(lisp.getResultMulti(prog, lisp.defaultEnv), [2, 3, 4]);
assert.deepEqual(lisp.run(prog), [2, 3, 4]);
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/lisp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function getResult(expr, env) {
return evaluate(expr, env)[0];
}

function getResultMulti(prog, env) {
var result = [null, env];
function run(prog) {
var result = [null, defaultEnv];
prog.forEach(function (expr) {
result = evaluate(expr, result[1]);
});
Expand Down Expand Up @@ -83,8 +83,8 @@ var defaultEnv = {
};

module.exports = {
defaultEnv: defaultEnv,
evaluate: evaluate,
getResult: getResult,
getResultMulti: getResultMulti,
defaultEnv: defaultEnv
run: run
};

0 comments on commit 454ca23

Please sign in to comment.