Skip to content

Commit

Permalink
Small class compiler test update
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jun 2, 2024
1 parent 3a8d7ee commit 6bcce25
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ pub const Compiler = struct {
.indexer => |idx| {
if (idx.target.type == .identifier) {
if (self.types.get(idx.target.type.identifier)) |stmt|
return self.fail("Cannot assign value to {s}", token, .{@tagName(stmt.type)});
return self.fail("Cannot assign value to {s} field", token, .{@tagName(stmt.type)});
}
try self.compileExpression(bin.right);
try self.compileExpression(bin.left);
Expand Down Expand Up @@ -988,11 +988,8 @@ pub const Compiler = struct {
_ = try self.writeInt(u8, @as(u8, @intCast(free_symbols.len)), token);
},
.instance => |ins| {
var cls: ?*const ast.Statement = null;
if (self.types.get(ins.name)) |stmt| {
cls = stmt;
}
if (cls == null or cls.?.type != .class) return self.fail("Unknown class {s}", token, .{ins.name});
const cls: ?*const ast.Statement = self.types.get(ins.name);
if (cls == null or cls.?.type != .class) return self.fail("Unknown class '{s}'", token, .{ins.name});
for (ins.fields, 0..) |field, i| {
if (!arrayOfTypeContains(u8, cls.?.type.class.field_names, ins.field_names[i]))
return self.fail("Class {s} does not contain a field named '{s}'", token, .{ ins.name, ins.field_names[i] });
Expand Down
5 changes: 5 additions & 0 deletions src/vm.test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ test "Class Compile Error" {
\\ value = 0
\\ }
\\ Test = 55
,
\\ var test = new Test{}
};
inline for (tests) |input| {
var mod = Module.create(allocator);
Expand All @@ -818,6 +820,7 @@ test "Instance" {
\\ list = List{},
\\ nested = List{}
\\ }
\\ assert(Test.value == 0, "Test.value == 0")
\\ const test = new Test{}
\\ test.value = 5
\\ assert(test.value == 5, "test.value == 5")
Expand All @@ -835,6 +838,8 @@ test "Instance" {
\\ assert(test.nested[0] == 2, "test.nested[2] == 2")
\\ test.list.add(test.nested)
\\ assert(test.list[1][0] == 2, "test.list[1][0] == 2")
\\ test.value = Test.value
\\ assert(test.value == 0, "test.value == 0")
;
var mod = Module.create(allocator);
defer mod.deinit();
Expand Down

0 comments on commit 6bcce25

Please sign in to comment.