Skip to content

Commit

Permalink
fix: Instance property could be referenced from object type
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jul 6, 2024
1 parent 68b2882 commit 33061c5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
46 changes: 23 additions & 23 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ pub const Node = struct {
As: IsAs,
AsyncCall: Node.Index,
Binary: Binary,
Block: []Node.Index,
BlockExpression: []Node.Index,
Block: []const Node.Index,
BlockExpression: []const Node.Index,
Boolean: bool,
Break: ?Node.Index,
Call: Call,
Expand Down Expand Up @@ -202,7 +202,7 @@ pub const Node = struct {
Return: Return,
// The type should be taken from the type_def field and not the token
SimpleType: void,
String: []Node.Index,
String: []const Node.Index,
StringLiteral: *obj.ObjString,
Subscript: Subscript,
Throw: Throw,
Expand Down Expand Up @@ -380,7 +380,7 @@ pub fn usesFiber(self: Self, node: Node.Index, seen: *std.AutoHashMap(Node.Index
}

pub const AnonymousObjectType = struct {
fields: []Field,
fields: []const Field,

pub const Field = struct {
name: TokenIndex,
Expand All @@ -399,7 +399,7 @@ pub const Call = struct {
callee: Node.Index,
// We need this because in a dot.call, callee is dot and its type will be == to call return type
callee_type_def: *obj.ObjTypeDef,
arguments: []Argument,
arguments: []const Argument,
catch_default: ?Node.Index,
tail_call: bool = false,

Expand Down Expand Up @@ -436,7 +436,7 @@ pub const Enum = struct {
name: TokenIndex,
case_type: ?Node.Index,
slot: Slot,
cases: []Case,
cases: []const Case,

pub const Case = struct {
name: TokenIndex,
Expand All @@ -457,9 +457,9 @@ pub const FiberType = struct {
};

pub const For = struct {
init_declarations: []Node.Index,
init_declarations: []const Node.Index,
condition: Node.Index,
post_loop: []Node.Index,
post_loop: []const Node.Index,
body: Node.Index,
label: ?TokenIndex,
};
Expand Down Expand Up @@ -506,8 +506,8 @@ pub const Function = struct {
pub const Entry = struct {
main_slot: ?usize = null,
main_location: ?TokenIndex = null,
test_slots: []usize,
test_locations: []TokenIndex,
test_slots: []const usize,
test_locations: []const TokenIndex,
exported_count: usize = 0,
};
};
Expand All @@ -516,9 +516,9 @@ pub const FunctionType = struct {
name: ?TokenIndex,
return_type: ?Node.Index,
yield_type: ?Node.Index,
error_types: []Node.Index,
arguments: []Argument,
generic_types: []TokenIndex,
error_types: []const Node.Index,
arguments: []const Argument,
generic_types: []const TokenIndex,
lambda: bool,

pub const Argument = struct {
Expand All @@ -536,7 +536,7 @@ pub const FunDeclaration = struct {
};

pub const GenericResolveType = struct {
resolved_types: []Node.Index,
resolved_types: []const Node.Index,
};

pub const If = struct {
Expand All @@ -549,7 +549,7 @@ pub const If = struct {
};

pub const Import = struct {
imported_symbols: []TokenIndex,
imported_symbols: []const TokenIndex,
prefix: ?TokenIndex,
path: TokenIndex,
import: ?Parser.ScriptImport,
Expand All @@ -562,7 +562,7 @@ pub const IsAs = struct {

pub const List = struct {
explicit_item_type: ?TokenIndex,
items: []Node.Index,
items: []const Node.Index,
};

pub const ListType = struct {
Expand All @@ -573,7 +573,7 @@ pub const Map = struct {
explicit_key_type: ?Node.Index,
explicit_value_type: ?Node.Index,

entries: []Entry,
entries: []const Entry,

pub const Entry = struct {
key: Node.Index,
Expand Down Expand Up @@ -605,10 +605,10 @@ pub const NamedVariable = struct {
pub const ObjectDeclaration = struct {
name: TokenIndex,
slot: Slot,
protocols: []Node.Index,
generics: []TokenIndex,
protocols: []const Node.Index,
generics: []const TokenIndex,
// List of either Function (methods) or VarDeclaration (properties)
members: []Member,
members: []const Member,

pub const Member = struct {
name: TokenIndex,
Expand All @@ -620,7 +620,7 @@ pub const ObjectDeclaration = struct {

pub const ObjectInit = struct {
object: ?Node.Index, // Should be a NamedVariableNode or GenericResolve
properties: []Property,
properties: []const Property,

pub const Property = struct {
name: TokenIndex,
Expand All @@ -631,7 +631,7 @@ pub const ObjectInit = struct {
pub const ProtocolDeclaration = struct {
name: TokenIndex,
slot: Slot,
methods: []Method,
methods: []const Method,

pub const Method = struct {
docblock: ?TokenIndex,
Expand Down Expand Up @@ -665,7 +665,7 @@ pub const Throw = struct {

pub const Try = struct {
body: Node.Index,
clauses: []Clause,
clauses: []const Clause,
unconditional_clause: ?Node.Index,

pub const Clause = struct {
Expand Down
2 changes: 1 addition & 1 deletion src/Codegen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub fn emitConstant(self: *Self, location: Ast.TokenIndex, value: Value) !void {
}

pub fn makeConstant(self: *Self, value: Value) !u24 {
const constant: u24 = try self.current.?.function.?.chunk.addConstant(null, value);
const constant = try self.current.?.function.?.chunk.addConstant(null, value);
if (constant > Chunk.max_constants) {
self.reportError("Too many constants in one chunk.");
return 0;
Expand Down
13 changes: 11 additions & 2 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ pub fn parse(self: *Self, source: []const u8, file_name: []const u8) !?Ast {
}) |decl| {
var statements = std.ArrayList(Ast.Node.Index).fromOwnedSlice(
self.gc.allocator,
self.ast.nodes.items(.components)[body_node].Block,
@constCast(self.ast.nodes.items(.components)[body_node].Block),
);
defer statements.shrinkAndFree(statements.items.len);

Expand All @@ -869,7 +869,7 @@ pub fn parse(self: *Self, source: []const u8, file_name: []const u8) !?Ast {
}) |decl| {
var statements = std.ArrayList(Ast.Node.Index).fromOwnedSlice(
self.gc.allocator,
self.ast.nodes.items(.components)[body_node].Block,
@constCast(self.ast.nodes.items(.components)[body_node].Block),
);
defer statements.shrinkAndFree(statements.items.len);

Expand Down Expand Up @@ -4270,6 +4270,15 @@ fn dot(self: *Self, can_assign: bool, callee: Ast.Node.Index) Error!Ast.Node.Ind
const property_field = obj_def.fields.get(member_name);
var property_type = if (property_field) |field| field.type_def else null;

if (property_field != null and !property_field.?.static) {
self.reportErrorFmt(
.undefined,
"Static property `{s}` is not defined",
.{
self.ast.tokens.items(.lexeme)[member_name_token],
},
);
} else
// Not found, create a placeholder, this is a root placeholder not linked to anything
// TODO: test with something else than a name
if (property_type == null and self.current_object != null and std.mem.eql(
Expand Down
2 changes: 1 addition & 1 deletion src/Reporter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const io = @import("io.zig");

const Self = @This();

// Do not reorder whitout updating documentation, values are explicit so they can be retrieved easily
// Do not reorder without updating documentation, values are explicit so they can be retrieved easily
pub const Error = enum(u8) {
already_conforming_protocol = 0,
arguments_count = 1,
Expand Down
12 changes: 8 additions & 4 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2073,9 +2073,14 @@ pub const VM = struct {
return false;
}

inline fn repurposeFrame(self: *Self, closure: *ObjClosure, arg_count: u8, catch_value: ?Value) Error!void {
fn repurposeFrame(self: *Self, closure: *ObjClosure, arg_count: u8, catch_value: ?Value) Error!void {
// Is or will be JIT compiled, call and stop there
if (!is_wasm and self.current_fiber.parent_fiber == null and try self.compileAndCall(closure, arg_count, catch_value)) {
if (!is_wasm and self.current_fiber.parent_fiber == null and
try self.compileAndCall(
closure,
arg_count,
catch_value,
)) {
return;
}

Expand Down Expand Up @@ -2719,8 +2724,6 @@ pub const VM = struct {
fn OP_GET_ENUM_CASE(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void {
const enum_ = self.peek(0).obj().access(ObjEnum, .Enum, self.gc).?;

_ = self.pop();

var enum_case: *ObjEnumInstance = self.gc.allocateObject(
ObjEnumInstance,
ObjEnumInstance{
Expand All @@ -2732,6 +2735,7 @@ pub const VM = struct {
unreachable;
};

_ = self.pop();
self.push(Value.fromObj(enum_case.toObj()));

const next_full_instruction: u32 = self.readInstruction();
Expand Down

0 comments on commit 33061c5

Please sign in to comment.