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

Alex Yeung
  • 2,495
  • 7
  • 31
  • 48
  • 3
    Yes, it can be done. I must wish you good luck, though. :) – Robin Maben Sep 08 '11 at 05:34
  • 5
    I'll just leave this here: http://bellard.org/jslinux/ – mu is too short Sep 08 '11 at 05:38
  • 1
    See https://github.com/gpjt/webgl-lessons/blob/master/example01/index.html for an example in WebGL. GLSL (shading language) programs are written in script blocks. – Ray Toal Sep 08 '11 at 05:38
  • I've written an article on [How to easily create your own language that compiles to JavaScript](https://hackernoon.com/creating-your-own-javascript-based-programming-language-has-never-been-easier-wju33by). – jcubic Dec 21 '21 at 20:03

7 Answers7

21

Yes, it's very much possible using Jison.

It generates a JavaScript parser based on the language constructs you define.

Jison takes a context-free grammar as input and outputs a JavaScript file capable of parsing the language described by that grammar. You can then use the generated script to parse inputs and accept, reject, or perform actions based on the input.

-- from the documentation

PS: CoffeeScript! was also created using this. :)

Community
  • 1
  • 1
Robin Maben
  • 22,194
  • 16
  • 64
  • 99
7

Yes, but there's a lot of work you'd have to do. Just like a real compiler, you'd have to parse the code, convert it into intermediate code, etc. After that, you'd have to simulate the environment including all of the runtime libraries included with those languages. In short, it's not practical, but it is possible.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Convert to "intermediate code" implies I need a "Virtual Machine" built with Javascript to run the intermediate code, doesn't it? – Alex Yeung Sep 08 '11 at 12:45
  • @Alex: That's what I was assuming you would do. Most compilers (and a large number of interpreters, too) generate intermediate code at some point. Whether that's outputted, executed, or transformed more differs, but most compilers have a common step of generating something similar to intermediate code. – icktoofay Sep 09 '11 at 02:12
7

Yes, Javascript is Turing Complete. You can code anything in it that you can code in any language. Of course that includes compilers. I can't imagine any reason to ever do this though. If you're good enough at Javascript to write a compiler in it, you'd probably like to just write your code in javascript instead of another language.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 2
    Agree with you. Practically, I am not going to do that. I am just interested in possibility of building compiler with Javascript. The idea is for programming language combination. – Alex Yeung Sep 08 '11 at 23:49
  • 4
    that's like telling an artist that if he/she is good enough to paint a picture of someone painting a picture of a cat, he/she should just paint a cat. if the artist wants to paint, let the artist paint. just don't try to tell them WHAT to paint. – unsynchronized Sep 03 '15 at 20:54
3

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

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
2

Yes, it is possible. But instead of writing your parser by hand I would encourage you to use a good parser generator.

For example ANTLR by Terence Parr is a very powerful parser generator that has a JavaScript target. It works in environments supporting ECMAScript 5.1. (tested in Firefox, Safari, Chrome, Internet Explorer and Node.js). It is open source (BSD license) with extensive documentation and a very good book available.

Using a tool like this, instead of writing your own parser, you write a grammar and the parser is generated for you.

rsp
  • 107,747
  • 29
  • 201
  • 177
1

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

wesbos
  • 25,839
  • 30
  • 106
  • 143
0

Yes it's possible.

It would be much easier however, to write an interpreter that converts from one language into Javascript and then have the browser handle generation and execution of byte code.

zellio
  • 31,308
  • 1
  • 42
  • 61