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.
This commit is contained in:
erick-alcachofa 2025-10-12 17:40:29 -06:00
parent 9dcd5490e3
commit c4c3d71cc4
Signed by: me
GPG Key ID: 6FA5F8643444BAFA
8 changed files with 857 additions and 580 deletions

View File

@ -0,0 +1,26 @@
#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Declarations.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct CompilationUnit;
} // namespace nodes
/* Public Aliases */
using CompilationUnitNode = Ptr<nodes::CompilationUnit>;
using AST = CompilationUnitNode;
/* Node definitions */
struct nodes::CompilationUnit {
String unitName;
Vector<TopLevelDeclNode> declarations;
};
} // namespace arti::lang::ast

View File

@ -0,0 +1,77 @@
#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 {
Mutable,
Constant,
};
enum class TypeQualifier {
Pointer,
Slice,
Mutable,
Optional,
};
enum class UnaryOperator {
Not,
Minus,
BitNot,
Ampersand,
Star,
};
enum class BinaryOperator {
Equal,
NotEqual,
GreaterThan,
LessThan,
GreaterEqual,
LessEqual,
};
enum class CompoundAssignOperator {
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>;
} // namespace arti::lang::ast

View File

@ -0,0 +1,139 @@
#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Types.hpp>
#include <artichoke/Parser/AST/Statements.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct ModuleDeclaration;
struct StructDeclaration;
struct EnumDeclaration;
struct FunctionDeclaration;
struct ImportDeclaration;
struct AliasDeclaration;
/* Helper declaration node types */
struct EnumMember;
struct StructMember;
struct GenericParam;
struct FunctionParam;
} // namespace nodes
/* Public Aliases */
using ModuleDeclNode = Ptr<nodes::ModuleDeclaration>;
using StructDeclNode = Ptr<nodes::StructDeclaration>;
using EnumDeclNode = Ptr<nodes::EnumDeclaration>;
using FunctionDeclNode = Ptr<nodes::FunctionDeclaration>;
using ImportDeclNode = Ptr<nodes::ImportDeclaration>;
using AliasDeclNode = Ptr<nodes::AliasDeclaration>;
using GenericParamNode = Ptr<nodes::GenericParam>;
using FunctionParamNode = Ptr<nodes::FunctionParam>;
using EnumMemberNode = Ptr<nodes::EnumMember>;
using StructMemberNode = Ptr<nodes::StructMember>;
/* Variant nodes */
using TopLevelDeclNode = Variant<
ModuleDeclNode,
StructDeclNode,
EnumDeclNode,
FunctionDeclNode,
ImportDeclNode,
AliasDeclNode
>;
using ModuleInnerDeclNode = Variant<
StructDeclNode,
EnumDeclNode,
FunctionDeclNode
>;
/* Node definitions */
struct nodes::ModuleDeclaration {
SourceLocation location;
Boolean isExported;
NamespacedIdentifierNode name;
Vector<ModuleDeclNode> childModules;
Vector<ModuleInnerDeclNode> innerDeclarations;
Vector<AliasDeclNode> aliasDeclarations;
};
struct nodes::StructDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Vector<GenericParamNode> genericParams;
Vector<StructMemberNode> structMembers;
};
struct nodes::EnumDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Vector<GenericParamNode> genericParams;
Vector<EnumMemberNode> enumMembers;
};
struct nodes::FunctionDeclaration {
SourceLocation location;
Boolean isExported;
String name;
Optional<TypeNode> returnType;
Vector<GenericParamNode> genericParams;
Vector<FunctionParamNode> functionParams;
CodeBlockStmtNode functionBody;
};
struct nodes::ImportDeclaration {
SourceLocation location;
Boolean importAll;
NamespacedIdentifierNode importTarget;
};
struct nodes::AliasDeclaration {
SourceLocation location;
String alias;
TypeNode target;
};
struct nodes::StructMember {
SourceLocation location;
String name;
TypeNode type;
};
struct nodes::EnumMember {
SourceLocation location;
String name;
Optional<TypeNode> type;
};
struct nodes::GenericParam {
SourceLocation location;
String name;
};
struct nodes::FunctionParam {
SourceLocation location;
Boolean isThis;
String name;
TypeNode type;
};
} // namespace arti::lang::ast

View File

@ -0,0 +1,205 @@
#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 UnaryExpression;
struct BinaryExpression;
struct AssignExpression;
struct CompoundAssignExpression;
struct FunctionCallExpression;
struct SliceAccessExpression;
struct SliceRangeExpression;
struct MemberAccessExpression;
struct PointerAccessExpression;
struct ScopeAccessExpression;
struct ReflectionExpression;
struct SliceCreationExpression;
struct SliceLengthExpression;
struct SlicePtrExpression;
} // namespace nodes
/* Public Aliases */
using IdentifierExprNode = Ptr<nodes::IdentifierExpression>;
using UnaryExprNode = Ptr<nodes::UnaryExpression>;
using BinaryExprNode = Ptr<nodes::BinaryExpression>;
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 PointerAccessExprNode = Ptr<nodes::PointerAccessExpression>;
using ScopeAccessExprNode = Ptr<nodes::ScopeAccessExpression>;
using ReflectionExprNode = Ptr<nodes::ReflectionExpression>;
using SliceCreationExprNode = Ptr<nodes::SliceCreationExpression>;
using SliceLengthExprNode = Ptr<nodes::SliceLengthExpression>;
using SlicePtrExprNode = Ptr<nodes::SlicePtrExpression>;
/* Variant nodes */
using ExpressionNode = Variant<
CharLtrlNode,
NullLtrlNode,
StringLtrlNode,
FloatLtrlNode,
IntegerLtrlNode,
BooleanLtrlNode,
StructLtrlNode,
SliceLtrlNode,
IdentifierExprNode,
UnaryExprNode,
BinaryExprNode,
AssignExprNode,
CompoundAssignExprNode,
FunctionCallExprNode,
SliceAccessExprNode,
SliceRangeExprNode,
MemberAccessExprNode,
PointerAccessExprNode,
ScopeAccessExprNode,
SliceCreationExprNode,
SliceLengthExprNode,
SlicePtrExprNode,
ReflectionExprNode
>;
/* Node definitions */
struct nodes::IdentifierExpression {
SourceLocation location;
String identifierName;
};
struct nodes::UnaryExpression {
SourceLocation location;
UnaryOperator op;
ExpressionNode right;
};
struct nodes::BinaryExpression {
SourceLocation location;
BinaryOperator 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;
String memberName;
ExpressionNode object;
};
struct nodes::PointerAccessExpression {
SourceLocation location;
String memberName;
ExpressionNode object;
};
struct nodes::ScopeAccessExpression {
SourceLocation location;
String memberName;
ExpressionNode scope;
Vector<TypeNode> genericParams;
};
struct nodes::ReflectionExpression {
SourceLocation location;
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::StructLiteralNamedFieldInit {
SourceLocation location;
String fieldName;
ExpressionNode fieldValue;
};
struct nodes::StructLiteralPositionalInit {
SourceLocation location;
ExpressionNode fieldValue;
};
struct nodes::StructLiteralNamedInitializer {
SourceLocation location;
Vector<StructLtrlNamedFieldInitNode> fields;
};
struct nodes::StructLiteralPositionalInitializer {
SourceLocation location;
Vector<StructLtrlPositionalInitNode> fields;
};
} // namespace arti::lang::ast

View File

@ -0,0 +1,109 @@
#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 StructLiteral;
struct SliceLiteral;
/* Helper declaration node types */
struct StructLiteralNamedFieldInit;
struct StructLiteralPositionalInit;
struct StructLiteralNamedInitializer;
struct StructLiteralPositionalInitializer;
} // 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 StructLtrlNode = Ptr<nodes::StructLiteral>;
using SliceLtrlNode = Ptr<nodes::SliceLiteral>;
using StructLtrlNamedFieldInitNode =
Ptr<nodes::StructLiteralNamedFieldInit>;
using StructLtrlPositionalInitNode =
Ptr<nodes::StructLiteralPositionalInit>;
using StructLtrlNamedInitializerNode =
Ptr<nodes::StructLiteralNamedInitializer>;
using StructLtrlPositionalInitializerNode =
Ptr<nodes::StructLiteralPositionalInitializer>;
/* Variant nodes */
using StructLtrlInitializerNode = Variant<
StructLtrlNamedInitializerNode,
StructLtrlPositionalInitializerNode
>;
/* 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;
};
struct nodes::StructLiteral {
SourceLocation location;
TypeNode type;
Optional<StructLtrlInitializerNode> initializer;
};
struct nodes::SliceLiteral {
SourceLocation location;
TypeNode type;
Optional<StructLtrlPositionalInitializerNode> initializer;
};
/* INFO: Helper types definitions are on Expressions.hpp
* due to dependency in ExpressionNode variant. */
} // namespace arti::lang::ast

