def my_if(env, args): cur_env = Env(env.name + '_0', env) cond = doeval(args[0], cur_env) cur_env = Env(env.name + '_1', env) if cond == true: rs = doeval(args[1], cur_env) else : rs = doeval(args[2], cur_env) return rs
def my_if(env, args): cur_env = Env(env.name + '_0', env) cond = doeval(args[0], cur_env) cur_env = Env(env.name + '_1', env) if cond == true: rs = doeval(args[1], cur_env) else: rs = doeval(args[2], cur_env) return rs
def dvd(env, args): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) rs = doeval(args[0], cur_env) idx += 1 for item in args[1:]: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env).get_value() rs /= tmp idx += 1 return rs
def dvd(env, args): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) rs = doeval(args[0],cur_env) idx += 1 for item in args[1:]: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item,cur_env).get_value() rs /= tmp idx += 1 return rs
def compare(env, args, func): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) pre = doeval(args[0],cur_env) idx += 1 while idx < len(args): item = args[idx] cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item,cur_env) if not func(float(pre.get_value()), float(tmp.get_value())): return false pre = tmp idx += 1 return true
def compare(env, args, func): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) pre = doeval(args[0], cur_env) idx += 1 while idx < len(args): item = args[idx] cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env) if not func(float(pre.get_value()), float(tmp.get_value())): return false pre = tmp idx += 1 return true
def my_not(env, args): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) rs = doeval(args[0],cur_env) #是否正确? if rs.get_value(): return false return true
def my_not(env, args): idx = 0 cur_env = Env(env.name + '_' + str(idx), env) rs = doeval(args[0], cur_env) #是否正确? if rs.get_value(): return false return true
def my_or(env, args): idx = 0 for item in args: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item,cur_env) if tmp.get_value() :return true idx += 1 return false
def my_or(env, args): idx = 0 for item in args: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env) if tmp.get_value(): return true idx += 1 return false
def my_list(env, args): if len(args) == 0: return nil # print args rs_args = [] for idx, item in enumerate(args): cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env) rs_args.append(tmp) return My_List(rs_args)
def my_list(env, args): if len(args) == 0: return nil # print args rs_args = [] for idx,item in enumerate(args): cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env) rs_args.append(tmp) return My_List(rs_args)
def add(env, args): rs = 0 idx = 0 for item in args: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item,cur_env) rs += tmp.get_value() idx += 1 return rs
def add(env, args): rs = 0 idx = 0 for item in args: cur_env = Env(env.name + '_' + str(idx), env) tmp = doeval(item, cur_env) rs += tmp.get_value() idx += 1 return rs
def apply(self, env, args): if len(args) > len (self.args): raise Exception('too many args!') cur_env = Env(env.name + '_0', self.env) for symbol, target in zip(self.args, args): define(cur_env, [symbol, target]) if len(args) < len(self.args): new_args = copy.copy(self.args[len(args):]) new_func = Func(new_args, self.body, cur_env) return new_func rs = nil for tmp_body in self.body: rs = doeval(tmp_body, cur_env) return rs
def apply(self, env, args): if len(args) > len(self.args): raise Exception('too many args!') cur_env = Env(env.name + '_0', self.env) for symbol, target in zip(self.args, args): define(cur_env, [symbol, target]) if len(args) < len(self.args): new_args = copy.copy(self.args[len(args):]) new_func = Func(new_args, self.body, cur_env) return new_func rs = nil for tmp_body in self.body: rs = doeval(tmp_body, cur_env) return rs
def define(env, args): symbol = args[0] cur_env = Env(env.name + '_0', env) target = doeval(args[1], cur_env) env.add_symbol(symbol, target)
def is_null(env, args): cur_env = Env(env.name + '_0', env) rs = doeval(args[0], cur_env) if rs == nil: return true return false
def cdr(env, args): cur_env = Env(env.name + '_0', env) pair = doeval(args[0],cur_env) if not isinstance(pair, Pair): raise Exception('cdr should be used to a pair!') return pair.cdr()
def cdr(env, args): cur_env = Env(env.name + '_0', env) pair = doeval(args[0], cur_env) if not isinstance(pair, Pair): raise Exception('cdr should be used to a pair!') return pair.cdr()
def cons(env, args): cur_env = Env(env.name + '_0', env) first = doeval(args[0],cur_env) cur_env = Env(env.name + '_1', env) second = doeval(args[1],cur_env) return Pair(first, second)
def cons(env, args): cur_env = Env(env.name + '_0', env) first = doeval(args[0], cur_env) cur_env = Env(env.name + '_1', env) second = doeval(args[1], cur_env) return Pair(first, second)