Signed-off-by: erick-alcachofa <erick@artichoke.dev>
Implement TypeExpression AST node to allow types to be used within
expressions, enabling the parsing of anonymous slice and array
initializers like `[]Type { ... }`.
- Register `[` as a prefix-style token (NUD) in the Pratt parser.
- Add `TypeExpression` node to AST and expression variants.
- Update `toDot` and `toString` visitors for AST visualization.
- Update frontend to open source files directly to fix issues at opening
paths.
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.
Signed-off-by: erick-alcachofa <erick@artichoke.dev>
Overhaul the AST and parser logic to support explicit generic
instantiation in expressions (e.g., `Result::<u32, u32>::Ok(0)`). This
is achieved by implementing the "turbofish" operator (`::<>`) and
specializing how member and module access are handled.
* Added `GenericExpression` to represent generic instantiations in
expressions.
* Updated the Pratt parser to look for `<` immediately following a `::`
(ModuleAccess) operator. If found, it parses a `GenericExpression`
containing the generic arguments.
* This change resolves the ambiguity between generic lists and
comparison operators in the expression parser.
* Renamed `PointerAccessExpression` to `PointerMemberAccessExpression`.
* Refactored `MemberAccessExpression` and
`PointerMemberAccessExpression` to store the member as an
`ExpressionNode`. This allows the right-hand side of a `.` or `->` to
be a complex expression (like a generic call).
* Simplified `ModuleAccessExpression` to a binary `left`/`right`
structure, separating scope resolution from generic instantiation.
* Flattened the `Type` AST: replaced recursive `baseType` structures
with a `Vector<TypeExpressionNode>` (`typeNodes`) to represent
namespaced paths (e.g., `std::collections::Map`) more efficiently.
* Removed redundant `NamespacedType` and `NamespacedIdentifier` nodes.
* Simplified `GenericType` and `IdentifierType` to use direct `String`
type names.
* Refactored `parseType` to iterate through namespaced components and
populate the new flattened `typeNodes` vector.
* Updated the Pratt infix loop to correctly dispatch to `ModuleAccess`,
`MemberAccess`, or `GenericExpression` based on the operator and
lookahead tokens.
* Adjusted `toDot` and `toString` visitors to match the new AST
definitions.
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.
Signed-off-by: erick-alcachofa <erick@artichoke.dev>
This commit refactors several AST nodes to use `Optional<T>` for fields
that are not always present. This includes `attribute` in
`ReflectionExpression`, `elseBranch` in `IfStatement` and
`WhileStatement`, `defaultCase` in `MatchStatement` and
`SwitchStatement`, and `preLoop` and `postLoop` in `CForStatement`. This
change improves the robustness and clarity of the AST by explicitly
modeling optionality.
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.