Skip to content

Instantly share code, notes, and snippets.

@plobsing
Created February 5, 2011 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plobsing/812857 to your computer and use it in GitHub Desktop.
Save plobsing/812857 to your computer and use it in GitHub Desktop.
[Ωη;)XD] primitive lisp (massaged from OMeta examples)
// 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