Created
February 5, 2011 22:15
-
-
Save plobsing/812857 to your computer and use it in GitHub Desktop.
[Ωη;)XD] primitive lisp (massaged from OMeta examples)
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
// Inspired by McCarthy's meta-circular lisp | |
ometa Lisp { | |
ev = string:a -> self['env'][a] | |
| [#lambda :fs :body] -> [#lambda, fs, body] | |
| [#quote :ans] -> ans | |
| [#cond evCond:ans] -> ans | |
| [ev:f ev*:xs] app(f, xs):ans -> ans, | |
evCond = condF* condT:ans anything* -> ans, | |
condT = [ev:x ?x ev:ans] -> ans, | |
condF = ~condT anything, | |
app = #car [[:hd anything*]] -> hd | |
| #cdr [[:hd anything*:tl]] -> tl | |
| #cons [:hd :tl] -> [hd].append(tl) | |
| #atom [:x] -> (!(x instanceof ResizablePMCArray)) | |
| #eq [:x :y] -> (x == y) | |
| [#lambda :fs :body] :args | |
enter bind(fs, args) ev(body):ans leave -> ans, | |
enter = { | |
var nenv = { '_p': self['env'] }; | |
for (string k in self['env']) nenv[k] = self['env'][k]; | |
self['env'] = nenv | |
}, | |
bind = [] [] | |
| [:f anything*:fs] [:a anything*:as] bind(fs, as) -> (self['env'][f] = a), | |
leave = -> (self['env'] = self['env']['_p']) | |
} | |
function lisp_init_pmc[anon, method](var p) { | |
self['env'] = { | |
car: 'car', | |
cdr: 'cdr', | |
cons: 'cons', | |
atom: 'atom', | |
eq: 'eq', | |
nil: null, | |
T: 'T' | |
}; | |
} | |
function init[anon,load,init]() { | |
using static lisp_init_pmc; | |
get_class('Lisp').add_vtable_override('init_pmc', lisp_init_pmc); | |
} | |
function lispEval(var x) { | |
return (new Lisp).match(x, 'ev'); | |
} | |
function pp(var x) { | |
if (x instanceof String | |
|| x instanceof Integer | |
|| x instanceof Float) { | |
print(string(x)); | |
} else if (x instanceof ResizablePMCArray) { | |
print("("); | |
for (var y in x) { | |
pp(y); | |
print(" "); | |
} | |
print(")"); | |
} else { | |
die("Unknown type"); | |
} | |
} | |
function main[main](var argv) { | |
var lists = [ | |
[`quote, [1, 2, 3]], | |
[`car, [`quote, [1, 2, 3]]], | |
[`cdr, [`quote, [1, 2, 3]]], | |
[`cons, [`quote, `x], [`quote, `y]], | |
[`eq, [`quote, `x], [`quote, `x]], | |
[[`lambda, [`x], [`cons, `x, `x]], [`quote, `boo]] | |
]; | |
for (var l in lists) { | |
print('> '); pp(l); print("\n"); | |
pp(lispEval(l)); print("\n"); | |
print("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment