Created
March 1, 2010 08:56
-
-
Save anonymous/318218 to your computer and use it in GitHub Desktop.
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
load(["lib/underscore.js"]); | |
load(["lib/json2.js"]); | |
var code = ['def', 'add', ['fn', ['x','y'], ['+','x','y']]]; | |
var fns = { | |
'quote': function(body) { | |
return JSON.stringify(body); | |
}, | |
'def': function(name, body) { | |
return name + " = " + compile(body) + ";"; | |
}, | |
'fn': function(arglist, body) { | |
return "function("+arglist.join(",")+") {\n return " + compile(body) + ";\n}"; | |
}, | |
'+': function() { | |
return _.reduce(arguments, "", function(xs,y) { return xs + "+" + y}).substring(1); | |
} | |
}; | |
function compile(exp) { | |
if(_.isArray(exp)) { | |
return fns[_.first(exp)].apply(this, _.rest(exp)); | |
} else { | |
return exp; | |
} | |
} | |
print(compile(code)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment