roc-lang / book-of-examples

Software Design by Example in Roc
Other
23 stars 15 forks source link

Topic proposal: A transpiler to JavaScript #51

Closed abhin4v closed 1 month ago

abhin4v commented 1 month ago

Topic proposal: A transpiler to JavaScript

I propose a new topic for the book: A transpiler to JavaScript. The idea to write a transpiler (a source-to-source compiler) for a simple imperative programming language to JavaScript (JS). This new language — that I call KahwahScript[^1] or KS for short — is similar to JS in syntax, but has fewer constructs and simpler/different semantics.

Some examples of different semantics:

Some examples of missing constructs:

In terms of code, we’ll need to write a Tokenizer, a Parser, a Translator and a CodeGenerator. This can be structured as two chapters, first for the first two components, and second of the last two.

Alternatively, we can make writing the Tokenizer and Parser an exercise for the readers while providing hints for their implementations, because there may be other chapters about parsing. In such case, we can have only one chapter that focuses on translation of KS to JS, and code generation.

We limit the scope to these components only, and leave the rest of the work as exercises for the readers. This may include:

The main takeaways from the chapter(s) will be:

The main potential complication that I see is, the readers may have to learn too much about the idiosyncrasies of JS.

For a proof of this concept, I’ve already written an implementation of the transpiler in Haskell.

[^1]: Kahwah is a traditional preparation of green tea consumed in the North Indian subcontinent, and in this case, a word play on Java (a coffee).

gvwilson commented 1 month ago

Hi @abhin4v - thank you for suggesting this, but one of our guidelines is that each topic (chapter) should be teachable in one hour, and this feels much bigger than that. We already have people working on parsers; is there some other subset of this idea that you could carve out? Thanks - @gvwilson

Anton-4 commented 1 month ago

one of our guidelines is that each topic (chapter) should be teachable in one hour, and this feels much bigger than that.

It does seem possible to make the language very simple. This would make the tokenizer, parser, translator and code generator very simple as well. Thereby making it teachable in one hour.

gvwilson commented 1 month ago

I'm still nervous:

  1. Introducing another language (albeit a mini-language).
  2. We already have chapters on parsing.
  3. The functional language community has a long history of focusing on language processors; perhaps the biggest objective of this book is to show that they can be used for other kinds of applications.

@abhin4v would you consider implementing the VM from https://third-bit.com/sdxpy/vm/ instead? Showing how to do things that most people consider very stateful (VM, editor) in a PF language would be cool, and the comparison with the Python and JavaScript versions (https://third-bit.com/sdxjs/virtual-machine/ for the latter) would be very cool.