View File

@ -1,580 +0,0 @@
#pragma once
#include <vector>
#include <memory>
#include <variant>
#include <optional>
namespace arti::lang::ast {
struct SourceLocation {
std::size_t line;
std::size_t column;
};
namespace node {
template <typename T>
using Ptr = std::unique_ptr<T>;
template <typename T>
using Opt = std::optional<T>;
enum class Mutability {
Mutable,
Constant,
};
enum class TypeQualifier {
Pointer,
Slice,
Mutable,
Optional,
};
enum class UnaryOperator {
Not,
Minus,
BitNot,
Ampersand,
Star,
};
enum class BinaryOperator {
Equal,
NotEqual,
GreaterThan,
LessThan,
GreaterEqual,
LessEqual,
};
enum class CompoundAssignType {
Addition,
Substraction,
Multiplication,
Division,
Modulo,
BitAnd,
BitOr,
LeftShift,
RightShift,
BoolAnd,
BoolOr,
};
/* -------- Nodes Forward Declarations -------- */
struct Module;
struct Struct;
struct Enum;
struct Function;
struct ImportStatement;
struct AliasStatement;
struct GenericParam;
struct FunctionParam;
struct StructMember;
struct EnumMember;
struct CodeBlock;
struct VariableDeclaration;
struct IfStatement;
struct ElseStatement;
struct DeferStatement;
struct ErrDeferStatement;
struct ReturnStatement;
struct BreakStatement;
struct ContinueStatement;
struct MatchStatement;
struct SwitchStatement;
struct CForStatement;
struct RangeForStatement;
struct WhileStatement;
struct DoWhileStatement;
struct InfLoopStatement;
struct ExpressionStatement;
struct MatchCase;
struct SwitchCase;
struct Type;
struct SimpleTypeExpression;
struct GenericTypeExpression;
struct AccessTypeExpression;
struct NamespacedIdentifier;
struct UnaryExpression;
struct BinaryExpression;
struct AssignExpression;
struct CompoundAssignExpression;
struct FunctionCallExpression;
struct SliceAccessExpression;
struct SliceRangeExpression;
struct MemberAccessExpression;
struct ScopeAccessExpression;
struct PointerAccessExpression;
struct ReflectionExpression;
struct SliceCreationExpression;
struct SliceLengthExpression;
struct SlicePtrExpression;
struct IdentifierExpression;
struct StructNamedInit;
struct StructNamedFieldsInit;
struct StructExpressionsInit;
struct CharLiteral;
struct NullLiteral;
struct StringLiteral;
struct FloatLiteral;
struct IntegerLiteral;
struct BooleanLiteral;
struct StructLiteral;
/* -------- Nodes Definitions -------- */
/* Top Level & Declarations */
using Declaration = std::variant<
Ptr<Struct>,
Ptr<Enum>,
Ptr<Function>>;
using TopLevelDeclaration = std::variant<
Ptr<Module>,
Ptr<Struct>,
Ptr<Enum>,
Ptr<Function>,
Ptr<ImportStatement>,
Ptr<AliasStatement>>;
struct Module {
SourceLocation location;
bool isExported;
Ptr<NamespacedIdentifier> name;
std::vector<Ptr<Module>> childModules;
std::vector<Declaration> declarations;
std::vector<Ptr<AliasStatement>> aliasedElements;
};
struct ImportStatement {
SourceLocation location;
bool importAll;
Ptr<NamespacedIdentifier> target;
};
struct AliasStatement {
SourceLocation location;
std::string name;
Ptr<NamespacedIdentifier> aliased;
};
struct Function {
SourceLocation location;
bool isExported;
std::string name;
Ptr<Type> returnType;
Ptr<CodeBlock> body;
std::vector<Ptr<GenericParam>> generics;
std::vector<Ptr<FunctionParam>> params;
};
struct Struct {
SourceLocation location;
bool isExported;
std::string name;
std::vector<Ptr<GenericParam>> generics;
std::vector<Ptr<StructMember>> members;
};
struct Enum {
SourceLocation location;
bool isExported;
std::string name;
std::vector<Ptr<GenericParam>> generics;
std::vector<Ptr<EnumMember>> members;
};
struct GenericParam {
SourceLocation location;
std::string name;
};
struct FunctionParam {
SourceLocation location;
bool isThis;
std::string name;
Ptr<Type> type;
};
struct StructMember {
SourceLocation location;
std::string name;
Ptr<Type> type;
};
struct EnumMember {
SourceLocation location;
std::string name;
Ptr<Type> type;
};
/* Expression node types */
using Expression = std::variant<
Ptr<CharLiteral>,
Ptr<NullLiteral>,
Ptr<StringLiteral>,
Ptr<FloatLiteral>,
Ptr<IntegerLiteral>,
Ptr<BooleanLiteral>,
Ptr<StructLiteral>,
Ptr<IdentifierExpression>,
Ptr<UnaryExpression>,
Ptr<BinaryExpression>,
Ptr<AssignExpression>,
Ptr<CompoundAssignExpression>,
Ptr<FunctionCallExpression>,
Ptr<SliceAccessExpression>,
Ptr<SliceRangeExpression>,
Ptr<MemberAccessExpression>,
Ptr<ScopeAccessExpression>,
Ptr<PointerAccessExpression>,
Ptr<ReflectionExpression>,
Ptr<SliceCreationExpression>,
Ptr<SliceLengthExpression>,
Ptr<SlicePtrExpression>>;
using StructLiteralFields = std::variant<
Ptr<StructNamedFieldsInit>,
Ptr<StructExpressionsInit>>;
struct CharLiteral {
SourceLocation location;
uint8_t value;
};
struct NullLiteral {
SourceLocation location;
};
struct StringLiteral {
SourceLocation location;
std::string value;
};
struct FloatLiteral {
SourceLocation location;
double value;
};
struct IntegerLiteral {
SourceLocation location;
uint64_t value;
};
struct BooleanLiteral {
SourceLocation location;
bool value;
};
struct StructLiteral {
SourceLocation location;
Ptr<Type> type;
StructLiteralFields fields;
};
struct StructNamedInit {
SourceLocation location;
std::string name;
Ptr<Expression> initializer;
};
struct StructNamedFieldsInit {
std::vector<StructNamedInit> fields;
};
struct StructExpressionsInit {
std::vector<SourceLocation> locations;
std::vector<Ptr<Expression>> initializers;
};
struct IdentifierExpression {
SourceLocation location;
std::string name;
};
struct UnaryExpression {
SourceLocation location;
UnaryOperator operatorT;
Ptr<Expression> right;
};
struct BinaryExpression {
SourceLocation location;
BinaryOperator operatorT;
Ptr<Expression> leftHs;
Ptr<Expression> rightHs;
};
struct AssignExpression {
SourceLocation location;
Ptr<Expression> leftHs;
Ptr<Expression> rightHs;
};
struct CompoundAssignExpression {
SourceLocation location;
CompoundAssignType type;
Ptr<Expression> leftHs;
Ptr<Expression> rightHs;
};
struct FunctionCallExpression {
SourceLocation location;
Ptr<Expression> callee;
std::vector<Ptr<Expression>> arguments;
};
struct SliceAccessExpression {
SourceLocation location;
Ptr<Expression> slice;
Ptr<Expression> index;
};
struct SliceRangeExpression {
SourceLocation location;
Ptr<Expression> slice;
Ptr<Expression> start;
Ptr<Expression> end;
};
struct MemberAccessExpression {
SourceLocation location;
Ptr<Expression> object;
std::string memberName;
};
struct ScopeAccessExpression {
SourceLocation location;
Ptr<Expression> object;
std::string memberName;
std::vector<Ptr<Type>> genericParams;
};
struct PointerAccessExpression {
SourceLocation location;
Ptr<Expression> object;
std::string memberName;
};
struct ReflectionExpression {
SourceLocation location;
Ptr<Expression> object;
Opt<std::string> attribute;
};
struct SliceCreationExpression {
SourceLocation location;
Ptr<Expression> object;
Ptr<Expression> length;
};
struct SliceLengthExpression {
SourceLocation location;
Ptr<Expression> object;
};
struct SlicePtrExpression {
SourceLocation location;
Ptr<Expression> object;
};
/* Statements */
using Statement = std::variant<
Ptr<VariableDeclaration>,
Ptr<IfStatement>,
Ptr<DeferStatement>,
Ptr<ErrDeferStatement>,
Ptr<ReturnStatement>,
Ptr<BreakStatement>,
Ptr<ContinueStatement>,
Ptr<AliasStatement>,
Ptr<MatchStatement>,
Ptr<SwitchStatement>,
Ptr<CForStatement>,
Ptr<RangeForStatement>,
Ptr<WhileStatement>,
Ptr<DoWhileStatement>,
Ptr<InfLoopStatement>,
Ptr<ExpressionStatement>>;
using ElseBranch = std::variant<
Ptr<ElseStatement>,
Ptr<IfStatement>>;
using Deferable = std::variant<
Ptr<Expression>,
Ptr<CodeBlock>>;
using PreLoopStatement = std::variant<
Ptr<VariableDeclaration>,
Ptr<Expression>>;
using TypeExpression = std::variant<
Ptr<SimpleTypeExpression>,
Ptr<GenericTypeExpression>,
Ptr<AccessTypeExpression>>;
struct CodeBlock {
SourceLocation location;
std::vector<Statement> statements;
};
struct VariableDeclaration {
SourceLocation location;
Mutability mutability;
std::string name;
Ptr<Type> type;
Ptr<Expression> initializer;
};
struct IfStatement {
SourceLocation location;
Opt<std::string> unwrappedVar;
Ptr<Expression> condition;
Ptr<CodeBlock> body;
Opt<ElseBranch> elseBranch;
};
struct ElseStatement {
SourceLocation location;
Opt<std::string> unwrappedVar;
Ptr<CodeBlock> body;
};
struct DeferStatement {
SourceLocation location;
Deferable body;
};
struct ErrDeferStatement {
SourceLocation location;
Deferable body;
};
struct ReturnStatement {
SourceLocation location;
Ptr<Expression> value;
};
struct BreakStatement {
SourceLocation location;
Opt<std::string> label;
};
struct ContinueStatement {
SourceLocation location;
Opt<std::string> label;
};
struct MatchStatement {
SourceLocation location;
Ptr<Expression> value;
std::vector<Ptr<MatchCase>> cases;
Ptr<CodeBlock> defaultCase;
};
struct SwitchStatement {
SourceLocation location;
Ptr<Expression> value;
std::vector<Ptr<SwitchCase>> cases;
Ptr<CodeBlock> defaultCase;
};
struct MatchCase {
SourceLocation location;
std::string identifier;
Ptr<Type> matchType;
Ptr<CodeBlock> body;
};
struct SwitchCase {
SourceLocation location;
Ptr<Expression> matchExpr;
Ptr<CodeBlock> body;
};
struct CForStatement {
SourceLocation location;
Ptr<PreLoopStatement> preLoop;
Ptr<Expression> condition;
Ptr<Expression> postLoop;
Ptr<CodeBlock> body;
};
struct RangeForStatement {
SourceLocation location;
Mutability varMutability;
std::string varName;
Ptr<Expression> range;
Ptr<CodeBlock> body;
};
struct WhileStatement {
SourceLocation location;
Opt<std::string> unwrappedVar;
Ptr<Expression> condition;
Ptr<CodeBlock> body;
Opt<ElseBranch> elseBranch;
};
struct DoWhileStatement {
SourceLocation location;
Ptr<CodeBlock> body;
Ptr<Expression> condition;
};
struct InfLoopStatement {
SourceLocation location;
Ptr<CodeBlock> body;
};
struct ExpressionStatement {
SourceLocation location;
Ptr<Expression> expression;
};
struct SimpleTypeExpression {
SourceLocation location;
Ptr<NamespacedIdentifier> name;
};
struct GenericTypeExpression {
SourceLocation location;
TypeExpression baseType;
std::vector<Ptr<Type>> genericArgs;
};
struct AccessTypeExpression {
SourceLocation location;
TypeExpression baseType;
std::string memberName;
};
struct Type {
SourceLocation location;
std::vector<TypeQualifier> qualifiers;
TypeExpression expression;
};
struct NamespacedIdentifier {
SourceLocation location;
std::vector<std::string> identifierParts;
};
} // namespace node
struct AST {
std::vector<node::TopLevelDeclaration> declarations;
};
} // namespace arti::lang::ast

