本文整理汇总了Python中env.Env类的典型用法代码示例。如果您正苦于以下问题:Python Env类的具体用法?Python Env怎么用?Python Env使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Env类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checkJudge
def checkJudge(self, name):
pTree = self.world.judgeTrees[name][0]
judge = JudgeDef(pTree)
judge.name = pTree.val
judge.envs = map(lambda x: self.strToSym(x, pTree.syntax), pTree.syntax.envs)
judge.args = map(lambda x: self.strToSym(x, pTree.syntax), pTree.syntax.children)
freshGen = Env()
judge.envLabels = map(lambda x: freshGen.freshOld(x), judge.envs)
judge.argLabels = map(lambda x: freshGen.freshOld(x), judge.args)
self.world.judges[pTree.val] = judge
for i in range(len(pTree.children)):
self.checkJCase(pTree.children[i], judge, i)
curIndex = len(pTree.children)
#add any other asts which hold parts of the judgement
for oTree in self.world.judgeTrees[name][1:]:
#check the shapes match (ignore any latex or label)
envs = map(lambda x: self.strToSym(x, oTree.syntax), oTree.syntax.envs)
args = map(lambda x: self.strToSym(x, oTree.syntax), oTree.syntax.children)
check = reduce(lambda x,y: x and y, map(lambda (x,y): x == y, zip(args, judge.args)), True)
check = check and reduce(lambda x,y: x and y, map(lambda (x,y): x == y, zip(envs, judge.envs)), True)
if not check:
expectStr = "; ".join(map(lambda x: x.name, judge.envs)) + " |- " + "; ".join(map(lambda x: x.name, judge.args))
foundStr = "; ".join(map(lambda x: x.name, envs)) + " |- " + "; ".join(map(lambda x: x.name, args))
self.addErr("Shape of repeated judgment does not match original judgement: found: " + foundStr + ", expected: " + expectStr, oTree)
#add any cases
for i in range(len(oTree.children)):
self.checkJCase(oTree.children[i], judge, curIndex + i)
curIndex += len(oTree.children)
示例2: EVAL
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:])
示例3: Function
class Function(Exp):
def __init__(self, params, cmd):
self.params = params
self.cmd = cmd
self.this = None
self.env = None
def eval(self, env):
if self.env is None:
self.env = Env(env)
self.env.declare('this', self.this)
return self
def set_this(self, this):
self.this = this
def call(self, args, env):
if len(args) != len(self.params):
raise Exception("Invalid count of parameters. Should be %s, is %s." % (len(self.params), len(args)))
new_env = Env(self.env)
values = zip(self.params, args)
for val in values:
new_env.declare(val[0], val[1])
return self.cmd.eval(new_env)
def __str__(self):
return "Function(%s, %s)" % (self.params, self.cmd)
示例4: EVAL
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)
示例5: Field
class Field(ScatterPlane):
'''
This is the Field which will contain cells.
'''
agent_widget = ObjectProperty(None)
total_reward = NumericProperty(0)
def __init__(self, cell_size=25, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cell_size = cell_size
# At the __init__ height and width, and consecutively center may be not
# established, yet due to layout logic.
Clock.schedule_once(self._init_after)
Clock.schedule_interval(self.update, 0.1)
def _init_after(self, dt):
''' Perform initializations after the layout is finalized. '''
self.env = Env()
# TODO: Move params to config file
with open('sarsa.pickle', 'rb') as fd:
self.sarsa = pickle.load(fd)
self.grid = Grid(self.canvas, 'line_loop', Color(), self.cell_size,
self.to_local(*self.center))
self.state = self.env.reset(self.grid)
self._place_agent(self.state.cell)
def _place_agent(self, cell):
self.agent_widget.center = self.grid.pixcenter(cell.q, cell.r)
# FIXME
for _ in self.grid.neighbors(cell.q, cell.r):
pass
def on_touch_down(self, touch):
super().on_touch_down(touch)
x, y = self.to_local(touch.x, touch.y)
q, r = self.grid.pixel_to_hex(x, y)
if (q, r) in self.grid:
print("Touched ({}, {}) in {}.".format(q, r, (x, y)))
print("env tvisited", self.env.tvisited[q, r])
print("state food", self.state.food)
else:
self.grid.init(q, r)
for _ in self.grid.neighbors(q, r):
pass
return True
# TODO: Shouldn't this feel better in SwarmApp?
def update(self, dt):
action = self.sarsa.policy(self.state, explore=False)
next_state, reward, done = self.env.step(action)
self.sarsa.adapt_policy(self.state, action, next_state, reward)
self.state = next_state
self.total_reward += int(reward)
self._place_agent(self.state.cell)
示例6: entry_point
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
示例7: call
def call(self, args, env):
if len(args) != len(self.params):
raise Exception("Invalid count of parameters. Should be %s, is %s." % (len(self.params), len(args)))
new_env = Env(self.env)
values = zip(self.params, args)
for val in values:
new_env.declare(val[0], val[1])
return self.cmd.eval(new_env)
示例8: EVAL
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
示例9: EVAL
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)
示例10: EVAL
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)
示例11: eval
def eval(self, env):
new_env = Env(env)
# put object in heap
addr = heap.alloc()
heap[addr] = self
for decl in self.decls:
decl.exp.set_this(addr)
decl.eval(new_env)
for key in new_env:
if new_env.directly_defined(key):
self.env[key] = new_env[key]
return addr
示例12: interpret
def interpret(code, print_ast=False):
ast = parse(tokenize(code))
if print_ast:
print(ast)
env = Env()
env.declare("alloc", Alloc())
env.declare("readline", ReadLine())
env.declare("true", 1)
env.declare("false", 0)
ast.eval(env)
示例13: eval_ast
def eval_ast(ast: MalType, env: Env):
if isinstance(ast, MalSymbol):
return env.get(ast)
elif isinstance(ast, MalList):
return MalList(EVAL(child, env) for child in ast)
else:
return ast
示例14: EVAL
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
示例15: EVAL
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)