def read_atom(reader): token = reader.next() if token == 'nil': return NIL elif token == 'true': return TRUE elif token == 'false': return FALSE elif re.match(r'-?\d+\.*\d*', token): return make_number(token) elif token.startswith('"'): if not valid_string(token): raise MalException('EOF') return make_string(token) elif token.startswith(':'): return make_keyword(token) elif re.match(r'.*', token): return make_symbol(token) raise MalException('Input/output error')
def read_list(tokens: Reader): map = {"(": ")", "{": "}", "[": "]"} out = list() tokens.next() while (tokens.peek() != ")" and tokens.peek() is not None): out.append(read_form(tokens)) tokens.next() if tokens.peek() is None: raise MalException("missing closing )") return out
def eval_ast(ast, env): if isinstance(ast, Symbol): out = env.get(str(ast)) if out is None: raise MalException("symbol not found") else: return out if isinstance(ast, list): if len(ast) == 0: return [] else: out = list() for x in ast: out.append(EVAL(x, env)) return out return ast
def read_atom(tokens: Reader): token = tokens.peek() int_re = re.compile(r"-?[0-9]+$") float_re = re.compile(r"-?[0-9][0-9.]*$") if token[0] == "\"": if token[-1] == "\"": return String(token[1:-1]) else: raise MalException("missing closing \"") if re.match(int_re, token): return Integer(int(token)) if re.match(float_re, token): return float(token) if token == "true": return Bolean(True) if token == "false": return Bolean(False) if token == "nill": return Nill() return Symbol(token)
def _check_position(self): if self._position >= len(self._tokens): raise MalException('EOF')
def throw(obj): raise MalException(obj) # String functions def pr_str(*args):
def _throw(val): raise MalException(val)
def throw(exc): raise MalException(exc)
def throw(exception: MalExpression) -> MalExpression: raise MalException(exception)
def nth(a, b): try: return a[b] except IndexError: raise MalException("range")
def throw(x): raise MalException(x)
def throw(obj): raise MalException(obj)
def find(self, key): if key in self.data: return self if self.outer is not None: return self.outer.find(key) raise MalException(MalString("'{}' not found".format(key)))