本文整理汇总了Python中env.Env.outer方法的典型用法代码示例。如果您正苦于以下问题:Python Env.outer方法的具体用法?Python Env.outer怎么用?Python Env.outer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类env.Env
的用法示例。
在下文中一共展示了Env.outer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import outer [as 别名]
def EVAL(ast, env):
if ast=="":
return ""
while True:
log(str(ast),"EVAL")
ast = macroexpand(ast,env)
if not isinstance(ast,list):
return eval_ast(ast,env)
f = ast[0]
if f=="def!":
v = EVAL(ast[2],env)
env.set(ast[1], v)
return v
elif f=="let*":
bindings = ast[1]
log(str(bindings),"bindings")
if len(bindings)%2!=0:
raise
cenv = Env()
cenv.outer = env
for k,vast in chunked(bindings,2):
v = EVAL(vast,cenv)
cenv.set(k,v)
new_env = Env(cenv)
cenv = new_env
ast = ast[2]
env = cenv
#return EVAL(ast[2],cenv)
elif f=="do":
eval_ast(ast[1:-1],env)
ast = ast[-1]
elif f=="if":
cond = EVAL(ast[1],env)
if (type(cond)==bool and cond) or (type(cond)!=bool and cond!="nil"):
ast = ast[2]
elif len(ast)==3:
ast = "nil"
else:
ast = ast[3]
elif f=="fn*":
# (fn* (x y) (+ x y))
binds = ast[1]
body = ast[2]
log(str(binds),"function.binds")
log(str(body),"function.body")
fn = lambda *param:EVAL(body,Env(env,binds,param))
return {
"ast": body
,"params": binds
,"env": env
,"fn": fn
,"__fn__": True
,"is_macro": False
}
elif f=="quote":
return ast[1]
elif f=="quasiquote":
ast = quasiquote(ast[1])
elif f=="defmacro!":
fun = EVAL(ast[2],env)
fun["is_macro"] = True
env.set(ast[1], fun)
return fun
elif f=="macroexpand":
return macroexpand(ast[1],env)
elif f=="try*":
# (try* A (catch* B C))
try:
return EVAL(ast[1],env)
except Exception as e:
bind = [ast[2][1]]
expr = [e.args[0]]
env = Env(outer=env,binds=bind,exprs=expr)
ast = ast[2][2]
else:
evaled = eval_ast(ast, env)
f = evaled[0]
p = evaled[1:]
if type(f)==dict:
ast = f["ast"]
env = Env(f["env"],f["params"],p)
else:
return f(*p)