Question

Often when translating between languages (whether with program translation or compiling) it's a one-way, destructive translation. The functionality of the "port" isn't lost, but some of the intent and expression is.

For instance, porting a program from Java to C is possible, but you lose the notion of classes and methods. If that program were to be ported back to Java you'd need to infer the intent of what would be considered an object to get back to the original state. And that's something that a computer isn't good at doing. As such you'd end up with a Java program that looked more like a C program with data structures and a big collection of static functions.

Also consider a compiler. Once a language has been compiled down to assembler or even to say CIL or JVM, concepts such as if statements are lost as they are turned into branches.

On a higher level, source to source translators exist, but some languages have features (i.e. delegates in C#) which don't exist in other languages (like Java not having delegates). The translation can happen, but is mildly destructive (i.e. Java would need a wrapper class to simulate delegates).

So, having said all that, is there a language who's goal is to be structurally compatible with all other languages by supporting all language features of all languages?

I'm not so concerned with being able to fully translate between languages as I am with being able to express each language's code in the common language. So the goal would be translating to the common language and back again to the source language without losing anything. Like: Java->Common->Java or LISP->Common->LISP

Was it helpful?

Solution

Once a language has been compiled down to assembler or even to say CIL or JVM, concepts such as if statements are lost as they are turned into branches.

Not particularly in practice. If you look at a tool like Reflector, it will happily turn CIL back into pretty accurate C# code, ifs and all.

So, having said all that, is there a language who's goal is to be structurally compatible with all other languages by supporting all language features of all languages?

No, because at that point we wouldn't need compilers or source translators since we have the One True Language. Sarcasm aside, this is effectively asking why we have different languages in the first place. Language features are not things that live in isolation. Providing one often hinders (or breaks) others. If there was a language that could provide the features of others without loss, why would we be using the others?

OTHER TIPS

Some intermediate languages have been successfully used as target for a lot of various languages, e.g. LLVM (or Ramsey & Jones' C--, which might be a dead project in 2015).

Notice that an intermediate language does not carry all the information provided in the source language (e.g. you are losing information when compiling from C to LLVM).

However, I am not sure that your wish makes sense. There are some programming language features which are not compositional, or are a whole program thing. Eg continuations (you can't implement call/cc without some support from your intermediate language), exceptions, or even garbage collection or parallelism or homoiconicity or persistence or threads (or concurrency).

Notice that for your question programming language semantics matter much more than syntax. Of course, all these intermediate representations are theoretically Turing-equivalent e.g. to a RAM machine.

BTW, some people might believe that x86-64 machine code or JavaScript is actually what you want....

In 2020 consider also libgccjit or Gimple or even generating C code. See of course this answer and Bismon. In RefPerSys we want to generate C++ code

The only way for a conversion from language X to some intermediate language IL and back to be lossless is for IL to be a superset of X. If you want to have multiple languages as X, then IL has to be a superset of all of them.

Therefore, I would suggest that IL has the following structure:

delimiter: ---------SOMERANDOMSTRING----------
---------SOMERANDOMSTRING----------
language: C
#include <stdio.h>
int main() { printf("hello world\n"); return 0; }
---------SOMERANDOMSTRING----------
language: Java
public class Main { public static void main(String[] args) {
  System.stdout.println("hello world");
}}
---------SOMERANDOMSTRING----------

In other words, MIME-like embedding of the actual source code into the language. The "compiler" then has to parse apart the pieces and pass them to a proper processor for that language.

This obviously isn't useful.

Licensed under: CC-BY-SA with attribution
scroll top