Esempio n. 1
0
def read_hash_map(reader):
    result = mal_types.MalHashMap()
    reader.next()
    while True:
        token = read_form(reader)
        if token in _list_ending_token:
            raise mal_types.MalException("expected '}', got {}".format(token))
        if token is None:
            raise mal_types.MalException(
                "expected 'EOF', got {}".format(token))
        if token == '}':
            break
        key = token
        reader.next()
        token = read_form(reader)
        if token in _list_ending_token:
            raise mal_types.MalException("expected '}', got {}".format(token))
        if token is None:
            raise mal_types.MalException(
                "expected 'EOF', got {}".format(token))
        if token == '}':
            break
        value = token
        reader.next()
        result[key] = value
    return result
Esempio n. 2
0
def eval_ast(ast, env):
    if isinstance(ast, mal_types.MalSymbol):
        v = env.get(ast)
        if v is None:
            raise mal_types.MalException("'{}' not found.".format(ast.data))
        return v
    elif isinstance(ast, mal_types.list_types):
        class_type = ast.__class__
        return class_type([EVAL(i, env) for i in ast])
    elif isinstance(ast, mal_types.MalHashMap):
        return mal_types.MalHashMap({EVAL(k, env): EVAL(v, env) for k, v in ast.items()})
    return ast
Esempio n. 3
0
def dissoc(x, *args):
    rv = mal_types.MalHashMap(x)
    for key in args:
        del [x[key]]
    return rv
Esempio n. 4
0
def assoc(x, *args):
    rv = mal_types.MalHashMap(x)
    for m, n in zip(args[::2], args[1::2]):
        rv[m] = n
    return rv
Esempio n. 5
0
def hash_map(*args):
    rv = mal_types.MalHashMap()
    for m, n in zip(args[::2], args[1::2]):
        rv[m] = n
    return rv