Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haxe backend in Haxe?

I know that Haxe compiler is written in OCaml programming language, which is a quite good choice for compiler developer. However recently I found Luaxe project, that seems to be a full-featured Lua backend for Haxe and was developed as pure Haxe library. I looked at the code of the project and it is using some kind of macro magic to generate Lua source code at compile time. So I would like to ask is it possible to implement a full-featured backend in pure Haxe without digging into OCaml and rebuilding the compiler? If so is there any limitations? Is there any good article on how to implement a backend in pure Haxe?

like image 960
user2102508 Avatar asked Feb 12 '23 05:02

user2102508


1 Answers

It looks like the Luaxe project is using the setCustomJSGenerator API.

Basically, this runs as a macro, using --macro "setCustomJSGenerator(luaxe.LuaGenerator.use)" or something similar.

I've never built a custom backend myself, but you can use it either to do custom Javascript, or, to generate source code for a different language, which is done in each of these:

  • Example JS Generator in the Haxe Standard Library: https://github.com/HaxeFoundation/haxe/blob/development/std/haxe/macro/ExampleJSGenerator.hx
  • The Lua Generator from Luaxe: https://github.com/PeyTy/LuaXe/blob/master/luaxe/LuaGenerator.hx
  • A Python code generator: https://github.com/frabbit/hx2python/blob/development/src/python/gen/PythonGenerator.hx
  • A Dart code generator: https://bitbucket.org/AndrewVernon/hx2dart/src/203d61b299c88ca3a2b043335d92b71546ce1239/src/haxe/macro/DartGenerator.hx?at=development

You can look at each of these to get an idea of how the APIs work, they all have a fairly similar structure by the looks of things.

In terms of limitations, you would have to ask someone who has used this. I know the Python generator ended up being moved into the standard Haxe compiler, so is presumably in OCaml now - perhaps they found it too limiting? I'm not sure.

If you would like to know more about specific limitations of this approach, I would either ask on the Haxe mailing list, or contact the developers of the targets above and ask for them to share their wisdom.

like image 191
Jason O'Neil Avatar answered Feb 25 '23 02:02

Jason O'Neil