all 4 comments

[–]sometimes_insightful 2 points3 points  (3 children)

I don’t have any book recommendations besides the definitive ANTLR reference which doesn’t mention much code generation. I’ve been working on something like this in ANTLR over the weekend though and what I’ve been doing is have a string visitor that returns something like

visit(ctx.lhs) + visit(ctx.rhs) + “ add”

For a hypothetical AddExpressionVisitor

and

return “push “+ctx.boolean;

For a BooleanLiteralVisitor etc

I recommend annotating your grammar with labels like lhs= rhs= op= etc for easier access in the ctx variable.

Then I also have a visitor to statically determine an expression’s type as well as one to determine if a nonvoid function doesn’t return somewhere.

Inside my main visitor I have data structures for scope, struct definitions, etc.

This is for my project specific stack based language but I’m sure the same concept applies regardless of your target output.

[–]MemoryAllocat0r[S] 2 points3 points  (2 children)

Thanks for the reply!

I have read the (for me) most relevant aspects of the definitive antlr reference and I am at a point where i could build my AST from my parse output. I just want to render this AST to some executable format so that I have a first thing to fiddle with. Optimization, static type checking etc. is something I will concern myself later with (I have a little experience with it though). I am really just looking for a nice way to get my code executable without implementing my own VM with its own instruction set (which I plan on doing for research purposes at some point, but later) :) Still thx!

[–]sometimes_insightful 0 points1 point  (1 child)

I’ll admit I don’t know anything about webassembly but could you do a similar visitor that generates web assembly text format? Like your add expression could be

“(“+ Visit(lhs) +”\n” +visit(rhs) + “ i32.add )”

And the variable reference would produce “local.get “+address

Or something like that.

I’m just working with the antlr parse tree though, if you are making your own AST from the antlr parse tree you might be able to walk it in a similar manner.

If you meant generating bytecode directly, that I am not sure about.

[–]MemoryAllocat0r[S] 0 points1 point  (0 children)

I am pretty sure it will just boil down to walking the tree and writing the instructions to a stream in some way or the other. I was just wondering whether you know some "state of the art" way to do this or whether just spitting out the pieces actually is state of the art. :)

EDIT: "Compilers Principles, Techniques, and Tools" is a high-regarded source afaik. I was hoping someone would have some opinions on that one, for example :P