lang_2023/include/interpreter_tree.hpp

705 lines
14 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <variant>
#include <optional>
#include <memory>
// for clangd
#include "utils.hpp"
namespace interpreter::tokens {
struct BaseNode {
std::pair<size_t, size_t> start_position;
std::pair<size_t, size_t> end_position;
std::optional<utils::IdType> type_;
};
// ----------------- Declarations -----------------
using AnyIdentifier = std::string;
using NameOrOperatorIdentifier = std::string;
using NameIdentifier = std::string;
using AnyTypeIdentifier = std::string;
using TypeIdentifier = std::string;
using AbstractTypeIdentifier = std::string;
using OperatorIdentifier = std::string;
using TypeclassIdentifier = std::string;
// Sources -----------------
struct SourceFile;
// Namespaces, partitions -----------------
struct Namespace;
struct NamespaceSources;
// Definitions -----------------
struct ImportStatement;
struct AliasDefinitionStatement;
struct VariableDefinitionStatement;
struct FunctionDeclaration;
struct FunctionDefinitionStatement;
struct TypeDefinitionStatement;
struct AbstractTypeDefinitionStatement;
struct TypeclassDefinitionStatement;
struct PartitionStatement;
//
using NamespaceStatement = std::variant<
std::unique_ptr<AliasDefinitionStatement>,
std::unique_ptr<FunctionDeclaration>,
std::unique_ptr<FunctionDefinitionStatement>,
std::unique_ptr<TypeDefinitionStatement>,
std::unique_ptr<PartitionStatement>,
std::unique_ptr<Namespace>>;
//
using SourceStatement = std::variant<
std::unique_ptr<ImportStatement>,
std::unique_ptr<AbstractTypeDefinitionStatement>,
std::unique_ptr<TypeclassDefinitionStatement>,
std::unique_ptr<NamespaceStatement>>;
//
// Definition parts
using ImportSymbol = AnyIdentifier; // can be extended name
struct FunctionDefinition;
struct TypeDefinition;
struct AnyAnnotatedType;
// TypeIdentifier <-> AbstractTypeIdentifier <-> std::string
using AnnotatedType = AnyAnnotatedType;
using AnnotatedAbstractType = AnyAnnotatedType;
// Flow control -----------------
struct TypeConstructorPatternParameter;
struct TypeConstructorPattern;
struct Match;
struct Condition;
struct DoWhileLoop;
struct WhileLoop;
struct ForLoop;
struct LoopLoop;
using FlowControl = std::variant<
std::unique_ptr<Match>,
std::unique_ptr<Condition>,
std::unique_ptr<DoWhileLoop>,
std::unique_ptr<WhileLoop>,
std::unique_ptr<ForLoop>,
std::unique_ptr<LoopLoop>>;
// Statements, expressions, blocks, etc. -----------------
struct Block;
struct FloatNumberLiteral;
struct NumberLiteral;
struct StringLiteral;
struct CharLiteral;
struct UnitLiteral;
struct BoolLiteral;
using Literal = std::variant<
std::unique_ptr<FloatNumberLiteral>,
std::unique_ptr<NumberLiteral>,
std::unique_ptr<StringLiteral>,
std::unique_ptr<CharLiteral>,
std::unique_ptr<UnitLiteral>,
std::unique_ptr<BoolLiteral>>;
//
struct NameExpression;
struct ScopedStatement;
struct AccessExpression;
using SubExpressionToken = std::variant<
std::unique_ptr<NameExpression>,
std::unique_ptr<ScopedStatement>,
std::unique_ptr<AccessExpression>,
std::unique_ptr<Literal>>;
//
struct FunctionCallExpression;
struct ArrayExpression;
struct ReferenceExpression;
using SubExpression = std::variant< // BiaryOperatorExpression is FunctionCallExpression
std::unique_ptr<FunctionCallExpression>,
std::unique_ptr<SubExpressionToken>,
std::unique_ptr<ReferenceExpression>>;
//
enum class LoopControlExpression {
Break,
Continue,
};
//
struct ReturnExpression;
using PrefixedExpression = std::variant<
std::unique_ptr<ReturnExpression>,
std::unique_ptr<LoopControlExpression>,
std::unique_ptr<Block>>;
//
struct LambdaFunction;
struct TypeConstructor;
using Expression = std::variant<
std::unique_ptr<LambdaFunction>,
std::unique_ptr<TypeConstructor>,
std::unique_ptr<PrefixedExpression>,
std::unique_ptr<SubExpression>>;
//
struct TupleExpression;
struct VariantExpression;
using SuperExpression = std::variant<
std::unique_ptr<FlowControl>,
std::unique_ptr<TupleExpression>,
std::unique_ptr<VariantExpression>,
std::unique_ptr<ArrayExpression>,
std::unique_ptr<Expression>>;
//
struct ScopedStatement;
// Operators
struct ReferenceExpression;
// Other expressions
struct FunctionCallExpression;
struct TupleExpression;
struct VariantExpression;
struct ReturnExpression;
struct TypeConstructorParameter;
struct TypeConstructor;
struct LambdaFunction;
struct ArrayExpression;
// Name
struct PartitionName {
BaseNode base;
std::vector<AnyIdentifier> path; // name is last element of path
};
struct NameExpression;
struct TupleName;
struct VariantName;
struct AnnotatedName;
// // ScopedAnyName <-> AnyName
using AnyName = std::variant<
std::unique_ptr<AnnotatedName>,
std::unique_ptr<TupleName>,
std::unique_ptr<VariantName>>;
using ScopedAnyName = AnyName;
// Type, typeclass, etc. -----------------
// Type
struct FunctionType;
struct TupleType;
struct VariantType;
struct TypeExpression;
struct TypeExpression;
using Constructor = std::string;
// // ScopedAnyType <-> AnyType
using AnyType = std::variant<
std::unique_ptr<TypeExpression>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>,
std::unique_ptr<FunctionType>>;
using ScopedAnyType = AnyType;
struct ExtendedScopedAnyType;
// Typeclass
struct ParametrizedTypeclass;
// TypeclassSubExpression -> ParametrizedTypeclass
// Typeclass & Type
struct ParametrizedType;
// Identifiers, constants, etc. -----------------
using Pattern = std::variant<
std::unique_ptr<NameIdentifier>,
std::unique_ptr<Literal>,
std::unique_ptr<TypeConstructorPattern>>;
using ScopedPattern = Pattern;
////////////////////////////////////////////////////////////////////////////////////////////
// ----------------- Sources -----------------
struct SourceFile {
BaseNode base;
std::vector<SourceStatement> statements;
};
// ----------------- Namespaces, partittions -----------------
struct NamespaceSources {
BaseNode base;
std::vector<NamespaceStatement> statements;
};
struct Namespace {
BaseNode base;
utils::ClassInternalsModifier modifier;
TypeIdentifier type;
NamespaceSources scope;
std::optional<utils::IdType> link_type_id_;
std::optional<utils::IdType> link_typeclass_id_;
};
// ----------------- Definitions -----------------
struct ImportStatement {
BaseNode base;
std::optional<TypeIdentifier> name;
std::string module_name;
std::vector<ImportSymbol> symbols;
};
struct AliasDefinitionStatement {
BaseNode base;
utils::AliasModifier modifier;
TypeIdentifier type;
std::vector<AbstractTypeIdentifier> parameters;
std::unique_ptr<TypeExpression> value;
utils::IdType type_id_;
};
struct VariableDefinitionStatement {
BaseNode base;
utils::IsConstModifier modifier;
utils::AssignmentModifier assignment_modifier;
AnyName name;
SuperExpression value;
};
struct FunctionDeclaration {
BaseNode base;
bool is_in_interface = false;
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
std::unique_ptr<FunctionType> type;
utils::IdType function_id_;
};
struct FunctionDefinitionStatement {
BaseNode base;
std::unique_ptr<FunctionDefinition> definition;
SuperExpression value;
utils::IdType function_id_;
};
struct TypeDefinitionStatement {
BaseNode base;
bool is_in_interface = false;
utils::ClassModifier modifier;
std::unique_ptr<TypeDefinition> definition;
AnyType value;
utils::IdType type_id_;
};
struct AbstractTypeDefinitionStatement {
BaseNode base;
utils::AbstractTypeModifier modifier;
std::unique_ptr<AnnotatedType> type;
utils::IdType type_graph_id_;
utils::IdType type_id_;
};
struct TypeclassDefinitionStatement {
BaseNode base;
std::unique_ptr<TypeDefinition> definition;
std::vector<std::pair<utils::ClassInternalsModifier,
std::unique_ptr<FunctionDeclaration>>> requirements;
utils::IdType typeclass_id_;
};
struct PartitionStatement {
BaseNode base;
utils::PartitionModifier modifier;
PartitionName name;
SuperExpression value;
utils::IdType executable_id_;
};
// Definition parts -----------------
struct FunctionDefinition {
BaseNode base;
NameOrOperatorIdentifier name;
std::vector<NameIdentifier> arguments;
};
struct TypeDefinition {
BaseNode base;
std::unique_ptr<AnnotatedType> type;
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
};
struct AnyAnnotatedType {
BaseNode base;
AnyTypeIdentifier type;
std::vector<std::unique_ptr<ParametrizedTypeclass>> typeclasses;
};
// ----------------- Flow control -----------------
struct TypeConstructorPatternParameter {
BaseNode base;
std::optional<NameIdentifier> name;
ScopedPattern value;
};
struct TypeConstructorPattern {
BaseNode base;
std::unique_ptr<TypeExpression> constructor;
std::vector<TypeConstructorPatternParameter> parameters;
};
struct MatchCase {
BaseNode base;
Pattern value;
std::optional<Expression> condition;
std::optional<Expression> statement;
};
struct Match {
BaseNode base;
Expression value;
std::vector<MatchCase> matches;
};
struct Condition {
BaseNode base;
std::vector<Expression> conditions; // if, elif
std::vector<Expression> statements; // if, elif, else
};
struct DoWhileLoop {
BaseNode base;
Expression condition;
Expression statement;
};
struct WhileLoop {
BaseNode base;
Expression condition;
Expression statement;
};
struct ForLoop {
BaseNode base;
utils::IsConstModifier variable_modifier;
AnyName variable;
Expression interval;
Expression statement;
};
struct LoopLoop {
BaseNode base;
Expression statement;
};
// ----------------- Statements, expressions, blocks, etc. -----------------
using BlockStatement = std::variant<
std::unique_ptr<Expression>,
std::unique_ptr<VariableDefinitionStatement>,
std::unique_ptr<FlowControl>,
std::unique_ptr<PrefixedExpression>>;
struct Block {
BaseNode base;
std::vector<BlockStatement> statements;
};
struct ScopedStatement {
BaseNode base;
SuperExpression statement;
};
// Operators -----------------
// struct BinaryOperatorExpression {
// BaseNode base;
//
// OperatorIdentifier operator_name;
// SubExpression left_expression;
// SubExpression right_expression;
// size_t precedence = utils::MaxOperatorPrecedence;
//
// utils::IdType function_id_;
// bool is_method_ = false;
// };
struct ReferenceExpression {
BaseNode base;
std::vector<utils::ReferenceModifier> references;
std::unique_ptr<ScopedStatement> expression;
};
struct AccessExpression {
BaseNode base;
std::unique_ptr<NameExpression> name;
SubExpressionToken id;
};
// Other Expressions -----------------
struct FunctionCallExpression {
BaseNode base;
std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeExpression>>> prefix;
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<TypeExpression>> parameters;
std::vector<SubExpression> arguments;
std::optional<size_t> precedence; // for operators
bool is_binary_operator_expression = false; // for operators
// only one from two is present
std::optional<utils::IdType> function_id_;
std::optional<utils::IdType> typeclass_graph_id_;
bool is_method_of_first_argument_ = false;
};
struct TupleExpression {
BaseNode base;
std::vector<SubExpression> expressions;
};
struct VariantExpression {
BaseNode base;
std::vector<SubExpression> expressions;
};
struct ReturnExpression {
BaseNode base;
Expression expression;
};
struct TypeConstructorParameter {
BaseNode base;
std::optional<NameIdentifier> name;
std::optional<utils::AssignmentModifier> asignment_modifier;
SubExpression value;
};
struct TypeConstructor {
BaseNode base;
std::unique_ptr<TypeExpression> constructor;
std::vector<TypeConstructorParameter> parameters;
};
struct LambdaFunction {
BaseNode base;
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
std::vector<NameIdentifier> arguments;
Expression expression;
std::vector<utils::IdType> argument_graph_ids_;
utils::IdType return_type_graph_id_;
};
struct ArrayExpression {
BaseNode base;
std::vector<SubExpression> elements;
};
// Name -----------------
struct NameExpression {
BaseNode base;
std::vector<NameIdentifier> names;
};
struct TupleName {
BaseNode base;
std::vector<ScopedAnyName> names;
};
struct VariantName {
BaseNode base;
std::vector<ScopedAnyName> names;
};
struct AnnotatedName {
BaseNode base;
NameIdentifier name;
std::optional<ScopedAnyType> type;
};
// ----------------- Type, typeclass, etc. -----------------
// Type -----------------
struct FunctionType {
BaseNode base;
std::vector<ScopedAnyType> types;
};
struct TupleType {
BaseNode base;
std::optional<Constructor> type;
std::vector<std::pair<std::optional<NameIdentifier>, std::unique_ptr<ExtendedScopedAnyType>>> entities;
};
struct VariantType {
BaseNode base;
std::optional<Constructor> type;
std::vector<std::variant<Constructor, std::unique_ptr<TupleType>>> constructors;
};
struct ParametrizedType {
BaseNode base;
AnyTypeIdentifier type;
std::vector<std::unique_ptr<TypeExpression>> parameters;
std::optional<utils::IdType> type_id_; // std::nullopt, if it is namespace without type
};
struct TypeExpression {
BaseNode base;
std::vector<ParametrizedType> path;
ParametrizedType type;
std::optional<size_t> array_size; // if array; 0 - dynamic size
std::optional<utils::IdType> type_id_;
std::optional<utils::IdType> constructor_id_;
};
struct ExtendedScopedAnyType {
BaseNode base;
std::vector<utils::ReferenceModifier> references;
AnyType type;
};
// Typeclass -----------------
struct ParametrizedTypeclass {
BaseNode base;
TypeclassIdentifier typeclass;
std::vector<std::unique_ptr<TypeExpression>> parameters;
utils::IdType typeclass_id_;
};
// ----------------- Comments [IGNORE] -----------------
// ----------------- Identifiers, constants, etc. -----------------
struct FloatNumberLiteral {
BaseNode base;
double value;
};
struct NumberLiteral {
BaseNode base;
int64_t value;
};
struct StringLiteral {
BaseNode base;
std::string value;
};
struct CharLiteral {
BaseNode base;
char value;
};
struct UnitLiteral {
BaseNode base;
};
struct BoolLiteral {
BaseNode base;
bool value;
};
} // namespace interpereter::tokens