u/Big-Rub9545

Compiler implementation language

Currently starting “Writing a C Compiler” by Nora Sandler. I initially wanted to start the project in C, but she herself suggests doing it in another language. Since I like it and she also suggests a language with pattern matching, Rust seems like a very good alternative.

Hoping to get some thoughts on this, particularly from people who’ve gone through this book before.

I like doing projects in C if they need speed and low-level detail (though more often it’s because coding like a caveman is fun), but the repetitive boilerplate, weak generic system and standard library make larger projects a pain to work through.

Rust is much more convenient here, though I’m aware it may get somewhat verbose or constricting for low-level work (at least from my limited experience).

reddit.com
u/Big-Rub9545 — 7 days ago

I’m currently working on improving the error reporting for my language, of which a major part is printing the line containing the error (with the usual arrows to specify a particular span).

However, I’m unsure of which direction to go with in terms of actually getting that line, assuming I know the line number. I’ve considered the following approaches.

  1. Store an array of the file split into lines. I used this previously with another interpreter and it worked very well, though I’m worried about it possibly taking up excessive memory for larger files.

  2. Iterate/search through the file content string by jumping across newline terminators to find the n-th line, then making a view object across that line. This works well but searching may be quite slow for later line numbers.

  3. Read the file line by line, stopping at the n-th line and using it to report the error. Likely the worst solution out of the 3 since it involves file I/O and rereads the file again. However, it would still be a straightforward solution.

Any advice or suggestions on this? How did others here approach this problem?

reddit.com
u/Big-Rub9545 — 29 days ago