Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2010 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/318218 to your computer and use it in GitHub Desktop.
Save anonymous/318218 to your computer and use it in GitHub Desktop.
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