Python-to-Haxe Compiler

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: GitHub - nightblade9/dragon: Dragon is a universal Python translater.

The companion project that wraps around lime to build projects lives here: GitHub - nightblade9/mars: Command-line tools for generating and running HaxeFlixel games in Python, via Dragon.

6 Likes

May I ask why?

@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 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:

1 Like

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.

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?

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?

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:

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!

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:

3 Likes

Thanks! I really appreciate the frank feedback, especially if it’s not positive – that’s when you really rethink and re-evaluate and move forward. Sorry I didn’t reply to this sooner, I just saw it now (long story).

Fundamentally, I would say your premise is false – I actually like Haxe. I’ve been using the Haxe/OpenFL/HaxeFlixel stack since ~2013, left it for a while, and came back, because it really is the best fit for my needs.

That said, there are a couple of motivations for writing this project:

  • The core Haxe language is somewhat hard to use – abstract classes, macros, generic dictonary/hashmap classes (compare it to C#/Java/Python/Ruby), etc.
  • It’s something I can work on when I can’t code games (eg. too tired or need a break from gamedev)
  • For me, the ideal tech stack really is something like “write Python and it works like Haxe/OpenFL/HaxeFlixel.” I looked into other options (eg. Python running in-browser, OpenGL, etc) and a transpiler seems like the best approach. (eg. there’s no easy way to get Python into Android)

That said, this will still be a useful project for me, even if it takes a couple of years; and even if it’s not as good as a “true” Python development experience – complete with interactive debugger, the ability to throw a REPL loop at my code, etc.

Cheers though, I appreciate the feedback. :slight_smile:

2 Likes

Hey nightblade9 I know it’s been over a year, but I just wanted you to know from a noobie that Python is a great language (no flames intended) and Haxe is foreign to me, but seems like an alternate (again no flames intended). I would like to see your idea come to a happy concusion (meaning it being applicable for the Python community.

A little back story for you:
I love using Blender to create games (easy, fun, and used to use Python), but since their migration to the latest version they cut out their game engine.
So Armory and UPBGE picked up the ball and so far only Armory is still running with it (well I must add), but here’s the rub. Armory uses Haxe.
Unfortunately my knowledge of yet another language at my age (Not that it matters) is harder to grasp as I didn’t start learning any programming until about 2 years ago.
Yes a late to the race programmer, lol.
So, I attempted to use the new mutli-platform Haxe that Armory uses, but again found I was in waaaay over my head.
Then I found your project…
I thought to myself (as I read the README) ahhh, I’m saved…
Then my heart sank. I saw the last revision was Jan 2018
I thought… well maybe they might start it back up. And maybe it may just work… Who knew.
Which led me to this post that sparked my interst when I read you may pick this back up.

From a noobie (old fart) who loved Python’s ability to be easy to learn (and still learning btw, lol)…
Please continue this work. It may benefit a lot of upcoming and future programmers and game devs. I know it’s a lot of work (trust me I really understand that), but the work you started is a good thing. Don’t let it die and fall into shadow. So many programmers quit an idea because they feel it isn’t worth while, but never realize that it actually was worth it to other people.

See, I can relate, because when Blender cut out their game engine it sent me into the abyss. I though… who needs my game anyways, It’s dead.
But my loving wife saw that I was attempting to give up. She looked at me and said, “I won’t let you give up on your dream. It is worth it. They will love it, all you have to do is stay with it. I have your back.”
Now before you say anything. Yes I am still perusing my game (I won’t place an ad here, lol) but I just wanted to tell you about one man’s idea can change the world.
I believe that you can.
Believing is half the battle, and If my loving wife believes in me… Then I can believe in you. Because I believe that your project can help others (like me).

Live Long, Program Greatness.
IamKraZ

The problem here is actually fundamental: Python is a dynamic, “typeless,” interpreted language. Meanwhile, Haxe is a strictly-typed compiler. Never the 'twain shall meet.