Skip to content

Instantly share code, notes, and snippets.

@flynx
Last active May 20, 2023 11:09
Show Gist options
  • Save flynx/09e88f624237e2c0325c99b14588a771 to your computer and use it in GitHub Desktop.
Save flynx/09e88f624237e2c0325c99b14588a771 to your computer and use it in GitHub Desktop.
a tiny little lisp interpreter written in js...
var lisp = function(c, context){
return c instanceof Array ?
(typeof(c[0]) == typeof('str')
&& c[0] in (context || ns) ?
(context || ns)[c[0]].call((context || ns),
...c.slice(1)
.map(e => lisp(e, context)))
: c.map(e => lisp(e, context)))
: c }
var ns = {
print: function(...args){
console.log(...args) },
add: function(...args){
return args.reduce(
function(res, c){
return res + c },
0) },
}
// Example...
lisp([
['print', 'hello from lisp!', 1, ['add', 5, -3], 3],
])
// vim:set ts=4 sw=4 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment