[
  {
    "path": ".gitignore",
    "content": "node_modules/"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2013-2014 Mary Rose Cook and contributors\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE."
  },
  {
    "path": "README.md",
    "content": "# Little Lisp\n\nA mini Lisp interpreter in JavaScript.  Supports lists (obvs), function invocation, lambdas, lets, if statements, numbers, strings and the library functions `first`, `rest` and `print`.\n\n* By Mary Rose Cook\n* https://maryrosecook.com\n* mary@maryrosecook.com\n\nThank you to Martin Tornwall for the implementations of let and if.\n\n## Repl\n\n```\n$ node repl.js\n```\n\n## Some runnable programs\n\n```lisp\n1\n```\n\n```lisp\n(first (1 2 3))\n```\n\n```lisp\n((lambda (x) (rest x)) (\"a\" \"b\" \"c\"))\n```\n"
  },
  {
    "path": "littlelisp.js",
    "content": ";(function(exports) {\n  var library = {\n    first: function(x) {\n      return x[0];\n    },\n\n    rest: function(x) {\n      return x.slice(1);\n    },\n\n    print: function(x) {\n      console.log(x);\n      return x;\n    }\n  };\n\n  var Context = function(scope, parent) {\n    this.scope = scope;\n    this.parent = parent;\n\n    this.get = function(identifier) {\n      if (identifier in this.scope) {\n        return this.scope[identifier];\n      } else if (this.parent !== undefined) {\n        return this.parent.get(identifier);\n      }\n    };\n  };\n\n  var special = {\n    let: function(input, context) {\n      var letContext = input[1].reduce(function(acc, x) {\n        acc.scope[x[0].value] = interpret(x[1], context);\n        return acc;\n      }, new Context({}, context));\n\n      return interpret(input[2], letContext);\n    },\n\n    lambda: function(input, context) {\n      return function() {\n        var lambdaArguments = arguments;\n        var lambdaScope = input[1].reduce(function(acc, x, i) {\n          acc[x.value] = lambdaArguments[i];\n          return acc;\n        }, {});\n\n        return interpret(input[2], new Context(lambdaScope, context));\n      };\n    },\n\n    if: function(input, context) {\n      return interpret(input[1], context) ?\n        interpret(input[2], context) :\n        interpret(input[3], context);\n    }\n  };\n\n  var interpretList = function(input, context) {\n    if (input.length > 0 && input[0].value in special) {\n      return special[input[0].value](input, context);\n    } else {\n      var list = input.map(function(x) { return interpret(x, context); });\n      if (list[0] instanceof Function) {\n        return list[0].apply(undefined, list.slice(1));\n      } else {\n        return list;\n      }\n    }\n  };\n\n  var interpret = function(input, context) {\n    if (context === undefined) {\n      return interpret(input, new Context(library));\n    } else if (input instanceof Array) {\n      return interpretList(input, context);\n    } else if (input.type === \"identifier\") {\n      return context.get(input.value);\n    } else if (input.type === \"number\" || input.type === \"string\") {\n      return input.value;\n    }\n  };\n\n  var categorize = function(input) {\n    if (!isNaN(parseFloat(input))) {\n      return { type:'number', value: parseFloat(input) };\n    } else if (input[0] === '\"' && input.slice(-1) === '\"') {\n      return { type:'string', value: input.slice(1, -1) };\n    } else {\n      return { type:'identifier', value: input };\n    }\n  };\n\n  var parenthesize = function(input, list) {\n    if (list === undefined) {\n      return parenthesize(input, []);\n    } else {\n      var token = input.shift();\n      if (token === undefined) {\n        return list.pop();\n      } else if (token === \"(\") {\n        list.push(parenthesize(input, []));\n        return parenthesize(input, list);\n      } else if (token === \")\") {\n        return list;\n      } else {\n        return parenthesize(input, list.concat(categorize(token)));\n      }\n    }\n  };\n\n  var tokenize = function(input) {\n    return input.split('\"')\n                .map(function(x, i) {\n                   if (i % 2 === 0) { // not in string\n                     return x.replace(/\\(/g, ' ( ')\n                             .replace(/\\)/g, ' ) ');\n                   } else { // in string\n                     return x.replace(/ /g, \"!whitespace!\");\n                   }\n                 })\n                .join('\"')\n                .trim()\n                .split(/\\s+/)\n                .map(function(x) {\n                  return x.replace(/!whitespace!/g, \" \");\n                });\n  };\n\n  var parse = function(input) {\n    return parenthesize(tokenize(input));\n  };\n\n  exports.littleLisp = {\n    parse: parse,\n    interpret: interpret\n  };\n})(typeof exports === 'undefined' ? this : exports);\n"
  },
  {
    "path": "littlelisp.spec.js",
    "content": "var t = require('./littlelisp').littleLisp;\n\nvar is = function(input, type) {\n  return Object.prototype.toString.call(input) === '[object ' + type + ']';\n};\n\n// takes an AST and replaces type annotated nodes with raw values\nvar unannotate = function(input) {\n  if (is(input, 'Array')) {\n    if (input[0] === undefined) {\n      return [];\n    } else if (is(input[0], 'Array')) {\n      return [unannotate(input[0])].concat(unannotate(input.slice(1)));\n    } else {\n      return unannotate(input[0]).concat(unannotate(input.slice(1)));\n    }\n  } else {\n    return [input.value];\n  }\n};\n\ndescribe('littleLisp', function() {\n  describe('parse', function() {\n    it('should lex a single atom', function() {\n      expect(t.parse(\"a\").value).toEqual(\"a\");\n    });\n\n    it('should lex an atom in a list', function() {\n      expect(unannotate(t.parse(\"()\"))).toEqual([]);\n    });\n\n    it('should lex multi atom list', function() {\n      expect(unannotate(t.parse(\"(hi you)\"))).toEqual([\"hi\", \"you\"]);\n    });\n\n    it('should lex list containing list', function() {\n      expect(unannotate(t.parse(\"((x))\"))).toEqual([[\"x\"]]);\n    });\n\n    it('should lex list containing list', function() {\n      expect(unannotate(t.parse(\"(x (x))\"))).toEqual([\"x\", [\"x\"]]);\n    });\n\n    it('should lex list containing list', function() {\n      expect(unannotate(t.parse(\"(x y)\"))).toEqual([\"x\", \"y\"]);\n    });\n\n    it('should lex list containing list', function() {\n      expect(unannotate(t.parse(\"(x (y) z)\"))).toEqual([\"x\", [\"y\"], \"z\"]);\n    });\n\n    it('should lex list containing list', function() {\n      expect(unannotate(t.parse(\"(x (y) (a b c))\"))).toEqual([\"x\", [\"y\"], [\"a\", \"b\", \"c\"]]);\n    });\n\n    describe('atoms', function() {\n      it('should parse out numbers', function() {\n        expect(unannotate(t.parse(\"(1 (a 2))\"))).toEqual([1, [\"a\", 2]]);\n      });\n    });\n  });\n\n  describe('interpret', function() {\n    describe('lists', function() {\n      it('should return empty list', function() {\n        expect(t.interpret(t.parse('()'))).toEqual([]);\n      });\n\n      it('should return list of strings', function() {\n        expect(t.interpret(t.parse('(\"hi\" \"mary\" \"rose\")'))).toEqual(['hi', \"mary\", \"rose\"]);\n      });\n\n      it('should return list of numbers', function() {\n        expect(t.interpret(t.parse('(1 2 3)'))).toEqual([1, 2, 3]);\n      });\n\n      it('should return list of numbers in strings as strings', function() {\n        expect(t.interpret(t.parse('(\"1\" \"2\" \"3\")'))).toEqual([\"1\", \"2\", \"3\"]);\n      });\n    });\n\n    describe('atoms', function() {\n      it('should return string atom', function() {\n        expect(t.interpret(t.parse('\"a\"'))).toEqual(\"a\");\n      });\n\n      it('should return string with space atom', function() {\n        expect(t.interpret(t.parse('\"a b\"'))).toEqual(\"a b\");\n      });\n\n      it('should return string with opening paren', function() {\n        expect(t.interpret(t.parse('\"(a\"'))).toEqual(\"(a\");\n      });\n\n      it('should return string with closing paren', function() {\n        expect(t.interpret(t.parse('\")a\"'))).toEqual(\")a\");\n      });\n\n      it('should return string with parens', function() {\n        expect(t.interpret(t.parse('\"(a)\"'))).toEqual(\"(a)\");\n      });\n\n      it('should return number atom', function() {\n        expect(t.interpret(t.parse('123'))).toEqual(123);\n      });\n    });\n\n    describe('invocation', function() {\n      it('should run print on an int', function() {\n        expect(t.interpret(t.parse(\"(print 1)\"))).toEqual(1);\n      });\n\n      it('should return first element of list', function() {\n        expect(t.interpret(t.parse(\"(first (1 2 3))\"))).toEqual(1);\n      });\n\n      it('should return rest of list', function() {\n        expect(t.interpret(t.parse(\"(rest (1 2 3))\"))).toEqual([2, 3]);\n      });\n    });\n\n    describe('lambdas', function() {\n      it('should return correct result when invoke lambda w no params', function() {\n        expect(t.interpret(t.parse(\"((lambda () (rest (1 2))))\"))).toEqual([2]);\n      });\n\n      it('should return correct result for lambda that takes and returns arg', function() {\n        expect(t.interpret(t.parse(\"((lambda (x) x) 1)\"))).toEqual(1);\n      });\n\n      it('should return correct result for lambda that returns list of vars', function() {\n        expect(t.interpret(t.parse(\"((lambda (x y) (x y)) 1 2)\"))).toEqual([1, 2]);\n      });\n\n      it('should get correct result for lambda that returns list of lits + vars', function() {\n        expect(t.interpret(t.parse(\"((lambda (x y) (0 x y)) 1 2)\"))).toEqual([0, 1, 2]);\n      });\n\n      it('should return correct result when invoke lambda w params', function() {\n        expect(t.interpret(t.parse(\"((lambda (x) (first (x))) 1)\")))\n          .toEqual(1);\n      });\n    });\n\n    describe('let', function() {\n      it('should eval inner expression w names bound', function() {\n        expect(t.interpret(t.parse(\"(let ((x 1) (y 2)) (x y))\"))).toEqual([1, 2]);\n      });\n\n      it('should not expose parallel bindings to each other', function() {\n        // Expecting undefined for y to be consistent with normal\n        // identifier resolution in littleLisp.\n        expect(t.interpret(t.parse(\"(let ((x 1) (y x)) (x y))\"))).toEqual([1, undefined]);\n      });\n\n      it('should accept empty binding list', function() {\n        expect(t.interpret(t.parse(\"(let () 42)\"))).toEqual(42);\n      });\n    });\n\n    describe('if', function() {\n      it('should choose the right branch', function() {\n        expect(t.interpret(t.parse(\"(if 1 42 4711)\"))).toEqual(42);\n        expect(t.interpret(t.parse(\"(if 0 42 4711)\"))).toEqual(4711);\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"littlelisp\",\n  \"description\": \"An interpreter for a little lisp.\",\n  \"author\": \"Mary Rose Cook <mary@maryrosecook.com> (https://maryrosecook.com/)\",\n  \"version\": \"0.1.0\",\n  \"scripts\": {\n    \"test\": \"node_modules/jasmine-node/bin/jasmine-node *.spec.js\"\n  },\n  \"dependencies\": {\n    \"jasmine-node\": \"^1.14.5\"\n  }\n}\n"
  },
  {
    "path": "repl.js",
    "content": "var repl = require(\"repl\");\nvar littleLisp = require(\"./littlelisp\").littleLisp;\n\nrepl.start({\n  prompt: \"> \",\n  eval: function(cmd, context, filename, callback) {\n    var ret = littleLisp.interpret(littleLisp.parse(cmd));\n    callback(null, ret);\n  }\n});\n"
  }
]