Skip to content

Commit

Permalink
feat: Infer empty list/map type in variable declaration
Browse files Browse the repository at this point in the history
closes #86
  • Loading branch information
giann committed Feb 1, 2024
1 parent 6ee8eac commit 246f603
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6458,7 +6458,7 @@ fn varDeclaration(
else
try self.gc.type_registry.getTypeDef(
.{
.def_type = .Void,
.def_type = .Any,
},
);

Expand Down Expand Up @@ -6509,6 +6509,22 @@ fn varDeclaration(
);
}

if (value) |uvalue| {
const tags = self.ast.nodes.items(.tag);
const components = self.ast.nodes.items(.components);
const parsed_type_def = if (parsed_type) |pt| self.ast.nodes.items(.type_def)[pt] else null;

// [T] variable = [] -> [T] variable = [<T>];
if (parsed_type_def != null and parsed_type_def.?.def_type == .List and tags[uvalue] == .List and components[uvalue].List.explicit_item_type == null and components[uvalue].List.items.len == 0) {
self.ast.nodes.items(.type_def)[uvalue] = parsed_type_def.?;
}

// {K: V} variable = {} -> {K: V} variable = [<K: V>];
if (parsed_type_def != null and parsed_type_def.?.def_type == .Map and tags[uvalue] == .Map and components[uvalue].Map.explicit_key_type == null and components[uvalue].Map.explicit_value_type == null and components[uvalue].Map.entries.len == 0) {
self.ast.nodes.items(.type_def)[uvalue] = parsed_type_def.?;
}
}

switch (terminator) {
.OptComma => _ = try self.match(.Comma),
.Comma => try self.consume(.Comma, "Expected `,` after variable declaration."),
Expand Down

0 comments on commit 246f603

Please sign in to comment.