Closed agentultra closed 10 years ago
(import ast hy.compiler)
(defn disassemble [tree]
(defn fix [tree]
(if (isinstance tree list)
(for [x tree]
(fix x)))
(setv tree.start_line 1)
(setv tree.start_column 1)
(setv tree.end_line 1)
(setv tree.end_column 1))
(fix tree)
(ast.dump (hy.compiler.hy_compile tree)))
(print (disassemble (quote (print ((fn [] (if x y z)))))))
Yields
Module(body=[FunctionDef(name='_hy_anon_fn_1', args=arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=[Return(value=IfExp(test=Name(id='x', ctx=Load()), body=Name(id='y', ctx=Load()), orelse=Name(id='z', ctx=Load())))], decorator_list=[]), Print(dest=None, values=[Call(func=Name(id='_hy_anon_fn_1', ctx=Load()), args=[], keywords=[], starargs=None, kwargs=None)], nl=True)])
On my refactor/compiler branch. That works on master too, but that particular construct yields a buggy ast AFAICT.
So that could be added to the (not yet existing) stdlib?
I think DISASSEMBLE returns None (or NIL in CL parlance) and prints its output to the stdout stream. +1 for pretty-printing as well.
nice
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_disassemble.html
A function that returns the implementation-specific representation of a Hy code object to stdout... so that you can see the generated Python AST from the REPL or a script.