reflaxe.CSharp icon indicating copy to clipboard operation
reflaxe.CSharp copied to clipboard

Haxe AST -> C# AST -> C# Printer

Open jeremyfa opened this issue 4 months ago • 5 comments

I started working on migrating current code from DirectToStringCompiler to GenericCompiler as well as make it use a printer instead of simply returning strings as expression. That (unfinished) work is in the generic-and-printer branch.

The initial rationale was to print C# code directly from Haxe AST because it was assumed the two languages are pretty close, and differences could be handled by manipulating Haxe AST directly, but @Simn advised on Discord against doing that and instead suggested to add a step to generate C# AST and print from it, in order to be sure we can carry the information we need and avoid making wrong assumptions.

Simon's explanation for reference:

What I can tell you is that the late C# target went for this "make the Haxe-AST more C#-compliant" appraoch with its gencommon framework, and that turned into a bit of an assumption hell. Things would frequently break when those assumptions turned out to no longer hold, and overall there was always this shadow of uncertainty because the data structure would not accurately match the output. As for concrete examples, IIRC one major problem with C# in particular is that its generics require type parameter information on calls which the Haxe AST doesn't carry. I don't remember the details, I think you had to figure out how concrete types mapped onto declared type parameters. You'll have to do this either way, of course, but doing it in a single, well-defined place instead of somehow trying to keep this information in the Haxe AST would just be way more pleasant.

As we don't want to repeat the mistakes of previous C# target, I decided to follow that advice and will work on generating an actual C# AST and print from it. This certainly requires some more work than without the intermediate C# AST but I agree it's for the best, long term.

Thus I'm pausing what I started in the generic-and-printer branch (there are still a lot of C# printing snippets that will be useful for later), and will start working on a Haxe AST -> C# AST -> C# Printer version (on a new branch).

This issue is there to let you know I'm working on this and keep track of the progress.

Current plan

(might change depending of how it goes when implementing it or from feedback in this issue):

  • Add a CSTypedExpr typedef which will be very similar to Haxe's TypedExpr, but modified as needed to match exactly the intended C# output. Do the same for any AST type needed to represent C# hierarchy (like ModuleType, ClassType etc...).
  • Use a GenericCompiler that is typed to return C# AST instead of strings
  • Create a CSPrinter class that outputs C# code from this C# AST

Timeline

I want to take time to make this right, so no plan to rush it. However, this project is very important for me (because I need it for Ceramic) so I'll dedicate a good amount of time to work on that C# target in 2024. Initial iteration for that C# AST implementation might take several weeks, then from that point if it goes right it will be merged to development branch and contributions from more people could be easier to manage without too much overlap.

A lot of work ahead, but exciting as well 😄

jeremyfa avatar Feb 21 '24 22:02 jeremyfa