View File

@ -0,0 +1,238 @@
#pragma once
#include <artichoke/Parser/AST/Common.hpp>
#include <artichoke/Parser/AST/Types.hpp>
#include <artichoke/Parser/AST/Expressions.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main declaration node types */
struct CodeBlockStatement;
struct VariableDeclStatement;
struct IfStatement;
struct ElseStatement;
struct DeferStatement;
struct ErrDeferStatement;
struct ReturnStatement;
struct BreakStatement;
struct ContinueStatement;
struct MatchStatement;
struct SwitchStatement;
struct CForStatement;
struct RangeForStatement;
struct WhileStatement;
struct DoWhileStatement;
struct InfLoopStatement;
struct ExpressionStatement;
/* Helper declaration node types */
struct MatchCase;
struct SwitchCase;
} // namespace nodes
/* Public Aliases */
using CodeBlockStmtNode = Ptr<nodes::CodeBlockStatement>;
using VariableStmtNode = Ptr<nodes::VariableDeclStatement>;
using IfStmtNode = Ptr<nodes::IfStatement>;
using ElseStmtNode = Ptr<nodes::ElseStatement>;
using DeferStmtNode = Ptr<nodes::DeferStatement>;
using ErrDeferStmtNode = Ptr<nodes::ErrDeferStatement>;
using ReturnStmtNode = Ptr<nodes::ReturnStatement>;
using BreakStmtNode = Ptr<nodes::BreakStatement>;
using ContinueStmtNode = Ptr<nodes::ContinueStatement>;
using MatchStmtNode = Ptr<nodes::MatchStatement>;
using SwitchStmtNode = Ptr<nodes::SwitchStatement>;
using CForStmtNode = Ptr<nodes::CForStatement>;
using RangeForStmtNode = Ptr<nodes::RangeForStatement>;
using WhileStmtNode = Ptr<nodes::WhileStatement>;
using DoWhileStmtNode = Ptr<nodes::DoWhileStatement>;
using InfLoopStmtNode = Ptr<nodes::InfLoopStatement>;
using ExpressionStmtNode = Ptr<nodes::ExpressionStatement>;
using MatchCaseNode = Ptr<nodes::MatchCase>;
using SwitchCaseNode = Ptr<nodes::SwitchCase>;
/* Variant nodes */
using StatementNode = Variant<
VariableStmtNode,
IfStmtNode,
DeferStmtNode,
ErrDeferStmtNode,
ReturnStmtNode,
BreakStmtNode,
ContinueStmtNode,
MatchStmtNode,
SwitchStmtNode,
CForStmtNode,
RangeForStmtNode,
WhileStmtNode,
DoWhileStmtNode,
InfLoopStmtNode,
ExpressionStmtNode
>;
using ElseBranchNode = Variant<
ElseStmtNode,
IfStmtNode
>;
using DeferableNode = Variant<
ExpressionStmtNode,
CodeBlockStmtNode
>;
using PreLoopStmtNode = Variant<
VariableStmtNode,
ExpressionStmtNode
>;
/* Node definitions */
struct nodes::CodeBlockStatement {
SourceLocation location;
Vector<StatementNode> statements;
};
struct nodes::VariableDeclStatement {
SourceLocation location;
String name;
Mutability mutability;
Optional<TypeNode> type;
Optional<ExpressionNode> initializer;
};
struct nodes::IfStatement {
SourceLocation location;
Optional<String> unwrappedVar;
ExpressionNode condition;
CodeBlockStmtNode body;
ElseBranchNode elseBranch;
};
struct nodes::ElseStatement {
SourceLocation location;
Optional<String> unwrappedVar;
CodeBlockStmtNode body;
};
struct nodes::DeferStatement {
SourceLocation location;
DeferableNode body;
};
struct nodes::ErrDeferStatement {
SourceLocation location;
DeferableNode body;
};
struct nodes::ReturnStatement {
SourceLocation location;
Optional<ExpressionNode> value;
};
struct nodes::BreakStatement {
SourceLocation location;
Optional<String> label;
};
struct nodes::ContinueStatement {
SourceLocation location;
Optional<String> label;
};
struct nodes::MatchStatement {
SourceLocation location;
ExpressionNode value;
Vector<MatchCaseNode> matchCases;
CodeBlockStmtNode defaultCase;
};
struct nodes::SwitchStatement {
SourceLocation location;
ExpressionNode value;
Vector<SwitchCaseNode> switchCases;
CodeBlockStmtNode defaultCase;
};
struct nodes::CForStatement {
SourceLocation location;
Optional<String> label;
PreLoopStmtNode preLoop;
ExpressionNode condition;
ExpressionNode postLoop;
CodeBlockStmtNode body;
};
struct nodes::RangeForStatement {
SourceLocation location;
Optional<String> label;
String varName;
Mutability varMutability;
ExpressionNode range;
CodeBlockStmtNode body;
};
struct nodes::WhileStatement {
SourceLocation location;
Optional<String> label;
Optional<String> unwrappedVar;
ExpressionNode condition;
CodeBlockStmtNode body;
ElseBranchNode elseBranch;
};
struct nodes::DoWhileStatement {
SourceLocation location;
Optional<String> label;
ExpressionNode condition;
CodeBlockStmtNode body;
};
struct nodes::InfLoopStatement {
SourceLocation location;
Optional<String> label;
CodeBlockStmtNode body;
};
struct nodes::ExpressionStatement {
SourceLocation location;
ExpressionNode expression;
};
struct nodes::MatchCase {
SourceLocation location;
TypeNode matchType;
Optional<String> unwrappedVar;
CodeBlockStmtNode body;
};
struct nodes::SwitchCase {
SourceLocation location;
ExpressionNode matchExpr;
CodeBlockStmtNode body;
};
} // namespace arti::lang::ast

