The glorious @xkcdRandom Zig builtin

51 points by wofo 18 hours ago on lobsters | 8 comments

andrewrk | 15 hours ago

It's a really handy function which is why it's already built in to zig! In fact it only requires one keystroke to input, and it's already available as a primitive, always in scope ;-)

bfiedler | 15 hours ago

I think it is a poor choice of title, since it is not about @xkcdRandom being built-in to Zig, but about how Zig built-ins are implemented.

gavinmorrow | 12 hours ago

Before reading I thought it was actually implemented in the official compiler.

technetium | 16 hours ago

I'm not opposed to an LLM being used here, but some depth would have been appreciated. What files were modified? What issues did you run into? As someone with little understanding of Zig compiler internals, were only the intermediate stages of the compiler modified or did you have to hack on the parser as well? When does Zig resolve @builtins? At what stage are they executed?

[OP] wofo | 15 hours ago

As you can see from the diff linked in the article (and now here), only three files needed changes:

  • lib/std/zig/BuiltinFn.zig: declares the builtin (the parser picks it up automatically with no further changes).
  • lib/std/zig/AstGen.zig: handles the builtin (here's where we generate the 4 that the user sees in the end).
  • lib/std/zig/AstRlAnnotate.zig: I don't understand this part too well. Here we opt out of something that is relevant for other builtins but not for others.

Interestingly, the compiler's frontend (or at least part of it) is part of the standard library. I don't know how many stages there are, and even though I did everything manually (except searching for the first file to edit), most of it is still uncharted territory for me (the compiler is big enough and the builtin handling is encapsulated). I hadn't looked at Zig's compiler before this week. I do have some background knowledge in compilers, which helped me navigate the codebase.

travisgriggs | 12 hours ago

I'm confused now. I assumed "brain-coding" referred to doing it with a wet/meat brain, NOT an LLM? Is that the right interpretation?

Author disclosed that they used LLM essentially to search (e.g. "google, as in verb") where the functionality for builtins was at.

[OP] wofo | 12 hours ago

I assumed "brain-coding" referred to doing it with a wet/meat brain, NOT an LLM? Is that the right interpretation?

That's what I was thinking when I used the term. Not sure why the parent thought LLMs were used for more than that.

technetium | 11 hours ago

Right, I understood that part. Even if the coding was done with an LLM I’m honestly fine with that. I referred to the “searching” bit when I said I would have appreciated more details.