erick-alcachofa c2f37d5702
feat(parser): add support for type-initiated expressions
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.
2025-12-28 10:21:24 -06:00

246 lines
6.8 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/Literals.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct IdentifierExpression;
struct PrefixExpression;
struct InfixExpression;
struct AssignExpression;
struct CompoundAssignExpression;
struct FunctionCallExpression;
struct SliceAccessExpression;
struct SliceRangeExpression;
struct MemberAccessExpression;
struct PointerMemberAccessExpression;
struct GenericExpression;
struct ModuleAccessExpression;
struct ReflectionExpression;
struct SliceCreationExpression;
struct SliceLengthExpression;
struct SlicePtrExpression;
struct TypeExpression;
} // namespace nodes
/* Public Aliases */
using IdentifierExprNode = Ptr<nodes::IdentifierExpression>;
using PrefixExprNode = Ptr<nodes::PrefixExpression>;
using InfixExprNode = Ptr<nodes::InfixExpression>;
using AssignExprNode = Ptr<nodes::AssignExpression>;
using CompoundAssignExprNode = Ptr<nodes::CompoundAssignExpression>;
using FunctionCallExprNode = Ptr<nodes::FunctionCallExpression>;
using SliceAccessExprNode = Ptr<nodes::SliceAccessExpression>;
using SliceRangeExprNode = Ptr<nodes::SliceRangeExpression>;
using MemberAccessExprNode = Ptr<nodes::MemberAccessExpression>;
using PointerMemberAccessExprNode = Ptr<nodes::PointerMemberAccessExpression>;
using GenericExprNode = Ptr<nodes::GenericExpression>;
using ModuleAccessExprNode = Ptr<nodes::ModuleAccessExpression>;
using ReflectionExprNode = Ptr<nodes::ReflectionExpression>;
using SliceCreationExprNode = Ptr<nodes::SliceCreationExpression>;
using SliceLengthExprNode = Ptr<nodes::SliceLengthExpression>;
using SlicePtrExprNode = Ptr<nodes::SlicePtrExpression>;
using TypeExprNode = Ptr<nodes::TypeExpression>;
/* Variant nodes */
using ExpressionNode = Variant<
CharLtrlNode,
NullLtrlNode,
StringLtrlNode,
FloatLtrlNode,
IntegerLtrlNode,
BooleanLtrlNode,
ObjectLtrlNode,
IdentifierExprNode,
PrefixExprNode,
InfixExprNode,
AssignExprNode,
CompoundAssignExprNode,
FunctionCallExprNode,
SliceAccessExprNode,
SliceRangeExprNode,
MemberAccessExprNode,
PointerMemberAccessExprNode,
GenericExprNode,
ModuleAccessExprNode,
SliceCreationExprNode,
SliceLengthExprNode,
SlicePtrExprNode,
ReflectionExprNode,
TypeExprNode
>;
/* Node definitions */
struct nodes::IdentifierExpression {
SourceLocation location;
String identifierName;
};
struct nodes::PrefixExpression {
SourceLocation location;
PrefixOperator op;
ExpressionNode right;
};
struct nodes::InfixExpression {
SourceLocation location;
InfixOperator op;
ExpressionNode left;
ExpressionNode right;
};
struct nodes::AssignExpression {
SourceLocation location;
ExpressionNode left;
ExpressionNode right;
};
struct nodes::CompoundAssignExpression {
SourceLocation location;
CompoundAssignOperator op;
ExpressionNode left;
ExpressionNode right;
};
struct nodes::FunctionCallExpression {
SourceLocation location;
ExpressionNode callee;
Vector<ExpressionNode> arguments;
};
struct nodes::SliceAccessExpression {
SourceLocation location;
ExpressionNode slice;
ExpressionNode index;
};
struct nodes::SliceRangeExpression {
SourceLocation location;
ExpressionNode slice;
Optional<ExpressionNode> start;
Optional<ExpressionNode> end;
};
struct nodes::MemberAccessExpression {
SourceLocation location;
ExpressionNode member;
ExpressionNode object;
};
struct nodes::PointerMemberAccessExpression {
SourceLocation location;
ExpressionNode member;
ExpressionNode object;
};
struct nodes::ModuleAccessExpression {
SourceLocation location;
ExpressionNode left;
ExpressionNode right;
};
struct nodes::GenericExpression {
SourceLocation location;
ExpressionNode typeNode;
std::vector<TypeNode> genericArgs;
};
struct nodes::ReflectionExpression {
SourceLocation location;
Optional<String> attribute;
ExpressionNode object;
};
struct nodes::SliceCreationExpression {
SourceLocation location;
ExpressionNode object;
ExpressionNode length;
};
struct nodes::SliceLengthExpression {
SourceLocation location;
ExpressionNode object;
};
struct nodes::SlicePtrExpression {
SourceLocation location;
ExpressionNode object;
};
struct nodes::ObjectLiteralNamedFieldInit {
SourceLocation location;
String fieldName;
ExpressionNode fieldValue;
};
struct nodes::ObjectLiteralNamedInitializer {
SourceLocation location;
Vector<ObjectLtrlNamedFieldInitNode> fields;
};
struct nodes::ObjectLiteralPositionalInitializer {
SourceLocation location;
Vector<ExpressionNode> fields;
};
struct nodes::ObjectLiteral {
SourceLocation location;
ExpressionNode type;
Optional<ObjectLtrlInitializerNode> initializer;
};
struct nodes::TypeExpression {
SourceLocation location;
TypeNode type;
};
} // namespace arti::lang::ast