Hello everyone,i’m a newcomer to zig , im trying learn zig by creating a bytecode interpreter by following crafting interpreters . i wanted to ask for feedback for my project. please guide me how can i write better and performant zig.
here the link for the repo :
zin
Hey, very impressive project!
From a code-style pov a lot of your structs should be file-level IMO, e.g. unit.Unit, vm.VM, gc.GC etc. The name doubling is kind of redundant.
thank you very much @Justus2308 for taking the time to check it out.
yeah there is a lot name doubling,i thought it would be cleaner, instead of writing
const lexer=@import("lexer.zig")
fn demo(tag:lexer.Token.Tag)void{
}
everywhere just make it a type alias thing like
const Token=lexer.Token
use this every where in the file.
they’re talking about having a file called unit.zig
which just contains a struct called Unit
when you could make the entire file the struct so instead of const Unit = @import("unit.zig").Unit
you would do const Unit = @import("Unit.zig")
.
what you are thinking of is just aliasing which is totally fine
Yeah definitely, but what I mean is that instead of having lexer.zig
and Lexer
as a struct in that file, you could just have Lexer.zig
as a file-level struct. That way your Lexer
type would just be const Lexer = @import("Lexer.zig")
instead of const Lexer = lexer.Lexer
. If Token
is strongly related to Lexer
anyway, it could be a type inside that struct and you could still refer to it as const Token = Lexer.Token
.
The stdlib frequently uses this pattern (e.g. for SinglyLinkedList). Note how the type itself is the file and includes its sub-types (e.g. Node
).
Thank you @vulpesx and @Justus2308 .
Im sorry i didnt get it first ,i thought you are taking about type alias stuff but i understand it now.