diff --git a/lib/include/artichoke/Parser/AST/Node.hpp b/lib/include/artichoke/Parser/AST/Node.hpp new file mode 100644 index 0000000..a992385 --- /dev/null +++ b/lib/include/artichoke/Parser/AST/Node.hpp @@ -0,0 +1,580 @@ +#pragma once + +#include +#include +#include +#include + +namespace arti::lang::ast { + + struct SourceLocation { + std::size_t line; + std::size_t column; + }; + + namespace node { + template + using Ptr = std::unique_ptr; + + template + using Opt = std::optional; + + 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, + Ptr, + Ptr>; + + using TopLevelDeclaration = std::variant< + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr>; + + struct Module { + SourceLocation location; + bool isExported; + Ptr name; + std::vector> childModules; + std::vector declarations; + std::vector> aliasedElements; + }; + + struct ImportStatement { + SourceLocation location; + bool importAll; + Ptr target; + }; + + struct AliasStatement { + SourceLocation location; + std::string name; + Ptr aliased; + }; + + struct Function { + SourceLocation location; + bool isExported; + std::string name; + Ptr returnType; + Ptr body; + std::vector> generics; + std::vector> params; + }; + + struct Struct { + SourceLocation location; + bool isExported; + std::string name; + std::vector> generics; + std::vector> members; + }; + + struct Enum { + SourceLocation location; + bool isExported; + std::string name; + std::vector> generics; + std::vector> members; + }; + + struct GenericParam { + SourceLocation location; + std::string name; + }; + + struct FunctionParam { + SourceLocation location; + bool isThis; + std::string name; + Ptr type; + }; + + struct StructMember { + SourceLocation location; + std::string name; + Ptr type; + }; + + struct EnumMember { + SourceLocation location; + std::string name; + Ptr type; + }; + + /* Expression node types */ + + using Expression = std::variant< + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr>; + + using StructLiteralFields = std::variant< + Ptr, + Ptr>; + + 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; + StructLiteralFields fields; + }; + + struct StructNamedInit { + SourceLocation location; + std::string name; + Ptr initializer; + }; + + struct StructNamedFieldsInit { + std::vector fields; + }; + + struct StructExpressionsInit { + std::vector locations; + std::vector> initializers; + }; + + struct IdentifierExpression { + SourceLocation location; + std::string name; + }; + + struct UnaryExpression { + SourceLocation location; + UnaryOperator operatorT; + Ptr right; + }; + + struct BinaryExpression { + SourceLocation location; + BinaryOperator operatorT; + Ptr leftHs; + Ptr rightHs; + }; + + struct AssignExpression { + SourceLocation location; + Ptr leftHs; + Ptr rightHs; + }; + + struct CompoundAssignExpression { + SourceLocation location; + CompoundAssignType type; + Ptr leftHs; + Ptr rightHs; + }; + + struct FunctionCallExpression { + SourceLocation location; + Ptr callee; + std::vector> arguments; + }; + + struct SliceAccessExpression { + SourceLocation location; + Ptr slice; + Ptr index; + }; + + struct SliceRangeExpression { + SourceLocation location; + Ptr slice; + Ptr start; + Ptr end; + }; + + struct MemberAccessExpression { + SourceLocation location; + Ptr object; + std::string memberName; + }; + + struct ScopeAccessExpression { + SourceLocation location; + Ptr object; + std::string memberName; + std::vector> genericParams; + }; + + struct PointerAccessExpression { + SourceLocation location; + Ptr object; + std::string memberName; + }; + + struct ReflectionExpression { + SourceLocation location; + Ptr object; + Opt attribute; + }; + + struct SliceCreationExpression { + SourceLocation location; + Ptr object; + Ptr length; + }; + + struct SliceLengthExpression { + SourceLocation location; + Ptr object; + }; + + struct SlicePtrExpression { + SourceLocation location; + Ptr object; + }; + + /* Statements */ + + using Statement = std::variant< + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr, + Ptr>; + + using ElseBranch = std::variant< + Ptr, + Ptr>; + + using Deferable = std::variant< + Ptr, + Ptr>; + + using PreLoopStatement = std::variant< + Ptr, + Ptr>; + + using TypeExpression = std::variant< + Ptr, + Ptr, + Ptr>; + + + struct CodeBlock { + SourceLocation location; + std::vector statements; + }; + + struct VariableDeclaration { + SourceLocation location; + Mutability mutability; + std::string name; + Ptr type; + Ptr initializer; + }; + + struct IfStatement { + SourceLocation location; + Opt unwrappedVar; + Ptr condition; + Ptr body; + Opt elseBranch; + }; + + struct ElseStatement { + SourceLocation location; + Opt unwrappedVar; + Ptr body; + }; + + struct DeferStatement { + SourceLocation location; + Deferable body; + }; + + struct ErrDeferStatement { + SourceLocation location; + Deferable body; + }; + + struct ReturnStatement { + SourceLocation location; + Ptr value; + }; + + struct BreakStatement { + SourceLocation location; + Opt label; + }; + + struct ContinueStatement { + SourceLocation location; + Opt label; + }; + + struct MatchStatement { + SourceLocation location; + Ptr value; + std::vector> cases; + Ptr defaultCase; + }; + + struct SwitchStatement { + SourceLocation location; + Ptr value; + std::vector> cases; + Ptr defaultCase; + }; + + struct MatchCase { + SourceLocation location; + std::string identifier; + Ptr matchType; + Ptr body; + }; + + struct SwitchCase { + SourceLocation location; + Ptr matchExpr; + Ptr body; + }; + + struct CForStatement { + SourceLocation location; + Ptr preLoop; + Ptr condition; + Ptr postLoop; + Ptr body; + }; + + struct RangeForStatement { + SourceLocation location; + Mutability varMutability; + std::string varName; + Ptr range; + Ptr body; + }; + + struct WhileStatement { + SourceLocation location; + Opt unwrappedVar; + Ptr condition; + Ptr body; + Opt elseBranch; + }; + + struct DoWhileStatement { + SourceLocation location; + Ptr body; + Ptr condition; + }; + + struct InfLoopStatement { + SourceLocation location; + Ptr body; + }; + + struct ExpressionStatement { + SourceLocation location; + Ptr expression; + }; + + struct SimpleTypeExpression { + SourceLocation location; + Ptr name; + }; + + struct GenericTypeExpression { + SourceLocation location; + TypeExpression baseType; + std::vector> genericArgs; + }; + + struct AccessTypeExpression { + SourceLocation location; + TypeExpression baseType; + std::string memberName; + }; + + struct Type { + SourceLocation location; + std::vector qualifiers; + TypeExpression expression; + }; + + struct NamespacedIdentifier { + SourceLocation location; + std::vector identifierParts; + }; + + } // namespace node + + struct AST { + std::vector declarations; + }; + +} // namespace arti::lang::ast