Skip to content

Instantly share code, notes, and snippets.

@q66
Last active December 26, 2015 15:09
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 q66/7170288 to your computer and use it in GitHub Desktop.
Save q66/7170288 to your computer and use it in GitHub Desktop.
lisp interpreter in vortex 2.0
let globs = {
["set" ] = |l, e| globs[l[1]] = l[2],
["cdr" ] = |l, e| rest(l[1]),
["car" ] = |l, e| first(l[1]),
["quote"] = |l, e| l[1],
["cond" ] = |l, e| if let el = firstcond(l[1], |el| eval(el[1], e))
then eval(el[2], e)
["+" ] = |l, e| l[1] + l[2],
["-" ] = |l, e| l[1] - l[2],
["*" ] = |l, e| l[1] * l[2],
["/" ] = |l, e| l[1] / l[2],
["=" ] = |l, e| l[1] == l[2]
}
let lazymap f args env ef = if set("cond", "quote", "set")[fun] then args
else map(|n| ef(n, env), args)
let rec eval list env =
case fun :: args then match globs[fun] with
case x when type(x) == "function" then x(lazymap(fun, args, env, eval), env)
case x then eval(x[3], dict(zip(x[2], lazymap(fun, args, env, eval))))
end
case _ then env[list] or list
-- example
eval({"set", "fact", {"lambda" {"x"},
{"cond", {
{{"=" "x" 0}, 1}
{true, {"*", "x", {"fact", {"-", "x", 1}}}}}}}}, globs)
print(eval({"fact", 6}, globs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment