Last active
May 20, 2023 11:09
-
-
Save flynx/09e88f624237e2c0325c99b14588a771 to your computer and use it in GitHub Desktop.
a tiny little lisp interpreter written in js...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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