erick-alcachofa 3180ca4662
feat(parser): implement object literals to unify struct and slice syntax
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

Implement support for object literals using a unified syntax for both
struct and slice initialization. Since the parser lacks the semantic
context to distinguish between a struct or a slice at this stage, both
are represented by the new `ObjectLiteral` AST node.

initialization within curly braces following a type expression:
* **Named Initializers**: Uses the `.field = value` syntax (e.g.,
  `Point { .x = 10, .y = 20 }`).
* **Positional Initializers**: Uses a comma-separated list of
  expressions (e.g., `[]i32 { 1, 2, 3 }`).

* Renamed `StructLiteral` and `SliceLiteral` nodes to `ObjectLiteral`.
* Refactored initialization helper nodes (e.g.,
  `StructLiteralNamedFieldInit` is now `ObjectLiteralNamedFieldInit`).
* Unified the representation in `Expressions.hpp` and `Literals.hpp` to
  use a single `ObjectLiteral` struct containing a `type` and an
  optional `initializer`.

* Integrated the opening brace `{` (`opLSquirly`) as a high-precedence
  postfix operator (binding power 19).
* Implemented parsing logic in `Expressions.cpp` to handle the
  transition from a type expression to an object initializer.
* Updated `toDot` and `toString` visitors to handle the unified
  `ObjectLiteral` nodes and their respective initializer variants.

* Improved robustness in `Declarations.cpp` by ensuring list parsing
  correctly handles closing braces in specific edge cases.
2025-12-28 00:12:43 -06:00

113 lines
3.7 KiB
C++

//============================================================================//
// //
// artichoke programming language //
// //
// Copyright (C) 2025 Erick Saul Guzman Ramos, whoami.artichoke.dev //
// //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as published //
// by the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
// //
//============================================================================//
#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Types.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct CharLiteral;
struct NullLiteral;
struct StringLiteral;
struct FloatLiteral;
struct IntegerLiteral;
struct BooleanLiteral;
struct ObjectLiteral;
/* Helper declaration node types */
struct ObjectLiteralNamedFieldInit;
struct ObjectLiteralNamedInitializer;
struct ObjectLiteralPositionalInitializer;
} // namespace nodes
/* Public Aliases */
using CharLtrlNode = Ptr<nodes::CharLiteral>;
using NullLtrlNode = Ptr<nodes::NullLiteral>;
using StringLtrlNode = Ptr<nodes::StringLiteral>;
using FloatLtrlNode = Ptr<nodes::FloatLiteral>;
using IntegerLtrlNode = Ptr<nodes::IntegerLiteral>;
using BooleanLtrlNode = Ptr<nodes::BooleanLiteral>;
using ObjectLtrlNode = Ptr<nodes::ObjectLiteral>;
using ObjectLtrlNamedFieldInitNode =
Ptr<nodes::ObjectLiteralNamedFieldInit>;
using ObjectLtrlNamedInitializerNode =
Ptr<nodes::ObjectLiteralNamedInitializer>;
using ObjectLtrlPositionalInitializerNode =
Ptr<nodes::ObjectLiteralPositionalInitializer>;
/* Variant nodes */
using ObjectLtrlInitializerNode = Variant<
ObjectLtrlNamedInitializerNode,
ObjectLtrlPositionalInitializerNode
>;
/* Node definitions */
struct nodes::CharLiteral {
SourceLocation location;
uint8_t value;
};
struct nodes::NullLiteral {
SourceLocation location;
};
struct nodes::StringLiteral {
SourceLocation location;
String value;
};
struct nodes::FloatLiteral {
SourceLocation location;
double value;
};
struct nodes::IntegerLiteral {
SourceLocation location;
uint64_t value;
};
struct nodes::BooleanLiteral {
SourceLocation location;
Boolean value;
};
/* INFO: Helper types definitions are on Expressions.hpp
* due to dependency in ExpressionNode variant. */
} // namespace arti::lang::ast