Beispiel #1
0
def pr_str(args):
    parts = []
    for exp in args.values:
        parts.append(printer._pr_str(exp, True))
    return MalStr(u" ".join(parts))
Beispiel #2
0
def keys(args):
    hm = args[0]
    keys = []
    for k in hm.dct.keys():
        keys.append(MalStr(k))
    return MalList(keys)
Beispiel #3
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)
        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"quasiquoteexpand" == a0sym:
            return quasiquote(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)
Beispiel #4
0
def slurp(args):
    a0 = args[0]
    if not isinstance(a0, MalStr):
        throw_str("slurp with non-string filename")
    return MalStr(unicode(open(str(a0.value)).read()))
Beispiel #5
0
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)