def load(filename): print "Loading and executing %s" % filename rps = 0 full_line = "" line_num = 1 for line in open(filename, "r"): line = line.strip() full_line += line rps += line.count("(")-line.count(")") if rps == 0 and full_line.strip() != "": try: tokens = tokenize(full_line) while len(tokens) > 0: val = eval(parse(tokens),global_env) except SystemExit: exit() except: print "\nAn error occurred on line %d:\n\t%s\n" % (line_num,full_line) traceback.print_exc() break full_line = "" line_num += 1
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 begin(v,*x): val = 0 for e in x[:-1]: val = eval(e, v) return Tail(x[-1], v)
def vprint(v,e): val = eval(e, v) print to_string(val) return val
def cond(v,*x): for (p, e) in x: if eval(p, v): return Tail(e, v) raise ValueError("No Branch Evaluates to True")
def setvar(v,var,e): val = eval(e, v) env.find(var)[var] = val return val
def defvar(v,var,e): val = eval(e, v) v[var] = val return val
def closure(call_env, *args): new_env = Env(zip(vars,[eval(exp, call_env) for exp in args]), clos_env) new_env['%'] = call_env return Tail(body, new_env)
return Tail(e, v) raise ValueError("No Branch Evaluates to True") def begin(v,*x): val = 0 for e in x[:-1]: val = eval(e, v) return Tail(x[-1], v) def vprint(v,e): val = eval(e, v) print to_string(val) return val global_env = Env({ '+': lambda v,x,y:eval(x,v)+eval(y,v), '-': lambda v,x,y:eval(x,v)-eval(y,v), '*': lambda v,x,y:eval(x,v)*eval(y,v), '/': lambda v,x,y:eval(x,v)/eval(y,v), '>': lambda v,x,y:eval(x,v)>eval(y,v), '<': lambda v,x,y:eval(x,v)<eval(y,v), '>=': lambda v,x,y:eval(x,v)>=eval(y,v), '<=': lambda v,x,y:eval(x,v)<=eval(y,v), '=': lambda v,x,y:eval(x,v)==eval(y,v), 'eq?': lambda v,x,y: (lambda vx,vy: (not isa(vx, list)) and (vx == vy))(eval(x,v),eval(y,v)), 'cons': lambda v,x,y:[eval(x,v)]+eval(y,v), 'car': lambda v,x:eval(x,v)[0], 'cdr': lambda v,x:eval(x,v)[1:], 'list': lambda v,*x:[eval(expr, v) for exp in x], 'append': lambda v,x,y:eval(x,v)+eval(y,v),