Skip to content

Commit

Permalink
Get tests running and passing for tokenization
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Jul 8, 2023
1 parent fe9b8b1 commit c19b774
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ compile() {
}

test() {
zig build test
cd "$project_root"/old-rust-tests || exit 1
cargo test
}
Expand Down
5 changes: 4 additions & 1 deletion src/interpret.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ pub const DtMachine = struct {
.bool => |b| try self.push(DtVal{ .bool = b }),
.int => |i| try self.push(DtVal{ .int = i }),
.float => |f| try self.push(DtVal{ .float = f }),
.string => |s| try self.push(DtVal{ .string = s }),
.string => |s| {
const unescaped = try string.unescape(self.alloc, s);
try self.push(DtVal{ .string = unescaped });
},
.deferred_term => |cmd| try self.push(DtVal{ .deferred_command = cmd }),
.err => |e| try self.push(.{ .err = e }),
.none => {},
Expand Down
4 changes: 4 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ fn readShebangFile(allocator: Allocator) !?[]const u8 {

return null;
}

test {
std.testing.refAllDecls(@This());
}
9 changes: 6 additions & 3 deletions src/string.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ pub fn escape(allocator: Allocator, unescaped: String) !String {
}

pub fn unescape(allocator: Allocator, unescaped: String) !String {
var result = try allocator.dupe(u8, unescaped);
var buf: []u8 = undefined;

for (escapes) |esc| {
result = try std.mem.replaceOwned(u8, allocator, result, esc.to, esc.from);
buf = std.mem.replaceOwned(u8, allocator, unescaped, esc.to, esc.from) catch |e| {
allocator.free(buf);
return e;
};
}

return result;
return buf;
}
17 changes: 7 additions & 10 deletions src/tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ pub const TokenIterator = struct {
}
}
self.index = end + 1;
const unescaped = try string.unescape(self.allocator, self.buf[strStart..end]);
return .{ .string = unescaped };
return .{ .string = self.buf[strStart..end] };
},
'~' => { // Parse an error
const lastChar = std.mem.indexOfPos(u8, self.buf, start, "~") orelse self.buf.len;
Expand Down Expand Up @@ -142,6 +141,7 @@ pub const Token = union(enum) {
.string => |s| std.debug.assert(std.mem.eql(u8, other.string, s)),
.term => |t| std.debug.assert(std.mem.eql(u8, other.term, t)),
.deferred_term => |t| std.debug.assert(std.mem.eql(u8, other.deferred_term, t)),
.err => |e| std.debug.assert(std.mem.eql(u8, other.err, e)),
.none => std.debug.assert(other == Token.none),
}
}
Expand All @@ -160,15 +160,12 @@ test "parse hello.dt" {
try expected.append(Token{ .term = "def" });

const helloFile = @embedFile("test/hello.dt");
const tokens = try Token.parse(std.testing.allocator, helloFile);
defer tokens.deinit();
var tokens = Token.parse(std.testing.allocator, helloFile);

std.debug.assert(tokens.items.len == 6);
var i: u8 = 0;
while (i < 6) {
std.debug.print("Expected: {any}, Actual: {any} ... ", .{ expected.items[i], tokens.items[i] });
expected.items[i].assertEql(tokens.items[i]);
std.debug.print("PASS\n", .{});
i += 1;
while (try tokens.next()) |token| : (i += 1) {
std.log.info("Expected: {any}, Actual: {any} ... ", .{ expected.items[i], token });
expected.items[i].assertEql(token);
std.log.info("PASS\n", .{});
}
}

0 comments on commit c19b774

Please sign in to comment.