5

Possible Duplicate:
How do I create my own programming language and a compiler for it

I want to create a new general purpose language that will compile to JavaScript and I'd like to be able to write it in that same JavaScript.

I wouldn't go much into detail about why, but how can I do it? Currently I have professionally coded in JS, Java, Groovy, PL/SQL, PHP among the rest, so I'm covered on the language user side of the story, but I have no theoretical or practical knowledge from the create language side.

I have browsed the web for the past week and noticed a soup of acronyms and/or names like RR, LALR, BNF, AST etc. so I figure I need two things:

  • Good Book(s) about theory
  • Working projects or experiments that I can use to jump start my learning (something free like that narcissus)

EDIT:

No, this question does not cover the EXACTLY SAME content and it is not on the same topic even worse, even though there are two good answers, they don't answer how I can practically USE JS TO BUILD THE TOOLS (JS TRANSPILER) I NEED FOR THE NEW LANGUAGE.

Any ideas?

Azder
  • 193

2 Answers2

15

First, forget about LALR and the other intimidating words associated with the dragons. There is a nice and friendly one, behold: PEG. A ready to use implementation for JavaScript is available.

After you've done with the parsing you will have to transform your Abstract Syntax Tree in one or several steps into an AST of JavaScript and serialise it into text. There are many schools of thought in this area, but my personal preference is to use the very simple and obvious term rewriting model. You can draw your inspiration from things like this.

An unmodified pure JavaScript is not quite suitable for implementing compilers straight away, since it does not provide any decent pattern matching facilities, so I'd recommend to implement a eDSL layer as above first before starting to implement your language transforms.

Depending on what kind of type system your source language will have, you may want to implement various kinds of typing algorithms. For some of the practical strict type systems, an embedded Prolog might help a lot. E.g., implementing a Hindley-Milner algorithm in Prolog is trivial. Otherwise, a simple type propagation implemented with trivial term rewriting will be sufficient.

If you want to perform some optimisations, consider employing intermediate representations such as SSA or CPS. You may possibly need a decent graph handling library for it.

SK-logic
  • 8,517
6

It is told that Jeremy Ashkenas was inspired to create CoffeeScript after reading this very book. Since CoffeeScript is one of the most popular languages for javascript as target language, I guess you also can find some useful information there.

shabunc
  • 2,454