def run_command(source, filename=None): __main__ = importlib.import_module('__main__') require("hy.cmdline", __main__, assignments="ALL") try: tree = hy_parse(source, filename=filename) except HyLanguageError: hy_exc_handler(*sys.exc_info()) return 1 with filtered_hy_exceptions(): hy_eval(tree, None, __main__, filename=filename, source=source) return 0
def runsource(self, source, filename='<input>', symbol='single'): global SIMPLE_TRACEBACKS def error_handler(e, use_simple_traceback=False): self.locals[mangle("*e")] = e if use_simple_traceback: print(e, file=sys.stderr) else: self.showtraceback() try: try: do = hy_parse(source) except PrematureEndOfInput: return True except LexException as e: if e.source is None: e.source = source e.filename = filename error_handler(e, use_simple_traceback=True) return False try: def ast_callback(main_ast, expr_ast): if self.spy: # Mush the two AST chunks into a single module for # conversion into Python. new_ast = ast.Module(main_ast.body + [ast.Expr(expr_ast.body)]) print(astor.to_source(new_ast)) value = hy_eval(do, self.locals, ast_callback=ast_callback, compiler=self.hy_compiler) except HyTypeError as e: if e.source is None: e.source = source e.filename = filename error_handler(e, use_simple_traceback=SIMPLE_TRACEBACKS) return False except Exception as e: error_handler(e) return False if value is not None: # Shift exisitng REPL results next_result = value for sym in self._repl_results_symbols: self.locals[sym], next_result = next_result, self.locals[sym] # Print the value. try: output = self.output_fn(value) except Exception as e: error_handler(e) return False print(output) return False
def can_eval(expr): return hy_eval(hy_parse(expr))
def eval_str(s): return hy_eval(hy.read_str(s), filename='<string>', source=s)