Hi there! I was looking into building a generic code generation tool, that would take a GraphQL schema in and would build code in Python, JavaScript, etc.<p>I've built something like this using a template engine like Jinja, but I'm not sure if that's the best approach, part of me wants to use AST for the languages and a printer to get the AST to code, but I don't, I'm open for suggestions<p>The idea is to also make it easy to built your own codegen output using plugins
You would think there would be parsing frameworks that would work like<p><pre><code> Language -> AST -> Language
</code></pre>
like maybe you could compile Java to an AST, modify the AST, then write the Java out. I have a list of "things that current parser generators don't do that are keeping us from writing interesting applications" and that's one of them. (There is also a space for tools that work with concrete syntax trees, particularly there were some high end CASE tools in the 1990s that were meant to let you edit a program by editing something like a UML document and make a patch like a professional programmer would make that would leave comments, spacing and all of that intact.)<p>The universal excuse that is brought out is that a bidirectional parser would be slow compared to a conventional parser. I wrote a simple site generator that made this page<p><a href="https://ontology2.com/essays/LookingForMetadataInAllTheWrongPlaces.html" rel="nofollow">https://ontology2.com/essays/LookingForMetadataInAllTheWrong...</a><p>that did templating at the DOM level, that is the template of the page as a whole is an HTML document which is parsed, manipulated with the DOM, then written out again in HTML. It works just fine, but it is 100-1000x slower than a conventional templating engine so it is a non-starter for a lot of people.<p>My own opinion is the property of LISP being homoiconic would be less special if other languages had tools that made it easy to parse bidirectionally and process ASTs, but it hasn't really been done.<p>If you are interested in going down this road click on my profile link and send me an email!
Usually you would want to use a CST for this task as opposed to an AST to preserve white space. comments, and structure during the generation. There's not a ton of tools that are designed to handle that for polyglot tools, tree-sitter is one (but it's a parser generator and not a code generator).<p>I think it would be easier to write for one language rather than N at first.