View File

@ -0,0 +1,63 @@
#pragma once
#include <artichoke/Parser/AST/Common.hpp>
namespace arti::lang::ast {
namespace nodes {
/* Forward declaration of types */
/* Main type node types */
struct Type;
struct GenericType;
struct IdentifierType;
struct NamespacedType;
/* Helper type node types */
struct NamespacedIdentifier;
} // namespace nodes
/* Public Aliases */
using TypeNode = Ptr<nodes::Type>;
using GenericTypeNode = Ptr<nodes::GenericType>;
using IdentifierTypeNode = Ptr<nodes::IdentifierType>;
using NamespacedTypeNode = Ptr<nodes::NamespacedType>;
using NamespacedIdentifierNode = Ptr<nodes::NamespacedIdentifier>;
/* Variant nodes */
using TypeExpressionNode = Variant<
GenericTypeNode,
IdentifierTypeNode,
NamespacedTypeNode
>;
/* Node definitions */
struct nodes::Type {
SourceLocation location;
Vector<TypeQualifier> qualifiers;
TypeExpressionNode baseType;
};
struct nodes::GenericType {
SourceLocation location;
TypeExpressionNode baseType;
Vector<TypeNode> genericArgs;
};
struct nodes::IdentifierType {
SourceLocation location;
NamespacedIdentifierNode typeName;
};
struct nodes::NamespacedType {
SourceLocation location;
Vector<String> identParts;
};
} // namespace arti::lang::ast