#Why isn't the value of `token` changing?

1 messages · Page 1 of 1 (latest)

pliant horizon
#

I'm very new to Zig and low-level, so sorry if the issue is obvious. I am trying to change token in the Token struct to TokenType.comment but it stays as null.

pub const Tokenizer = struct {
    source: []const u8,
    tokens: std.ArrayList(Token),

    current_line: u8,

    pub fn scanTokens(self: *Tokenizer) !void {
        //while (!(self.current_line >= self.source.len)) {
        try self.tokens.append(.{ .token = null, .line = self.current_line });
        var token = self.tokens.getLast();
        token.scanToken(self);

        self.current_line += 1;
        // }
        _ = try self.tokens.append(.{ .token = Token.TokenType.eof, .line = self.current_line });
    }

    pub const Token = struct {
        token: ?TokenType,
        line: usize,

        fn scanToken(self: *Token, tokenizer: *Tokenizer) void {
            switch (tokenizer.source[tokenizer.current_line]) {
                'n' => {
                    self.token = TokenType.comment;
                },
                else => {
                    std.debug.print("{c}\n", .{tokenizer.source[tokenizer.current_line]});
                },
            }
        }

        pub const TokenType = enum {
            eof,
            new_line,
            comment,
        };
    };
};

Below is how I'm using the struct.

pub fn main() !void {
    // an allocator is defined here (discord limit made me remove)

    const allocator = arena.allocator();

    var tokenizor = zkld.Tokenizer.Tokenizer{ .tokens = std.ArrayList(zkld.Tokenizer.Tokenizer.Token).init(allocator), .current_line = 0, .source = 
        \\name "Kura Bóbr Ja Pierdole"
        \\age 9
    };

    try tokenizor.scanTokens();

    for (tokenizor.tokens.items) |token| {
        std.debug.print("{any}\n", .{token.token});
    }
}

Expected output:

Tokenizer.Tokenizer.Token.TokenType.comment
Tokenizer.Tokenizer.Token.TokenType.eof

Output I'm getting:

null
Tokenizer.Tokenizer.Token.TokenType.eof
main dragon
#

getLast returns a copy, so the token youre mutating isnt the one in the list, you need a pointer to it

brazen river
#

^ this is correct, but you should probably just append after mutating it, rather than before

#

that way you don't need a pointer into the array