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
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)
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)) repl_env.set(types._symbol(u'eval'), MalEval(None, env=repl_env, EvalFunc=EVAL)) mal_args = [] if len(argv) >= 3: for a in argv[2:]: mal_args.append(MalStr(unicode(a))) repl_env.set(_symbol(u'*ARGV*'), MalList(mal_args)) # core.mal: defined using the language itself REP("(def! *host-language* \"rpython\")", repl_env) REP("(def! not (fn* (a) (if a false true)))", repl_env) REP( "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env) REP( "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env) REP("(def! inc (fn* [x] (+ x 1)))", repl_env) REP( "(def! gensym (let* [counter (atom 0)] (fn* [] (symbol (str \"G__\" (swap! counter inc))))))", repl_env) REP( "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))", repl_env) if len(argv) >= 2: REP('(load-file "' + argv[1] + '")', repl_env) return 0 REP("(println (str \"Mal [\" *host-language* \"]\"))", 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) if IS_RPYTHON: llop.debug_print_traceback(lltype.Void) else: print("".join(traceback.format_exception(*sys.exc_info()))) return 0
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)) repl_env.set(types._symbol(u'eval'), MalEval(None, env=repl_env, EvalFunc=EVAL)) mal_args = [] if len(argv) >= 3: for a in argv[2:]: mal_args.append(MalStr(unicode(a))) repl_env.set(_symbol(u'*ARGV*'), MalList(mal_args)) # core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))", repl_env) REP( "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))", repl_env) REP( "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))", repl_env) if len(argv) >= 2: REP('(load-file "' + argv[1] + '")', repl_env) return 0 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
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)
def EVAL(ast, env): while True: #print("EVAL %s" % printer._pr_str(ast)) if not types._list_Q(ast): return eval_ast(ast, env) if len(ast) == 0: return ast # 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 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"defmacro!" == a0sym: func = EVAL(ast[2], env) func.ismacro = True return env.set(ast[1], func) elif u"macroexpand" == a0sym: return macroexpand(ast[1], env) elif u"try*" == a0sym: if len(ast) < 3: return EVAL(ast[1], env) a1, a2 = ast[1], ast[2] a20 = a2[0] if isinstance(a20, MalSym): if a20.value == u"catch*": try: return EVAL(a1, env) except types.MalException as exc: exc = exc.object catch_env = Env(env, _list(a2[1]), _list(exc)) return EVAL(a2[2], catch_env) except Exception as exc: exc = MalStr(unicode("%s" % exc)) catch_env = Env(env, _list(a2[1]), _list(exc)) return EVAL(a2[2], catch_env) return EVAL(a1, env) 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)
def multiply(args): a, b = args[0], args[1] assert isinstance(a, MalInt) assert isinstance(b, MalInt) return MalInt(a.value * b.value) def divide(args): a, b = args[0], args[1] assert isinstance(a, MalInt) assert isinstance(b, MalInt) return MalInt(int(a.value / b.value)) repl_env.set(_symbol(u'+'), MalFunc(plus)) repl_env.set(_symbol(u'-'), MalFunc(minus)) repl_env.set(_symbol(u'*'), MalFunc(multiply)) repl_env.set(_symbol(u'/'), MalFunc(divide)) def entry_point(argv): 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
def multiply(args): a, b = args[0], args[1] assert isinstance(a, MalInt) assert isinstance(b, MalInt) return MalInt(a.value * b.value) def divide(args): a, b = args[0], args[1] assert isinstance(a, MalInt) assert isinstance(b, MalInt) return MalInt(int(a.value / b.value)) repl_env[u'+'] = MalFunc(plus) repl_env[u'-'] = MalFunc(minus) repl_env[u'*'] = MalFunc(multiply) repl_env[u'/'] = MalFunc(divide) def entry_point(argv): 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
def EVAL(ast, env): while True: #print("EVAL %s" % printer._pr_str(ast)) if types._symbol_Q(ast): assert isinstance(ast, MalSym) return env.get(ast) elif types._vector_Q(ast): res = [] for a in ast.values: res.append(EVAL(a, env)) return MalVector(res) elif types._hash_map_Q(ast): new_dct = {} for k in ast.dct.keys(): new_dct[k] = EVAL(ast.dct[k], env) return MalHashMap(new_dct) elif not types._list_Q(ast): return ast # primitive value, return unchanged else: # 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"defmacro!" == a0sym: func = EVAL(ast[2], env) func.ismacro = True return env.set(ast[1], func) elif u"try*" == a0sym: if len(ast) < 3: return EVAL(ast[1], env) a1, a2 = ast[1], ast[2] a20 = a2[0] if isinstance(a20, MalSym): if a20.value == u"catch*": try: return EVAL(a1, env) except types.MalException as exc: exc = exc.object catch_env = Env(env, _list(a2[1]), _list(exc)) return EVAL(a2[2], catch_env) except Exception as exc: exc = MalStr(unicode("%s" % exc)) catch_env = Env(env, _list(a2[1]), _list(exc)) return EVAL(a2[2], catch_env) return EVAL(a1, env) elif u"do" == a0sym: if len(ast) == 0: return nil for i in range(1, len(ast) - 1): EVAL(ast[i], 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: f = EVAL(a0, env) args = ast.rest() if f.ismacro: ast = f.apply(ast.rest()) # Continue loop (TCO) else: res = [] for a in args.values: res.append(EVAL(a, env)) el = MalList(res) if isinstance(f, MalFunc): if f.ast: ast = f.ast env = f.gen_env(el) # Continue loop (TCO) else: return f.apply(el) else: raise Exception("%s is not callable" % f)