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
def nth(lst, n): if isinstance(n, mal_types.MalNumber): n = n.data try: return lst[n] except IndexError: raise mal_types.MalException("nth: index out of range")
def read_list(reader, starting_token): result = _list_type_mapping[starting_token]() reader.next() while True: token = read_form(reader) if token in _list_ending_token and token != _list_token_mapping[ starting_token]: raise mal_types.MalException("expected '{}', got {}".format( _list_token_mapping[starting_token], token)) if token is None: raise mal_types.MalException( "expected 'EOF', got {}".format(token)) if token in _list_ending_token: break result.append(token) reader.next() return result
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
def throw(args): raise types.MalException(args[0])
def throw(x): raise mal_types.MalException(x)