diff --git a/src/compiler.zig b/src/compiler.zig index c70588b..5d0c81d 100644 --- a/src/compiler.zig +++ b/src/compiler.zig @@ -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); @@ -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] }); diff --git a/src/vm.test.zig b/src/vm.test.zig index fba969b..bc41516 100644 --- a/src/vm.test.zig +++ b/src/vm.test.zig @@ -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); @@ -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") @@ -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();