def eval(x, env): "Evaluate an expression in an environment." while True: if isa(x, Symbol): # variable reference return env[x] elif isa(x, list): # (proc exp*) proc = eval(x[0], env) if hasattr(proc, '__call__'): val = proc(env,*x[1:]) if isa(val, Tail): x = val.expr env = val.env else: return val elif isa(proc, bool): #sugar for boolean branches x = x[1] if proc else x[2] else: raise ValueError("%s = %s is not a procedure" % (to_string(x[0]),to_string(proc))) else: return x
def repl(prompt='vernal> '): try: while True: full_line = raw_input(prompt) rps = full_line.count("(")-full_line.count(")") while rps != 0 or full_line == "": line = raw_input(">\t") full_line += line rps += line.count("(")-line.count(")") try: tokens = tokenize(full_line) while len(tokens) > 0: val = eval(parse(tokens),global_env) if val is not None: print to_string(val) except ValueError as e: print e.message except Exception as e: raise e except (KeyboardInterrupt, SystemExit): pass except: print "\nFatal Error\n" traceback.print_exc()
def vprint(v,e): val = eval(e, v) print to_string(val) return val