6 Commits

Author SHA1 Message Date
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
5762497f56
feat(parser): implement Pratt expression parsing and refactor operator types
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

Overhaul the expression parsing mechanism to utilize a Pratt (top-down
operator precedence) parser. This change provides a more scalable and
maintainable way to handle operator precedence and associativity
compared to standard recursive descent.

As part of this transition, the nomenclature for operators has been
refined to reflect their position in the grammar (Prefix, Infix,
Postfix) rather than their arity.

* Renamed `UnaryOperator` and `UnaryExpression` to `PrefixOperator` and
  `PrefixExpression`.
* Renamed `BinaryOperator` and `BinaryExpression` to `InfixOperator` and
  `InfixExpression`.
* Renamed `ScopeAccessExpression` to `ModuleAccessExpression`.
* Introduced `PostfixOperator` enum and associated logic for function
  calls, slicing, and reflection attributes.
* Updated `toDot.cpp` and `toString.cpp` to support the new node types
  and renamed operators.

* Added `Pratt.hpp` and `Pratt.cpp` to define `BindingPower` and map
  operators to their respective precedence levels.
* Added `Operators.cpp` to handle token-to-operator mapping and
  classification (isPrefix, isInfix, isPostfix).
* Refactored `Parser::parseExpression` to implement the core Pratt loop
  using binding power comparisons.

* Moved literal parsing logic into a dedicated `Literals.cpp`.
* Implemented explicit parsing methods for `Integer`, `Float`, `Char`,
  `String`, `Boolean`, and `Null` literals.
* Added support for `this` and `_` (underscore) as identifier
  expressions.

* **Prefix**: `!`, `-`, `~`, `&` (MemPtr), `*` (DerefPtr).
* **Infix**: Arithmetic, Comparison, Bitwise, Logical, and all Compound
  Assignments.
* **Postfix**: `()` (Call), `[]` (Slice/Access), `.#` (Slice length),
  `.*` (Slice pointer), and `.@` (Reflection).

* **Missing Literals**: Struct literals and Array literals are not yet
  implemented in the new parsing flow.
* **Node Specialization**: `MemberAccess`, `PointerMemberAccess`, and
  `ModuleAccess` currently use generic infix logic and need to be
  migrated to their specific AST node types.
* **Error Handling**: Literal parsing (specifically `std::stold` and
  `std::stoul`) needs safety checks to prevent potential exceptions
  during conversion.
* **Diagnostics**: Refine the error message for unexpected tokens in
  postfix expressions to explicitly list supported operators.
* **Generic Ambiguity**: Generic type/function instantiation currently
  causes parsing conflicts with comparison operators (e.g., `Foo<T>`).
  This is a known issue that will be resolved by transitioning the
  grammar to a turbofish-style `::<...>` syntax.
2025-12-26 23:32:49 -06:00
b99f3586dc
chore(license): Added NOTICE header to all source files
Signed-off-by: erick-alcachofa <erick@artichoke.dev>
2025-12-25 13:12:41 -06:00
d979b10bee
feat(AST): Add Uninitialized and missing binary operators to common enums
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit adds `Uninitialized` to several enums in `Common.hpp` to
ensure they are properly initialized.

It also adds missing binary operators like `BitAnd`, `BitXor`,
`Adition`, and `Multiplication`. This change improves the robustness and
functionality of the AST parser.
2025-10-15 15:53:37 -06:00
5e94021ae5
feat(AST): Add node factory helper and missing identifier node
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit introduces an utility factory function and structural
improvements to the Abstract Syntax Tree (AST).

* Adds a new `ASTNodePtr` C++20 concept to constrain template types to
  be `std::unique_ptr`s pointing to AST nodes.
* Introduces a `MakeNode<T>()` factory function that uses this concept
  to simplify and standardize the creation of new nodes.
* Fixed `NamespacedType` and added the missing `NamespacedIdentifier`
  node.
2025-10-12 18:55:34 -06:00
c4c3d71cc4
feat(AST): Refactor AST nodes into a multi-file structure
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

This commit refactors the AST (Abstract Syntax Tree) to improve code
organization, clarity, and maintainability. The large single-file AST
definition has been split into multiple, logically grouped header files.

The key changes are:

- **New File Structure**: The single `Node.hpp` file is replaced by a
  modular structure consisting of `Common.hpp`, `Declarations.hpp`,
  `Expressions.hpp`, `Literals.hpp`, `Statements.hpp`, and a new central
  `AST.hpp` header.
- **Improved Naming**: All AST node structs and their aliases have been
  renamed to follow a consistent `[NodeName][NodeType]` convention, such
  as `StructDeclaration` and `StructDeclNode`.
- **Namespace Change**: The `node` namespace has been replaced by
  `arti::lang::ast::nodes` to provide better encapsulation and prevent
  naming conflicts.
- **Type Aliases**: Helper aliases like `String`, `Vector`, and
  `Variant` have been introduced to simplify the code.
2025-10-12 17:40:29 -06:00