COMMUNITY

Python-to-Haxe Compiler


#1

Hi,

I spent the last month prototyping a Python-to-Haxe compiler. The goal was to eventually be able to write HaxeFlixel games in Python.

For various reasons, I decided not to finish this (gargantuan) project. But I did get as far as converting the HaxeFlixel default template project into Python (as my “starter” code) and compiling it back to Haxe.

If anyone is interested, you can read up about it here: https://github.com/nightblade9/dragon

The companion project that wraps around lime to build projects lives here: https://github.com/nightblade9/mars


(Michael Solomon) #2

May I ask why?


(Marcelo Serpa) #3

@Michael

May I ask why?

He might enjoy programming in Python more but still wants to tap into Haxe’s compiler and eco-system. That’s the same thing tardis-go does.

Some people just don’t like Haxe, the language. There are actually other similar projects around, I recall the Spoon language (abandoned, I think) and ruhaxe, which compiles a subset of Ruby to Haxe.

It’s can be a rabbit hole, but it’s a very valid use case. Using Haxe as a target language also allows you to potentially implement new features for your language 1) without the need to learn Ocaml, 2) without waiting for the Haxe core team to do it.

@nightblade9 Very interesting project, thanks for sharing :slight_smile:


#4

This is exactly why. I’ve used about a dozen programming languages, and Python is overall my favourite. (No flame war intended – I actually like Ruby more, but it seems to have a much smaller community base, and Python is not without flaws.)

I thought writing a proof-of-concept is the hard part, but the hard part is actually that it’s not Python. It’s not as easy to use, etc.

Someone else may be interseted in this project in the future. I think, overall, it won’t achieve my goals/needs, so I put it down.


(Marcelo Serpa) #5

Hi @nightblade9

I think, overall, it won’t achieve my goals/needs, so I put it down.

Do you mind elaborating on why you think that? What roadblocks have you stumbled upon specifically?


#6

Not at all. I documented them in the README, actually:

Dragon is now abandoned, due to a number of problems:

  • The complexity of implementing some Python features (such as generators and generator functions)
  • The vast array of built-in Python functions that need to be re-implemented (eg. itertools)
  • Lack of code completion (unless you wrap Haxe natives in Python classes)
  • The end-result is not “Python”-like – you can’t put it into the REPL and poke it.

I conducted a thought experiment: what if I finished Dragon? What would the final user experience be for a Python/Haxe developer?

That’s where the last two issues came about – to get code-complete, I probably need to create Python versions that wrap Haxe classes. This is tedious, because HaxeFlixel updates quite often. I might be able to automate it, using the xml target and auto-generating the Python code. But that’s a big undertaking.

Similarly, I would also probably have to reimplement a lot of the Python library in Haxe; unless I could use something like the source of PyPy and run Dragon through it to generate Haxe code.

Even if I accomplished all that, the final result is not Python. You can’t throw it into the REPL, you can’t run dir(x) on some variable to see methods, and so on. And I’m not even touching on the complexity of implementing generators, local/global/nonlocal scope, etc. in Haxe – I don’t have a clue how to do that.

So while there aren’t any good cross-platform Python game development libraries out there, using Haxe as a back-end is not a great idea in my view. I’d rather write raw Haxe, and focus my time on libraries that I can use to get stuff done faster.

Does that make sense?


#7

After thinking about this more, and looking at solutions like WebRuby and Skulpt, I don’t think this approach is wrong; it’s not that it’s not Pythonic; it’s typical to extend these kinds of languages in this way to get wider portability or functionality.

It’s just a ton of work. If someone’s interested in the results (or in helping out), maybe I can use that to convince myself to pick it up again :slight_smile:


#8

I found a fellow Python/Haxe developer to collaborate with, so I’ll definitely be picking this up again in the future. I am very much interested in hearing from the “Haxe” side of the community about this, so please don’t hesitate to give me your thoughts and feedback!


(Jérémy Faivre) #9

Back a few years ago, I experimented converting Typescript code to Haxe (repo of the converter is there: https://github.com/jeremyfa/node-ts2hx but don’t try to use it on a real project heh).

My main motivation was not that I didn’t like Haxe. I really think Haxe is a great language. But at that time the language got pretty poor IDE integration (especially on Mac where there wasn’t anything like FlashDevelop/HaxeDevelop) and made it difficult to commit to it compared to other languages (like typescript) that already got pretty good editors.

But nowadays, these issues are mostly solved: There is Visual Studio Code (that didn’t exist a few years back) and an excellent plugin for Haxe maintained by the Haxe Foundation. Haxe language + VSCode are one of the most comfortable tools I use.

I switched to Haxe to make games and a load of other things and I am not looking back.

It’s super interesting to transpile python to Haxe, I had fun doing similar things to convert code from Typescript to Haxe and can imagin how rewarding it was to see your python code just compile and run a flixel project! But here are my thoughts: I think you could give Haxe a second chance. It really is a great and powerful language which is easy to get started with nowadays. Plus Visual Studio Code integration is top notch and works on all desktop platforms :+1:

And, if you do like python, there are probably plenty of use cases where python and Haxe+python target could play very well together!

I know all I say here is not encouraging you to work on your python-to-haxe converter, but you asked for feedback and thoughts, so here they are :nerd_face: