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

149 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 <cstddef>
#include <memory>
#include <vector>
#include <string>
#include <variant>
#include <optional>
namespace arti::lang::ast {
struct SourceLocation {
std::size_t line;
std::size_t column;
};
enum class Mutability {
Uninitialized,
Mutable,
Constant,
};
enum class TypeQualifier {
Uninitialized,
Pointer,
Slice,
Mutable,
Optional,
};
enum class PrefixOperator {
Uninitialized,
Not,
Minus,
BitNot,
MemPtr,
DerefPtr,
};
enum class InfixOperator {
Uninitialized,
Modulo,
Addition,
Substraction,
Division,
Multiplication,
Equal,
NotEqual,
LessThan,
GreaterThan,
LessEqual,
GreaterEqual,
LeftShift,
RightShift,
BoolAnd,
BoolOr,
BitAnd,
BitOr,
BitXor,
Assignment,
ModuleAccess,
MemberAccess,
PointerMemberAccess,
AdditionAssignment,
SubstractionAssignment,
MultiplicationAssignment,
DivisionAssignment,
ModuloAssignment,
BitAndAssignment,
BitOrAssignment,
LShiftAssignment,
RShiftAssignment,
BoolAndAssignment,
BoolOrAssignment,
};
enum class PostfixOperator {
Uninitialized,
FunctionCall,
SliceAccess,
SliceSize,
PtrToSlice,
SliceToPtr,
Reflect,
ObjectLiteral,
};
enum class CompoundAssignOperator {
Uninitialized,
Addition,
Substraction,
Multiplication,
Division,
Modulo,
BitAnd,
BitOr,
LeftShift,
RightShift,
BoolAnd,
BoolOr,
};
/* Alising of types for consistency */
using Boolean = bool;
using String = std::string;
template <typename T>
using Ptr = std::unique_ptr<T>;
template <typename T>
using Optional = std::optional<T>;
template <typename... T>
using Variant = std::variant<T...>;
template <typename T>
using Vector = std::vector<T>;
template <typename Node>
concept ASTNodePtr = requires {
typename Node::element_type;
requires std::is_same_v<std::unique_ptr<typename Node::element_type>, Node>;
};
} // namespace arti::lang::ast