News for hackers. Stuff that matters.
Re:My First Fifteen Compilers by cosarara on Friday July 12, 2019 @05:08AM (#20419336)
How does this approach play with error handling? If there is an error in my code and it's only caught at step 10, won't the error message be completely inscrutable? Will the line numbers make sense?
Re:My First Fifteen Compilers by thedufer on Friday July 12, 2019 @07:29AM (#20419878)
In the only compiler I'm somewhat familiar with (OCaml), a decent amount of the work is done in AST->AST transformations. In the AST, every node is annotated with a location, which is a tuple of filename, line number, char start, char end. As long as these are propagated in the right way, you get good location information in errors.
Re:My First Fifteen Compilers by mojuba on Friday July 12, 2019 @05:37AM (#20419430)
Line number information is usually passed down the chain because it's required for generating debug info anyway.

However, as you descend into deeper optimization layers of the compiler, errors are less and less likely to occur. Optimizations are either possible or not, though sometimes these layers can warn you about possible inefficiencies at the source code level (unused local, etc.)

Re:My First Fifteen Compilers by sterlind on Friday July 12, 2019 @06:34AM (#20419620)
A simple approach could be to mark up spans of characters in layer N with what spans were used to generate them in layer N-1. Then when parser N hits an error, put red squiggly lines under the source characters responsible. For instance, in C#:

using (<span decl>var x = 3</span>) { <span inner>Console.WriteLine(x);</span>}

Parser 0 transforms the using:

<span stmt1 parent=decl>var x = 3;</span> try { <span stmt2 parent=inner> Console.WriteLine(x);</span> } finally { <span stmt3=generated>x.Dispose();</span> }

Parser 1 (or a later parser, inductively) notices x doesn't have Dispose() defined, and throws MissingMethod(Dispose, span=generated).

Parser 0 catches this error, knows that this is because x isn't disosable, and emits its own error: NotDisposable(span=decl).

If a parser doesn't know to catch the error, it still passes it up the chain, transforming spans where appropriate. When it hits the IDE, the error is printed and the red squiggly lines go where they should.

(Disclaimer: I've never written a compiler)