Example #1
0
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
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
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
Example #6
0
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
Example #7
0
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
Example #8
0
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
Example #9
0
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
Example #10
0
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
Example #11
0
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)
Example #12
0
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)
Example #13
0
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
Example #14
0
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
Example #15
0
 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
Example #16
0
 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
Example #17
0
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)
Example #18
0
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
Example #19
0
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
Example #20
0
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()
Example #21
0
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()
Example #22
0
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)
Example #23
0
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)
Example #24
0
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)