Search code examples
language-agnosticcompiler-constructionprogramming-languagescompiler-development

Compiler that recognize different-different languages and send them to their corresponding compilers. Possible?


I was thinking whether it is possible to bridge asp.net, php and java to form a single page.

Actually i dont need any such thing as of now. It was just an idea that stiked to my mind as some features of some languages are good and some features or some other languages are good, so i was thinking what if i combine all these features into one

I mean, I m creating a page that has code from all the 3 languages asp.net php and java.

<asp code></asp code>
<php code></php code>
<java code></java code>

or

<html>
    <asp code>
    <php code></php code>
    <java code></java code>
    </asp code>
</html>

or something like that complier recognize different segments of code and send them to run on their compilers to execute. And output can be recognized and used by other languages in XML

i m not saying that all the languages to interact with each other. Although they can interact with each other through XML. But I only mean to say that the file is compiled as a single entity having different- different programming languages code which are send to their respective compiler to get executed and finally returning back to the parent compiler

I am thinking of a compiler that can be developed whcih recognize different languages code and send them to their compiler as done by .net framework eg MSIL


Solution

  • Is it possible?

    To quote reverend Lovejoy from The Simpsons "short answer no with an if. Long answer yes, with a but."

    No, it is not currently possible if you use currently available technology.

    Yes, but it requires you to roll your own server which would act as a shim, splitting out the different sections of code and sending them to the requisite language parsers + compilers, and then bring those separate sections back together to display the page.

    Edit: @Shantanu: My pleasure. The implementation is completely left up to you as I have not investigated anything like this at all.

    Ultimately I feel this is not the most productive thing to do as you will probably run into a large number of issues.

    The biggest being: Code from one language will have no concept of what is being done in the other languages.

    i.e. If you have a variable defined with values in your ASP, the Java or PHP versions would be unaware of it without huge effort, not to mention they would be completely unable to access the memory from each other's processes.

    However, if you do want to go down this route, I suggest you look into a parser generator like ANTLR. It will help you write a parser which could look for your special tags (note, this could be done with regex or a hand spun parser if needs be).

    Once you have the split code, you will want to send it to the compilers for each language, which you should be able to receive textual output from. Once you have that text, it should be all html + javascript, which can then be combined back together to display the page.

    I will say that if you want to have the 3 languages interact, you will be creating a HUGE project. It may be easier to use the .Net framework and write the PHP and JAVA (which probably already exist) languages for it, allowing you to forgo creating a whole server stack.