How can I update my input and output from 0.15.1 to 0.15.2?

In 0.15.1, there is a 'Cal$ ’ waiting for me to type in and press Enter. This is what I want.

In version 0.15.2, when I press Enter for the first time, it will automatically and continuously output 'Cal$ Cal$ … "

var stdout_buf: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buf);
const stdout = &stdout_writer.interface;

// ...

fn prase(line: []const u8) !void {
    var lexer = Lexer.init(line);
    var token = lexer.next();

    while (token.type != TokenType.EOF and token.type != TokenType.ERROR) {
        try stdout.print("Token: {s}, Src: {s}\n", .{@tagName(token.type), token.src orelse "N/A"});

        token = lexer.next();
    }
    try stdout.print("Cal$ ", .{});
    try stdout.flush();
}

pub fn main() !void {
    var stdin_buf: [1024]u8 = undefined;
    var stdin_reader = std.fs.File.stdin().reader(&stdin_buf);
    const stdin = &stdin_reader.interface;

    try stdout.print("Cal$ ", .{});
    try stdout.flush();

    while (true) {
        const line = stdin.takeDelimiterExclusive('\n') catch |e| switch (e) {
            error.EndOfStream => break,
            else => continue,
        };

        try prase(line);
    }
}

Forgive me for the fact that my code seems to be written incorrectly.

I still don’t understand how to correctly implement my requirements, and I don’t even know what I need to look for in the documentation.

How can I modify my code? Or where should I start? Thank you!

You’ll need to take that delimiter to make progress. takeDelimiterExclusive means that the take excludes the delimiter. So, the delimiter remains in the buffer, and the next call reads an empty line forever because you never take the delimiter.

2 Likes

Thanks! I see! What a stupid question I asked :sob:

It is not stupid. Version 0.15.2 accidentally introduced a breaking change that affected you. Your code worked in 0.15.1 because of the bug. But the bugfix involving breaking change was intended for 0.16 and not for 0.15.x. All of this is a bit too complicated to understand considering that release notes are absent. Your question is perfectly reasonable.

7 Likes