views:

245

answers:

6

Im just curious if there is is any sort of program/application out there that will allow to enter code in one language and translate that to another language such as asm. This seems perfectly possible....so does anything like this exist?

+14  A: 

Yes. They are called compilers.

Compilers are just one example of a class of programs called language-translators.

Compilers convert higher-level languages, such as C++ and Java into lower-level languages, including virtual machine byte-codes, assembly, C, or directly into machine-runnable object-code.

Oddthinking
I don't know if that was meant to be a smart ass response or not, but I think its pretty clear I am not asking about compiling into machine code.
Most compilers go to assembly. Then they run the assembler.
dmckee
It's not a smart ass response. It's the very definition of a compiler, look it up :)
Ben Schwehn
It's not clear, because assembly and machine code are functionally equivalent.
John Millikin
I gave a quick answer, and then padded it out within a few minutes. These comments are all on the quick answer I gave. I wasn't intending to be a smart-ass; sorry if it came across that way.
Oddthinking
I apologize for jumping to that conclusion so quick.
@dmckee - actually, few compilers except gcc do that.
anon
+4  A: 

It's effectively what any compilator does since assembler is just another form of machine code. I believe that GCC does this explicitly, and that you can ask it to show you the intermediate assembler. For instance, take a look at the GNU Assembler.

Emil H
yes, by passing the -S parameter to gcc
none
A: 

Here is a program for converting .NET code to Java, PHP, JavaScript and ActionScript http://jsc.sourceforge.net/

mcintyre321
A: 

There are tools that allow you to translate one language into another. Basically, there's a parser, a translator, and printer.

The parser obviously parses the source into a AST.

The translator then has to transform the AST into structures that make sense in the target language.

Finally, the printer understands the post-transformed structures and can output that target code.

chris
+1  A: 

Issues arise when you say that something is "perfectly possible." A feature of one language often does not translate directly or easily to another; that's why we choose a language for a task in the first place! For example, converting a fibonacci number from Java to C is trivial, but to Haskell? Sure, it's still doable, but try converting a program that opens posix threads and listens on multiple ports for various bits of network traffic.

Almost every piece of useful code relies on extensively on external libraries, many of which are not open source. Aside from this, what do you think the following should translate to in C? Java even?

def method( f ):
    G = {'a':1}
    f(G)

def f( x ):
    print( [ (key, value) for (key,value) in x.items() ] )

method(f)

This task is inherently more complicated than it seems for anything but the most trivial case (C-language to C-language.) Going between static and dynamically typed languages will be rough, as will anything language specific.

Stefan Kendall
A: 

You may be interested haXe, see http://haxe.org/.

Robby Slaughter