Hugo Hacker News

Let's write a compiler, part 4: Testing

p4bl0 2021-08-17 13:41:29 +0000 UTC [ - ]

For context, there was some discussion here on HN about parts 1 and 3 of this series:

https://news.ycombinator.com/item?id=28183062

https://news.ycombinator.com/item?id=28197876

rightbyte 2021-08-17 14:01:02 +0000 UTC [ - ]

Looking at the code it feels more like an assembler than a compiler since there is no mess and it outputs C code.

But it is very clean and nice. I like the pattern.

ModernMech 2021-08-17 16:26:18 +0000 UTC [ - ]

It seems like we're going to have the same discussion on this topic every day until all parts are posted.

Joker_vD 2021-08-17 15:07:33 +0000 UTC [ - ]

> more like an assembler than a compiler since there is no mess and it outputs C code.

Yes, translating between closely related languages is often like this: the mapping between their constructs is 1-to-1 and almost trivial. But try translating e.g. λ-calculus into C in one go, without separate lambda-lifting/closure-converting steps: it's absolutely doable but quite messy.

chrisseaton 2021-08-17 15:40:55 +0000 UTC [ - ]

> feels more like an assembler than a compiler

Yes it's a kind of transpiler, or template compiler, a typically 1-1 mapping.

monocasa 2021-08-17 18:27:46 +0000 UTC [ - ]

When absolute compiler perf isn't required, I love nanopass compilers for this. Each pass slowly massages a slightly different IR towards the final form and there isn't really a limit to how different the code source and end target can be, just needs more passes.