-2

If I wanted to turn a Java game into Lua, how could I do it? I'm thinking that you could have a program read the assembly language behind two programming languages, find the patterns, attach them to functions, and then convert the code accordingly. Has it been done, attempted, or theorized? What would be required to achieve this?

  • 2
    The term for this is "[porting](https://en.wikipedia.org/wiki/Porting)", and it's not as easy as you might think. – Dan Pichelman Jul 11 '16 at 17:00
  • A program that translates a program from one language to another language is called a "compiler", and compilers most certainly exist. But they are usually written by hand, not generated automatically, as you suggest. (Although I must admit I am not quite sure *what* it is you are suggesting.) – Jörg W Mittag Jul 11 '16 at 17:14
  • Java isn't compiled into assembly language, it's compiled into bytecode that is then interpreted by a Java-runtime. – Mark Jul 11 '16 at 21:21
  • @Mark They are both written in C apparently, would that mean anything? – Zachary Johnson Jul 11 '16 at 21:52

2 Answers2

0

If I understand you correctly, you suggest a tool such that if you have a compiler which compiles from A to B, and a compiler which compiles from C to B, then the tool could analyze the behavior of both compilers and from this resolve how to translate from A to C.

This sounds fascinating, but I never heard about anything remotely like this, and I'm unsure if it could even work. You probably have to invent it yourself!

In reality, if you want to translate code from one high level language into another, it is done manually by humans who knows both languages. There are some examples of tools which translate from one high-level languages into another, but this is only possible for languages which are semantically closely related (like from C# to VB.Net). For example it is simply not possible to make an automatic translator from C to Python, because a lot of things you can do in C is (deliberately) not possible in Python.

Of course a compiler is itself an automatic translator from one language to another, but they usually only go from a high-level language to a lover-level language, an the generated code is not designed to be human readable and therefore not usually practical to use as source code for further development.

JacquesB
  • 57,310
  • 21
  • 127
  • 176
  • 1
    Why do you say it is not possible to make a compiler from C to Python? There is [Clue](https://github.com/davidgiven/clue), which is a C compiler that targets ECMAScript, Lua, Perl, Common Lisp, and Java. There's no reason it shouldn't be able to target Python as well … or any Turing-complete language, really. [Emscripten](http://Emscripten.Org/) is a compiler from LLVM bitcode to ECMAScript (in other words, an LLVM backend for ECMAScript), and again, there's nothing standing in the way of doing something similar for Python. – Jörg W Mittag Jul 11 '16 at 18:39
  • @JörgWMittag: Interesting - so how does Clue compiler allow you to poke at arbitrary memory locations in say JavaScript? – JacquesB Jul 11 '16 at 19:07
  • 1
    @JacquesB, actually, C doesn't let you poke at arbitrary memory locations either. If you're doing that, you are invoking undefined behavior. – Winston Ewert Jul 11 '16 at 19:15
  • @WinstonEwert: But even if we *want* to allow it, it's trivial: just make "memory" a Python list, and then the C code can poke around in there all it wants. That's what "memory" is anyway, a list of byte-sized cells. – Jörg W Mittag Jul 12 '16 at 14:10
0

There are lots of compiler that do these kinds of conversions:

  1. GWT Java -> Javascript
  2. Haxe Haxe -> Just About Anything
  3. ClojureScript Clojure -> Javascript
  4. TioPort Java -> D
  5. RPython Python (Subset) -> C

In general, these compilers produce a second program that will do exactly the same thing as the first one. However, the second program will very difficult to read.

I'm thinking that you could have a program read the assembly language behind two programming languages, find the patterns, attach them to functions, and then convert the code accordingly.

That's not really how it works. Generally, they begin with your source, not your resulting assembly. For each construct in your source language: class, function, assignement, etc. it produces the equivalent construct in the target language. So if you write a function in your source language, it will add a function in the target language that does the same thing.

Winston Ewert
  • 24,732
  • 12
  • 72
  • 103