Last active
April 6, 2018 07:44
-
-
Save boogie666/ed320d67cc33b1202fc99aa8c9c09ff9 to your computer and use it in GitHub Desktop.
128 char lisp interperter
This file contains hidden or 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
//the interperter | |
e=(s,c)=>s/s?s:s.trim?c(s):(S=s[0],S=="if"?e(s[1],c)?e(s[2],c):e(s[3],c):S=="f"?a=>e(s[2],n=>s[1]==n?a:c(n)):e(S,c)(e(s[1],c))); | |
//global ctx | |
function global_scope(name){ | |
if(name === "log") | |
return item => console.log(item); | |
if(name === "+") | |
return a => b => a + b; | |
if(name === "<") | |
return a => b => a < b ? 1 : 0; | |
if(name === "-") | |
return a => b => a - b; | |
if(name === "*") | |
return a => b => a * b; | |
throw name + " is not defined."; | |
} | |
const fib_source_code = | |
["log", [ | |
[["f", "x", ["x", "x"]], | |
["f", "x", | |
[["f", "q", | |
["f", "n", | |
["if", [["<", "n"], 2], | |
"n", | |
[["+", ["q", [["-", "n"], 1]]], ["q", [["-", "n"], 2]]]]]], | |
["f", "arg", | |
[["x", "x"], "arg"]]]]], 10]]; | |
e(fib_source_code, global_scope); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment