Skip to content

Fix parser.zig and add test#4

Draft
aidenfoxivey wants to merge 3 commits intoRyEggGit:mainfrom
aidenfoxivey:main
Draft

Fix parser.zig and add test#4
aidenfoxivey wants to merge 3 commits intoRyEggGit:mainfrom
aidenfoxivey:main

Conversation

@aidenfoxivey
Copy link
Copy Markdown
Contributor

@aidenfoxivey aidenfoxivey commented Apr 8, 2026

I fixed parser.zig - not sure if isSpecial is how you wanted it. Can make amendments.

src/parser.zig Outdated
Comment on lines +38 to +51
try tokens.append(allocator, .{ .word = "||" });
i += 2;
} else {
try tokens.append(allocator, .{ .word = "|" });
i += 1;
}
},
'&' => {
if (i + 1 < input.len and input[i + 1] == '&') {
try tokens.append(allocator, .{ .word = "&&" });
i += 2;
} else {
try tokens.append(allocator, .{ .word = "&" });
i += 1;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these shoudl return a Token enum

Comment on lines +90 to +115
fn expectTokens(input: []const u8, expected: []const Token, allocator: std.mem.Allocator) !void {
const tokens = try tokenize(input, allocator);
defer allocator.free(tokens);
try testing.expectEqualDeep(expected, tokens);
}

test "canonical example" {
try expectTokens("echo hello world", &[_]Token{ Token{ .word = "echo" }, Token{ .word = "hello" }, Token{ .word = "world" } }, testing.allocator);
}

test "quotes" {
try expectTokens("echo \"hello world\"", &[_]Token{ Token{ .word = "echo" }, Token{ .word = "hello world" } }, testing.allocator);
}

// TODO: Decide if an unmatched quote should be an error or if it should just assume until \n is the quote.
test "unmatched quote" {
try expectTokens("echo \"hello world", &[_]Token{ Token{ .word = "echo" }, Token{ .word = "hello world" } }, testing.allocator);
}

test "leading and trailing spaces" {
try expectTokens(" echo hello world ", &[_]Token{ Token{ .word = "echo" }, Token{ .word = "hello" }, Token{ .word = "world" } }, testing.allocator);
}

test "pipes and logical operators" {
try expectTokens("echo hello | grep h && echo done", &[_]Token{ Token{ .word = "echo" }, Token{ .word = "hello" }, Token{ .word = "|" }, Token{ .word = "grep" }, Token{ .word = "h" }, Token{ .word = "&&" }, Token{ .word = "echo" }, Token{ .word = "done" } }, testing.allocator);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

U da 🐐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants