本文整理汇总了Python中env.Env.set方法的典型用法代码示例。如果您正苦于以下问题:Python Env.set方法的具体用法?Python Env.set怎么用?Python Env.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类env.Env
的用法示例。
在下文中一共展示了Env.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
if not isinstance(a0, MalSym):
raise Exception("attempt to apply on non-symbol")
if u"def!" == a0.value:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif u"let*" == a0.value:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
return EVAL(a2, let_env)
else:
el = eval_ast(ast, env)
f = el.values[0]
if isinstance(f, MalFunc):
return f.apply(el.values[1:])
else:
raise Exception("%s is not callable" % f)
示例2: entry_point
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def entry_point(argv):
repl_env = Env()
def REP(str, env):
return PRINT(EVAL(READ(str), env))
# core.py: defined using python
for k, v in core.ns.items():
repl_env.set(_symbol(unicode(k)), MalFunc(v))
# core.mal: defined using the language itself
REP("(def! not (fn* (a) (if a false true)))", repl_env)
while True:
try:
line = mal_readline.readline("user> ")
if line == "": continue
print(REP(line, repl_env))
except EOFError as e:
break
except reader.Blank:
continue
except types.MalException as e:
print(u"Error: %s" % printer._pr_str(e.object, False))
except Exception as e:
print("Error: %s" % e)
#print("".join(traceback.format_exception(*sys.exc_info())))
return 0
示例3: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
while True:
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
ast = macroexpand(ast, env)
if not types._list_Q(ast):
return eval_ast(ast, env)
if len(ast) == 0: return ast
a0 = ast[0]
if "def!" == a0:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif "let*" == a0:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
ast = a2
env = let_env
# Continue loop (TCO)
elif "quote" == a0:
return ast[1]
elif "quasiquote" == a0:
ast = quasiquote(ast[1]);
# Continue loop (TCO)
elif 'defmacro!' == a0:
func = EVAL(ast[2], env)
func._ismacro_ = True
return env.set(ast[1], func)
elif 'macroexpand' == a0:
return macroexpand(ast[1], env)
elif "do" == a0:
eval_ast(ast[1:-1], env)
ast = ast[-1]
# Continue loop (TCO)
elif "if" == a0:
a1, a2 = ast[1], ast[2]
cond = EVAL(a1, env)
if cond is None or cond is False:
if len(ast) > 3: ast = ast[3]
else: ast = None
else:
ast = a2
# Continue loop (TCO)
elif "fn*" == a0:
a1, a2 = ast[1], ast[2]
return types._function(EVAL, Env, a2, env, a1)
else:
el = eval_ast(ast, env)
f = el[0]
if hasattr(f, '__ast__'):
ast = f.__ast__
env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
示例4: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
while True:
if type(ast) == list:
if ast[0] == "def!":
val = EVAL(ast[2], env)
env.set(ast[1], val)
return val
elif ast[0] == "let*":
new_env = Env(env)
bindings = ast[1]
for i in range(0, len(bindings), 2):
val = EVAL(bindings[i+1], new_env)
new_env.set(bindings[i], val)
# return EVAL(ast[2], new_env)
ast = ast[2]
env = new_env
continue
elif ast[0] == "do":
# elements = [eval_ast(e, env) for e in ast[1:]]
# return elements[-1]
[eval_ast(e, env) for e in ast[1:-1]]
ast = ast[-1]
continue
elif ast[0] == "if":
cond = EVAL(ast[1], env)
if cond != None and cond != False:
# cond was true
ast = ast[2]
else:
if len(ast) > 3:
ast = ast[3]
else:
return None
continue
elif ast[0] == "fn*":
# def func(*params):
# new_env = Env(env, ast[1], params)
# res = EVAL(ast[2], new_env)
# return res;
return maltypes.Function(ast[2], ast[1], env)
else:
l = eval_ast(ast, env)
f = l[0]
if type(f) == maltypes.Function:
ast = f.ast
new_env = Env(f.env, f.params, l[1:])
env = new_env
else:
return f(*l[1:])
else:
res = eval_ast(ast, env)
return res
示例5: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
while True:
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
if isinstance(a0, MalSym):
a0sym = a0.value
else:
a0sym = u"__<*fn*>__"
if u"def!" == a0sym:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif u"let*" == a0sym:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
ast = a2
env = let_env # Continue loop (TCO)
elif u"quote" == a0sym:
return ast[1]
elif u"quasiquote" == a0sym:
ast = quasiquote(ast[1]) # Continue loop (TCO)
elif u"do" == a0sym:
if len(ast) == 0:
return nil
elif len(ast) > 1:
eval_ast(ast.slice2(1, len(ast)-1), env)
ast = ast[-1] # Continue loop (TCO)
elif u"if" == a0sym:
a1, a2 = ast[1], ast[2]
cond = EVAL(a1, env)
if cond is nil or cond is false:
if len(ast) > 3: ast = ast[3] # Continue loop (TCO)
else: return nil
else:
ast = a2 # Continue loop (TCO)
elif u"fn*" == a0sym:
a1, a2 = ast[1], ast[2]
return MalFunc(None, a2, env, a1, EVAL)
else:
el = eval_ast(ast, env)
f = el.values[0]
if isinstance(f, MalFunc):
if f.ast:
ast = f.ast
env = f.gen_env(el.rest()) # Continue loop (TCO)
else:
return f.apply(el.rest())
else:
raise Exception("%s is not callable" % f)
示例6: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast: MalType, env: Env) -> MalType:
while True:
if isinstance(ast, MalVector):
return MalVector(EVAL(member, env) for member in ast)
if isinstance(ast, MalHashmap):
return MalHashmap([ast[0], EVAL(ast[1], env)])
if not isinstance(ast, MalList): # not a list
return eval_ast(ast, env)
if isinstance(ast, MalList):
if len(ast) == 0: # an empty list
return ast
else: # a list
if ast[0] == 'def!':
return env.set(ast[1], EVAL(ast[2], env))
elif ast[0] == 'let*':
let_env = Env(outer=env)
param1 = iter(ast[1])
for symbol, value in zip(param1, param1):
let_env.set(symbol, EVAL(value, env=let_env))
# return EVAL(ast[2], env=let_env)
ast, env = ast[2], let_env
continue
elif ast[0] == 'do':
# value = nil
# for element in ast[1:]:
# value = EVAL(element, env)
# return value
for ele in ast[1:-1]:
eval_ast(ele, env)
ast = ast[-1]
continue
elif ast[0] == 'if':
cond = EVAL(ast[1], env)
if cond != nil and MalBool(cond):
# return EVAL(ast[2], env)
ast = ast[2]
continue
elif len(ast) == 4:
# return EVAL(ast[3], env)
ast = ast[3]
continue
else:
return nil
elif ast[0] == 'fn*':
return MalFunction(ast=ast[2], params=ast[1], env=env,
eval_fn=EVAL)
else:
f, *args = eval_ast(ast, env)
if isinstance(f, MalFunction):
env = Env(binds=f.params, exprs=args, outer=f.env)
ast = f.ast
continue
else:
return f(*args)
示例7: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(mt, env):
if type(mt) == list:
if mt[0] == "def!":
val = EVAL(mt[2], env)
env.set(mt[1], val)
return val
elif mt[0] == "let*":
new_env = Env(env)
bindings = mt[1]
for i in range(0, len(bindings), 2):
val = EVAL(bindings[i+1], new_env)
new_env.set(bindings[i], val)
return EVAL(mt[2], new_env)
elif mt[0] == "do":
elements = [eval_ast(e, env) for e in mt[1:]]
return elements[-1]
elif mt[0] == "if":
cond = EVAL(mt[1], env)
if cond != None and cond != False:
# cond was true
res = EVAL(mt[2], env)
else:
if len(mt) > 3:
res = EVAL(mt[3], env)
else:
res = maltypes.Nil()
return res
elif mt[0] == "fn*":
def func(*params):
new_env = Env(env, mt[1], params)
res = EVAL(mt[2], new_env)
return res;
return func
else:
l = eval_ast(mt, env)
func = l[0]
return func(*l[1:])
else:
res = eval_ast(mt, env)
return res
示例8: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
#print("EVAL %s" % printer._pr_str(ast))
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
if isinstance(a0, MalSym):
a0sym = a0.value
else:
a0sym = u"__<*fn*>__"
if u"def!" == a0sym:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif u"let*" == a0sym:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
return EVAL(a2, let_env)
elif u"do" == a0sym:
el = eval_ast(ast.rest(), env)
return el.values[-1]
elif u"if" == a0sym:
a1, a2 = ast[1], ast[2]
cond = EVAL(a1, env)
if cond is nil or cond is false:
if len(ast) > 3: return EVAL(ast[3], env)
else: return nil
else:
return EVAL(a2, env)
elif u"fn*" == a0sym:
a1, a2 = ast[1], ast[2]
return MalFunc(None, a2, env, a1, EVAL)
else:
el = eval_ast(ast, env)
f = el.values[0]
if isinstance(f, MalFunc):
return f.apply(el.rest())
else:
raise Exception("%s is not callable" % f)
示例9: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
while True:
#print("EVAL %s" % ast)
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
if "def!" == a0:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif "let*" == a0:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
return EVAL(a2, let_env)
elif "do" == a0:
eval_ast(ast[1:-1], env)
ast = ast[-1]
# Continue loop (TCO)
elif "if" == a0:
a1, a2 = ast[1], ast[2]
cond = EVAL(a1, env)
if cond is None or cond is False:
if len(ast) > 3: ast = ast[3]
else: ast = None
else:
ast = a2
# Continue loop (TCO)
elif "fn*" == a0:
a1, a2 = ast[1], ast[2]
return types._function(EVAL, Env, a2, env, a1)
else:
el = eval_ast(ast, env)
f = el[0]
if hasattr(f, '__ast__'):
ast = f.__ast__
env = f.__gen_env__(el[1:])
else:
return f(*el[1:])
示例10: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
if type(ast) == List and len(ast) > 0:
function = ast[0]
if function == 'fn*':
bindings = ast[1]
body = ast[2]
return Function(Env, bindings, env, body, EVAL)
elif function == 'let*':
scoped_env = Env(env)
bindings = ast[1]
for i in range(0, len(bindings), 2):
symbol = Symbol(bindings[i])
value = EVAL(bindings[i+1], scoped_env)
scoped_env.set(symbol, value)
expression = ast[-1]
return EVAL(expression, scoped_env)
elif function == 'def!':
symbol = Symbol(ast[1])
value = EVAL(ast[2], env)
env.set(symbol, value)
return value
elif function == 'do':
return_val = None
for exp in ast[1:]:
return_val = EVAL(exp, env)
return return_val
elif function == 'if':
condition = EVAL(ast[1], env)
if_branch = ast[2]
if condition is not False and condition is not None:
return EVAL(if_branch, env)
else:
else_branch = None
try:
else_branch = ast[3]
except IndexError as e:
pass
return EVAL(else_branch, env)
else:
evaluated = eval_ast(ast, env)
return evaluated[0](*evaluated[1:])
evaluated = eval_ast(ast, env)
return evaluated
示例11: repl
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def repl():
# set up base environment
env = Env(outer=None)
for k, v in ns.items():
env.set(k, v)
env.set(SYMBOL_EVAL, lambda ast: EVAL(ast, env=env)) # `eval` added to ns
s = """
(def! load-file
(fn* (f)
(eval (read-string (str "(do " (slurp f) ")")))))
"""
EVAL(READ(s), env=env)
# read-eval-print loop
while True:
try:
s = input("=> ")
rep(s, env)
except Exception as e:
print("error: {0}".format(e))
示例12: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(mt, env):
if type(mt) == list:
if mt[0] == "def!":
val = EVAL(mt[2], env)
env.set(mt[1], val)
return val
elif mt[0] == "let*":
new_env = Env(env)
bindings = mt[1]
for i in range(0, len(bindings), 2):
val = EVAL(bindings[i+1], new_env)
new_env.set(bindings[i], val)
return EVAL(mt[2], new_env)
else:
l = eval_ast(mt, env)
f = l[0]
return f(*l[1:])
else:
return eval_ast(mt, env)
示例13: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
if type(ast) == List and len(ast) > 0:
function = ast[0]
if function == 'let*':
scoped_env = Env(env)
bindings = ast[1]
for i in range(0, len(bindings), 2):
symbol = Symbol(bindings[i])
value = EVAL(bindings[i+1], scoped_env)
scoped_env.set(symbol, value)
expression = ast[-1]
return EVAL(expression, scoped_env)
elif function == 'def!':
symbol = Symbol(ast[1])
value = EVAL(ast[2], env)
env.set(symbol, value)
return value
else:
evaluated = eval_ast(ast, env)
return evaluated[0](*evaluated[1:])
evaluated = eval_ast(ast, env)
return evaluated
示例14: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast: MalType, env: Env) -> MalType:
if isinstance(ast, MalVector):
return MalVector(EVAL(member, env) for member in ast)
if isinstance(ast, MalHashmap):
return MalHashmap([ast[0], EVAL(ast[1], env)])
if not isinstance(ast, MalList): # not a list
return eval_ast(ast, env)
if isinstance(ast, MalList):
if len(ast) == 0: # an empty list
return ast
else: # a list
if ast[0] == 'def!':
return env.set(ast[1], EVAL(ast[2], env))
elif ast[0] == 'let*':
let_env = Env(outer=env)
param1 = iter(ast[1])
for symbol, value in zip(param1, param1):
let_env.set(symbol, EVAL(value, env=let_env))
return EVAL(ast[2], env=let_env)
else:
f, *args = eval_ast(ast, env)
return f(*args)
示例15: EVAL
# 需要导入模块: from env import Env [as 别名]
# 或者: from env.Env import set [as 别名]
def EVAL(ast, env):
#print("EVAL %s" % ast)
if not types._list_Q(ast):
return eval_ast(ast, env)
# apply list
if len(ast) == 0: return ast
a0 = ast[0]
if "def!" == a0:
a1, a2 = ast[1], ast[2]
res = EVAL(a2, env)
return env.set(a1, res)
elif "let*" == a0:
a1, a2 = ast[1], ast[2]
let_env = Env(env)
for i in range(0, len(a1), 2):
let_env.set(a1[i], EVAL(a1[i+1], let_env))
return EVAL(a2, let_env)
elif "do" == a0:
el = eval_ast(ast[1:], env)
return el[-1]
elif "if" == a0:
a1, a2 = ast[1], ast[2]
cond = EVAL(a1, env)
if cond is None or cond is False:
if len(ast) > 3: return EVAL(ast[3], env)
else: return None
else:
return EVAL(a2, env)
elif "fn*" == a0:
a1, a2 = ast[1], ast[2]
return types._function(EVAL, Env, a2, env, a1)
else:
el = eval_ast(ast, env)
f = el[0]
return f(*el[1:])