Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there programming languages that directly translate into another?

Is there a programming language that doesn't compile, but rather just translates into another language? I apologize if this is a stupid question to ask, but I was just wondering if this would be a literal shortcut in creating a programming language. Wouldn't it be easier (probably not speedy) but still doable?

like image 625
Jason Nümbãr Avatar asked Mar 20 '18 21:03

Jason Nümbãr


1 Answers

Is there a programming language that doesn't compile, but rather just translates into another language?

That makes no sense to me. My definition of compilation is "translating from one language (the source language) to another (the target language)".

Usually the source language is something written by humans and the target language is machine code (or asm), but that's not a requirement. In fact, many compilers are structured as multiple layers, each translating to another intermediate language (until the final layer emits code in the target language).

And it's not directly related to a language, but a particular implementation. We can take C, for example: There are C interpreters, C compilers that target assembler code, C compilers that target machine code (of various platforms), C compilers that target JavaScript, C compilers that target Perl, etc.

As for simplifying the implementation of a language: Yes, there are various kinds of code reuse that apply.

One way is to separate compiler front-ends (translate from source language to an internal abstract representation) and back-ends (translate from the internal abstract representation to machine code for a particular platform). This way you can keep the front-end and only write a new back-end if you want to support another target platform. You can also keep the back-end and only write a new front-end if you want to add support for another source language.

Another way is to use a full-blown programming language as the intermediate representation. For example, your new compiler might produce C code, which can then be compiled to machine code by any C compiler. The first implementation of C++ did exactly this. C has a number of drawbacks as a compiler target language; there have been efforts to create languages better suited for the task (see e.g. C--, which is used internally by GHC (a Haskell compiler)).

like image 108
melpomene Avatar answered Sep 28 '22 00:09

melpomene