Skip to content

Commit 8ce87ad

Browse files
authored
Add element type propagation for anonymous struct literals in ArrayLiteral (#69)
Co-authored-by: io-eric <io-eric@users.noreply.github.com>
1 parent 7660b68 commit 8ce87ad

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

src/ast/component.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ std::string Component::to_webcc(CompilerSession &session)
561561
{
562562
std::string elem_type = var->type.substr(0, var->type.length() - 2);
563563

564+
// Propagate element type to anonymous struct literals
565+
arr_lit->propagate_element_type(elem_type);
566+
564567
// Component state arrays with T[] type: always use webcc::vector (even if not mut).
565568
//
566569
// WHY NOT USE FIXED ARRAYS HERE?

src/ast/expressions.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,19 @@ bool ArrayLiteral::is_static() {
773773
return true;
774774
}
775775

776+
void ArrayLiteral::propagate_element_type(const std::string& type) {
777+
element_type = type;
778+
for (auto& elem : elements) {
779+
// If this is an anonymous struct literal (ComponentConstruction with empty name),
780+
// fill in the type from the array's element type
781+
if (auto comp = dynamic_cast<ComponentConstruction*>(elem.get())) {
782+
if (comp->component_name.empty()) {
783+
comp->component_name = type;
784+
}
785+
}
786+
}
787+
}
788+
776789
std::string ArrayRepeatLiteral::to_webcc() {
777790
// Generate initialization - webcc::array constructor will fill with the value
778791
// The actual array type and initialization is handled by VarDeclaration::to_webcc

src/ast/expressions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ struct ArrayLiteral : Expression {
154154
std::string to_webcc() override;
155155
void collect_dependencies(std::set<std::string>& deps) override;
156156
bool is_static() override;
157+
158+
// Propagate element type to anonymous struct literals (ComponentConstruction with empty name)
159+
void propagate_element_type(const std::string& type);
157160
};
158161

159162
// Fixed-size array repeat initializer: [value; count] e.g., [0; 100] or [0; NUM_ITEMS]

src/ast/statements.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ std::string VarDeclaration::to_webcc()
7373
{
7474
std::string elem_type = type.substr(0, type.length() - 2);
7575

76+
// Propagate element type to anonymous struct literals
77+
arr_lit->propagate_element_type(elem_type);
78+
7679
// Optimization: If immutable and initialized with literal, use fixed-size array
7780
// No need for dynamic allocation if we know the size at compile time and can't modify
7881
if (!is_mutable)

src/frontend/parser/expr.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ std::unique_ptr<Expression> Parser::parse_primary()
255255
return std::make_unique<BoolLiteral>(false);
256256
}
257257

258+
// Anonymous struct literal: { field = value, ... }
259+
// Type is inferred from context (e.g., array element type)
260+
if (current().type == TokenType::LBRACE && allow_brace_init)
261+
{
262+
advance();
263+
auto parsed_args = parse_call_args(TokenType::RBRACE);
264+
expect(TokenType::RBRACE, "Expected '}'");
265+
266+
// Create ComponentConstruction with empty name - type will be inferred
267+
auto data_expr = std::make_unique<ComponentConstruction>("");
268+
data_expr->args = std::move(parsed_args);
269+
return data_expr;
270+
}
271+
258272
// Identifer or function call (also allow 'key' and 'data' keywords as identifier)
259273
if (is_identifier_token())
260274
{
@@ -372,6 +386,9 @@ std::unique_ptr<Expression> Parser::parse_primary()
372386
return std::make_unique<ArrayLiteral>();
373387
}
374388

389+
// Allow { field = value } syntax for anonymous struct literals in arrays
390+
allow_brace_init = true;
391+
375392
// Parse first expression
376393
auto first_expr = parse_expression();
377394

0 commit comments

Comments
 (0)