Alex Rivera | Logout

Possible to write a Compiler with Javascript?

Asked 2011-09-08T05:31:39.123
14

Is it possible to use Javascript to write a compiler that can support other kind of language as scripting?

Let's say, I have a piece of HTML.

<script language="cpp" id="cppScriptBlock" EntryPoint="main">
    int main() {
        cout << "<h1>CPPHeader</h1>";
    }
</script>

<script language="java" id="javaScriptBlock" EntryPoint="MyJavaClass">
    public class MyJavaClass {
        public final void main() {
            java.lang.System.out.println("<h1>JavaHeader</h1>");
        }
    }
</script>

<script language="csharp" id="csharpScriptBlock" EntryPoint="MyCSharpClass ">
    public class MyCSharpClass {
        public static void Main() {
            System.Console.WriteLine("<h1>CSharpHeader</h1>");
        }
    }
</script>


<script language="javascript">
    $("#cppScriptBlock").compileAndRun();
    $("#javaScriptBlock").compileAndRun();
    $("#csharpScriptBlock").compileAndRun();
</script>

And finally generate the following HTML

<h1>CPPHeader</h1>
<h1>JavaHeader</h1>
<h1>CSharpHeader</h1>

Is it possible?

Alex

Edit
Report

2 Answers

2

See Metacompiler tutorial about how to write arbitrary compilers (and compier-compilers) in general, using Javascript as an implementation language.

answered 2011-09-08T05:55:10.347
1

You should take a look into JS tempting languages. Specifically the following:

answered 2011-09-08T05:33:16.147

Your Answer