perlang-org / perlang

The Perlang Programming Language
https://perlang.org
MIT License
16 stars 1 forks source link

Rewrite the Perlang compiler in Perlang #454

Closed perlun closed 2 weeks ago

perlun commented 5 months ago

Moved to GitLab

Please continue to the new version of this issue here: https://gitlab.perlang.org/perlang/perlang/-/issues/454. The GitHub page you are currently reading will not contain the latest information on this issue.


In other words, "let's become self-hosted". :slightly_smiling_face:

With the effort done in #406, this is no longer a distant pipe dream. Here's the rough plan, as it currently stands:

Challenges

Some parts will be reasonable to rewrite, like the ones described above, but other parts of the compiler will provide a significantly more challenging experience. I am thinking about the scanning, parsing and tree-walking parts here. Those are all "glued together" because of the data structures they use (C# classes to represent the different expression and statement types). We can either:

  1. Do it as a "big-bang" conversion, where we convert all relevant code to Perlang in one big PR. Not a great idea, and it will be a huge undertaking to try to do it like this. And because of the immaturity of the language as it stands, we might even be sidetracked during the process by wanting/needing to add more language functionality to support the effort, delaying it even further.
  2. Find a way to "inter-op" between C# and Perlang.

The latter seems "more doable" in a way, but then again:

Let's postpone this for now, but it's still good to get this noted down here.

Timeframe

My current estimate is that the full rewrite of the compiler into Perlang will take about 3-5 years, given our current capacity. The process will hopefully help us mature the language and the compiler as a nice side-effect.

End-goal

The The V Programming Language has done their V-to-C transpiler in an interesting way. They provide the transpiled sources in an auto-generated repo here: https://github.com/vlang/vc.

That way, you can:

  1. Use the C version of the V compiler to build a functional compiler, without the V compiler.
  2. Then, recompile the V source code using the generated compiler in step 1.
  3. Finally, recompile the V source code using the built-from-V compiler in step 2. :slightly_smiling_face:

All of this can be seen in their simple Makefile. We could opt for something similar: the end goal could be to maintain the Perlang compiler as a single C++ file, "all-inclusive", only depending on the Perlang stdlib (also written in C++), which is then statically linked with the compiler => produces a native Perlang compiler for an arbitrary platform.

Then, we use that compiler to regenerate our C++ sources, just like our friends in the V project does it. :grin: Wouldn't that be quite nice?