Beispiel #1
0
def assoc(args):
    src_hm, key_vals = args[0], args.rest()
    new_dct = src_hm.dct.copy()
    for i in range(0,len(key_vals),2):
        k = key_vals[i]
        if not isinstance(k, MalStr):
            throw_str("assoc called with non-string/non-keyword key")
        new_dct[k.value] = key_vals[i+1]
    return MalHashMap(new_dct)
Beispiel #2
0
def dissoc(args):
    src_hm, keys = args[0], args.rest()
    new_dct = src_hm.dct.copy()
    for k in keys.values:
        if not isinstance(k, MalStr):
            throw_str("dissoc called with non-string/non-keyword key")
        if k.value in new_dct:
            del new_dct[k.value]
    return MalHashMap(new_dct)
Beispiel #3
0
def eval_ast(ast, env):
    if types._symbol_Q(ast):
        assert isinstance(ast, MalSym)
        return env.get(ast)
    elif types._list_Q(ast):
        res = []
        for a in ast.values:
            res.append(EVAL(a, env))
        return MalList(res)
    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)
    else:
        return ast  # primitive value, return unchanged
Beispiel #4
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)