Created
November 25, 2012 19:01
-
-
Save diiq/4144809 to your computer and use it in GitHub Desktop.
Evaluator
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
FAIL = Symbol("Fail") | |
def eval(code, env): | |
ret = FAIL | |
if isinstance(code, Call): | |
function = eval(code.call, env) | |
new_env = function.bindings | |
for arg in function.lambda_list: | |
if arg.code: | |
new_env[arg.name] = code.args[arg.name] | |
else: | |
new_env[arg.name] = eval(code.args[arg.name], env) | |
if isinstance(function, Builtin): | |
ret =function.function(new_env, env) | |
elif isinstance(function, Lambda): | |
for c in function.body: | |
ret = eval(c, new_env) | |
elif (isinstance(code, Builtin) or | |
isinstance(code, Number)): | |
ret = code | |
elif isinstance(code, Symbol): | |
ret = env[code.symbol] | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment