0

I have several Hy Lang code snippets that I use in my python project. I was wondering if there is a relatively easy way for me to get an Abstract Syntax Tree of the Hy code with some python library or maybe even the Hy compiler itself (I've looked into the compiler code but I'm having a hard time understanding it).

I know that Hy transforms all the Hy lang code to a python AST before making that bytecode and looking at the compiler I do not see a function that would help me convert the Hy code to a Hy object that I can feed to HyASTCompiler for example.

I'm hoping I can leverage something already built and not have to use something like ANTLER and write my own grammar.

Thanks!

Using Hy Version 0.12 and python3

Kenny
  • 33
  • 4

1 Answers1

1

I'm not clear if you want Python ast objects or just Hy code as data. The latter is provided by read and read-str. For the former, no public interface is currently provided, but take a look at the docstring of the internal function hy.compiler.hy_compile.

Hy 0.12 is ancient, though, so you'll probably need to upgrade.

Kodiologist
  • 2,984
  • 18
  • 33
  • I would prefer python AST objects (as I need to "ask questions" about it). I'm looking at 'hy_compile' right now but that takes in a Hy AST object to compile. I don't even have a Hy AST yet, I just have the Hy code that I want to convert to a Hy AST. I think maybe 'hy_parse' would do that, however I don't think that is there (or works the same in version 0.12) I'm currently looking at the most recent version of the Hy source code. – Kenny Oct 23 '20 at 16:37
  • @Kenny A "Hy AST" object is just what the manual calls Hy models. Use `read` or `read-str` to make one out of a string. – Kodiologist Oct 23 '20 at 16:38
  • I see, thank you for that info. I will try that If I need more information. For now I also found the hy2py function (lol, I actually see some of your posts there too @kodiologist) and I think that can give me everything I need if used with the "--with-ast" flag. – Kenny Oct 23 '20 at 16:52