AST like coding syntax. Easy upgrade!

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 6 19:46:57 PDT 2015


On Sunday, 6 September 2015 at 21:16:18 UTC, Prudence wrote:
> Yeah, but wouldn't it be so much nicer? (and probably 
> debuggable inline)
>
> interpret({
>     var a = 5;
>     a += 2;
>     a;
> }
>

Not really because that already more-or-less works today (add a q 
before that { and it will compile).

The interpreter throws exceptions when it gets a bad program; 
syntax errors, runtime errors, etc. Those exceptions are 
propagated to the compiler user in the form of error messages. 
Delete the last semicolon and rerun it. The compiler now spews:


/home/me/arsd/script.d(2164): Error: uncaught CTFE exception 
arsd.script.ScriptCompileException("4: Parse error, unexpected 
end of input when reading expression, expecting ;")
d.d(4):        called from here: interpret("\x0a\x09\x09var a = 
5;\x0a\x09\x09a += 2;\x0a\x09\x09a\x0a\x09", var(cast(Type)0, 
Payload(null, , , , , , )).this(null), null)
d.d(9):        while evaluating pragma(msg, a)


d.d line 4 is the interpret function call. The error message from 
the interpreter is listed above that and lists line 4 of its 
input.


So it isn't exactly nice to read.... but probably could be. Keep 
in mind that I didn't write this thing for compile time, it is a 
runtime script interpreter that just happens to also work at 
compile time. If I wrote a thing that caught that exception and 
nicely formatted it at compile time, it could be made prettier.


But still, that message *does* contain the information you need 
because exceptions from the external parser are returned to the D 
compiler.




THAT SAID, I do know what you're asking and I've kinda wanted it 
before. It would be nice to have a .sourceof for an alias 
function (and I think that's been implemented in a pull request 
before, it isn't impossible to do). I just don't feel it would be 
all that important, since what we have now really goes a very 
long way.


More information about the Digitalmars-d mailing list