News for hackers. Stuff that matters.
Re:Zig's Comptime Is Bonkers Good by gotoeleven on Tuesday January 07, 2025 @11:08AM (#42623919)
Surely there's a way to generate code by manipulating an AST structure? Is there some reason this can't be done in Zig or is it just that no one has bothered?

Doing it this way is more verbose but sidesteps all hygiene issues.

Re:Zig's Comptime Is Bonkers Good by Cloudef on Tuesday January 07, 2025 @12:32PM (#42624995)
Generating code during comptime is explicitly forbidden by the author. You can still generate code during build.zig of course.
Re:Zig's Comptime Is Bonkers Good by ScottRedig on Tuesday January 07, 2025 @06:48PM (#42629144)
Zig lets you inspect type info (including, eg, function signatures), but it doesn't give you raw access to the AST. There's no way to access the ast of the body of the function. As highlighted by view 0 in my article, I consider this a good thing. Zig code can be read without consideration for which pieces are comptime or not, something that heavy AST manipulation loses.

Though, if you really wanted to do stupid things, you could use @embedFile to load a Zig source file, then use the Zig compiler's tokenizer/ast parser (which are in the standard library) to parse that file into an AST. Don't do that, but you could.

Re:Zig's Comptime Is Bonkers Good by Cloudef on Tuesday January 07, 2025 @07:37PM (#42629559)
This reminds me of comptime brainfuck interpreter https://github.com/edqx/ccb