Skip to content

Commit

Permalink
feat: Anonymous objects can omit property name when initial value is …
Browse files Browse the repository at this point in the history
…a named variable
  • Loading branch information
giann committed Jun 3, 2024
1 parent c86651c commit 5918881
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 180 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Modified
- Enum can now have `rg`, `ud`, `void`, `pat` has value type
- `var` can be used for key and/or value type in `for` and `foreach` loop
- Anonymous object can also omit property name when initial value is a named variable

## Fixed
- Type checking was not done on object instance property assignments
Expand Down
33 changes: 25 additions & 8 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3888,16 +3888,33 @@ fn anonymousObjectInit(self: *Self, _: bool) Error!Ast.Node.Index {
}
try property_names.put(property_name_lexeme, property_name);

try self.consume(.Equal, "Expected `=` after property name.");
// Named variable with the same name as property
const expr = if (self.check(.Comma) or self.check(.RightBrace)) named: {
const expr = try self.expression(true);

const expr = try self.expression(false);
try properties.append(
.{
.name = property_name,
.value = expr,
},
);

break :named expr;
} else regular: {
try self.consume(.Equal, "Expected `=` after property name.");

const expr = try self.expression(false);

try properties.append(
.{
.name = property_name,
.value = expr,
},
);

break :regular expr;
};

try properties.append(
.{
.name = property_name,
.value = expr,
},
);
try object_type.resolved_type.?.Object.fields.put(
property_name_lexeme,
.{
Expand Down
Loading

0 comments on commit 5918881

Please sign in to comment.