示例#1
0
def hy2py_main():
    options = dict(prog="hy2py",
                   usage="%(prog)s [options] [FILE]",
                   formatter_class=argparse.RawDescriptionHelpFormatter)
    parser = argparse.ArgumentParser(**options)
    parser.add_argument("FILE",
                        type=str,
                        nargs='?',
                        help="Input Hy code (use STDIN if \"-\" or "
                        "not provided)")
    parser.add_argument("--with-source",
                        "-s",
                        action="store_true",
                        help="Show the parsed source structure")
    parser.add_argument("--with-ast",
                        "-a",
                        action="store_true",
                        help="Show the generated AST")
    parser.add_argument("--without-python",
                        "-np",
                        action="store_true",
                        help=("Do not show the Python code generated "
                              "from the AST"))

    options = parser.parse_args(sys.argv[1:])

    if options.FILE is None or options.FILE == '-':
        sys.path.insert(0, "")
        filename = '<stdin>'
        source = sys.stdin.read()
    else:
        filename = options.FILE
        set_path(filename)
        with io.open(options.FILE, 'r', encoding='utf-8') as source_file:
            source = source_file.read()

    with filtered_hy_exceptions():
        hst = hy_parse(source, filename=filename)

    if options.with_source:
        _print_for_windows(hst)
        print()
        print()

    with filtered_hy_exceptions():
        _ast = hy_compile(hst, '__main__', filename=filename, source=source)

    if options.with_ast:
        _print_for_windows(ast.dump(_ast))
        print()
        print()

    if not options.without_python:
        _print_for_windows(ast_unparse(_ast))

    parser.exit(0)
示例#2
0
文件: cmdline.py 项目: etanol/hy
 def ast_callback(self, exec_ast, eval_ast):
     if self.spy:
         try:
             # Mush the two AST chunks into a single module for
             # conversion into Python.
             new_ast = ast.Module(
                 exec_ast.body + ([] if eval_ast is None else [ast.Expr(eval_ast.body)]),
                 type_ignores=[])
             print(ast_unparse(new_ast))
         except Exception:
             msg = 'Exception in AST callback:\n{}\n'.format(
                 traceback.format_exc())
             self.write(msg)