lang_2023/include/interpreter_tree.hpp

546 lines
12 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <variant>
#include <optional>
#include <memory>
namespace interpreter::tokens {
// ----------------- 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;
struct Sources;
// Namespaces, partitions -----------------
struct Partition;
struct Namespace;
// Definitions -----------------
struct ImportStatement;
struct AliasDefinitionStatement;
struct VariableDefinitionStatement;
struct FunctionDeclaration;
struct FunctionDefinitionStatement;
struct TypeDefinitionStatement;
struct AbstractTypeDefinitionStatement;
struct TypeclassDefinitionStatement;
using SourceStatement = std::variant<
std::unique_ptr<ImportStatement>,
std::unique_ptr<AliasDefinitionStatement>,
std::unique_ptr<FunctionDeclaration>,
std::unique_ptr<FunctionDefinitionStatement>,
std::unique_ptr<TypeDefinitionStatement>,
std::unique_ptr<AbstractTypeDefinitionStatement>,
std::unique_ptr<TypeclassDefinitionStatement>,
std::unique_ptr<Namespace>>;
// Definition parts
using ImportSymbol = AnyIdentifier;
struct FunctionDefinition;
struct TypeDefinition;
struct DefinitionParameter;
// Flow control -----------------
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 NameExpression;
struct ScopedStatement;
using SubExpressionToken = std::variant<
std::unique_ptr<NameExpression>,
std::unique_ptr<ScopedStatement>>;
//
struct FunctionCallExpression;
struct BinaryOperatorExpression;
struct ArrayExpression;
struct ReferenceExpression;
using SubExpression = std::variant<
std::unique_ptr<FunctionCallExpression>,
std::unique_ptr<BinaryOperatorExpression>,
std::unique_ptr<SubExpressionToken>,
std::unique_ptr<ArrayExpression>,
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;
struct UnaryOperatorExpression;
using Expression = std::variant<
std::unique_ptr<LambdaFunction>,
std::unique_ptr<TypeConstructor>,
std::unique_ptr<PrefixedExpression>,
std::unique_ptr<UnaryOperatorExpression>,
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<Expression>>;
//
struct ScopedStatement;
// Operators
struct BinaryOperatorExpression;
struct UnaryOperatorExpression;
struct ReferenceExpression;
// Other expressions
struct FunctionCallExpression;
struct TupleExpression;
struct VariantExpression;
struct ReturnExpression;
struct TypeConstructor;
struct LambdaFunction;
struct ArrayExpression;
// Name
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 AnnotatedType;
struct ParametrizedType;
struct TypeExpression;
using Constructor = std::string;
// // ScopedAnyType <-> AnyType
using AnyType = std::variant<
std::unique_ptr<ParametrizedType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>,
std::unique_ptr<FunctionType>>;
using ScopedAnyType = AnyType;
struct ExtendedScopedAnyType;
// Typeclass
struct ParametrizedTypeclass;
struct TypeclassExpression;
using TypeclassUsage = std::variant<
std::unique_ptr<TypeclassExpression>,
std::unique_ptr<ParametrizedTypeclass>>;
// Typeclass & Type
using TypeParameter = std::variant<
std::unique_ptr<TypeExpression>,
std::unique_ptr<ParametrizedType>>;
using TypeSubExpression = std::variant<
std::unique_ptr<AnyTypeIdentifier>,
std::unique_ptr<ParametrizedType>>;
//
using FunctionArgument = std::variant<
SubExpressionToken,
TypeSubExpression>;
// Comments [IGNORE] -----------------
// Identifiers, constants, etc. -----------------
struct ExtendedName {
std::string name;
};
struct FloatNumberLiteral;
struct NumberLiteral;
struct StringLiteral;
struct CharLiteral;
using Literal = std::variant<
std::unique_ptr<FloatNumberLiteral>,
std::unique_ptr<NumberLiteral>,
std::unique_ptr<StringLiteral>,
std::unique_ptr<CharLiteral>>;
//
using NameSubExpression = std::variant<
std::unique_ptr<ExtendedName>,
std::unique_ptr<Literal>,
std::unique_ptr<SuperExpression>>;
enum class ReferenceType {
Reference,
UniqueReference,
};
////////////////////////////////////////////////////////////////////////////////////////////
// ----------------- Sources -----------------
struct SourceFile {
std::vector<std::variant<SourceStatement, Partition>> statements;
};
struct Sources {
std::vector<SourceStatement> statements;
};
// ----------------- Namespaces, partittions -----------------
struct Namespace {
enum Modifier { Const, Var };
std::optional<Modifier> modifier;
std::optional<ExtendedName> name;
TypeIdentifier type;
std::unique_ptr<Sources> scope;
};
struct Partition {
enum PartitionName {
Test,
Interface,
Core,
Lib,
Module,
Exe,
};
PartitionName name;
std::unique_ptr<Sources> scope;
};
// ----------------- Definitions -----------------
struct ImportStatement {
std::optional<TypeIdentifier> name;
std::string module_name;
std::vector<ImportSymbol> symbols;
};
struct AliasDefinitionStatement {
enum {Alias, Type, Let} modifier;
TypeIdentifier type;
std::vector<AbstractTypeIdentifier> parameters;
std::unique_ptr<ParametrizedType> value;
};
struct VariableDefinitionStatement {
enum { Var, Const } modifier;
enum { Move, Assign } assignment_modifier;
AnyName name;
SuperExpression value;
};
struct FunctionDeclaration {
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::unique_ptr<FunctionType> type;
};
struct FunctionDefinitionStatement {
bool is_inline;
std::unique_ptr<FunctionDefinition> definition;
SuperExpression value;
};
struct TypeDefinitionStatement {
enum { Struct, Class } modifier;
std::unique_ptr<TypeDefinition> definition;
AnyType value;
};
struct AbstractTypeDefinitionStatement {
enum { Basic, Abstract } modifier;
std::unique_ptr<AnnotatedType> type;
};
struct TypeclassDefinitionStatement {
std::unique_ptr<TypeDefinition> definition;
std::vector<std::unique_ptr<FunctionDeclaration>> requirements;
};
// Definition parts -----------------
struct FunctionDefinition {
enum { Operator, Function } modifier;
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<ExtendedName> arguments;
};
struct TypeDefinition {
std::unique_ptr<AnnotatedType> type;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
};
struct DefinitionParameter {
AbstractTypeIdentifier type;
std::vector<TypeclassUsage> typeclasses;
};
// ----------------- Flow control -----------------
struct MatchCase {
Expression value;
std::optional<Expression> condition;
std::optional<Expression> statement;
};
struct Match {
Expression value;
std::vector<MatchCase> matches;
};
struct Condition {
std::vector<Expression> conditions; // if, elif
std::vector<Expression> statements; // if, elif, else
};
struct DoWhileLoop {
Expression condition;
Expression statement;
};
struct WhileLoop {
Expression condition;
Expression statement;
};
struct ForLoop {
AnyName variable;
Expression interval;
Expression statement;
};
struct LoopLoop {
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 {
std::vector<BlockStatement> statements;
};
struct ScopedStatement {
SuperExpression statement;
};
// Operators -----------------
struct BinaryOperatorExpression {
OperatorIdentifier operator_name;
SubExpression left_expression;
SubExpression right_expression;
};
struct UnaryOperatorExpression {
OperatorIdentifier operator_name;
Expression expression;
};
struct ReferenceExpression {
std::vector<ReferenceType> references;
std::unique_ptr<ScopedStatement> expression;
};
// Other Expressions -----------------
struct FunctionCallExpression {
std::unique_ptr<NameExpression> name;
std::vector<FunctionArgument> arguments;
};
struct TupleExpression {
std::vector<SubExpression> expressions;
};
struct VariantExpression {
std::vector<SubExpression> expressions;
};
struct ReturnExpression {
Expression expression;
};
struct TypeConstructor {
enum AssignmentModifier { Move, Assign };
std::unique_ptr<ParametrizedType> type;
std::vector<std::tuple<ExtendedName, AssignmentModifier, SubExpression>> parameters;
};
struct LambdaFunction {
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<ExtendedName> arguments;
Expression expression;
};
struct ArrayExpression {
std::vector<Expression> elements;
};
// Name -----------------
struct NameExpression {
std::vector<TypeSubExpression> namespaces;
std::vector<NameSubExpression> expressions;
};
struct TupleName {
std::vector<ScopedAnyName> names;
};
struct VariantName {
std::vector<ScopedAnyName> names;
};
struct AnnotatedName {
NameIdentifier name;
std::optional<ScopedAnyType> type;
};
// ----------------- Type, typeclass, etc. -----------------
// Type -----------------
struct FunctionType {
std::vector<ScopedAnyType> types;
};
struct TupleType {
std::optional<Constructor> type;
std::vector<std::pair<std::optional<ExtendedName>, std::unique_ptr<ExtendedScopedAnyType>>> entities;
};
struct VariantType {
std::optional<Constructor> type;
std::vector<std::variant<Constructor, std::unique_ptr<TupleType>>> constructors;
};
struct AnnotatedType {
std::unique_ptr<TypeExpression> type_expression;
std::vector<TypeclassUsage> annotations;
};
struct ParametrizedType {
std::unique_ptr<TypeExpression> type_expression;
std::vector<TypeParameter> parameters;
};
struct TypeExpression {
std::vector<TypeSubExpression> namespaces;
AnyTypeIdentifier type;
std::optional<size_t> array_size; // if array; 0 - dynamic size
};
struct ExtendedScopedAnyType {
std::vector<ReferenceType> references;
AnyType type;
};
// Typeclass -----------------
struct ParametrizedTypeclass {
std::unique_ptr<TypeclassExpression> typeclass_expression;
std::vector<TypeParameter> parameters;
};
struct TypeclassExpression {
std::vector<TypeSubExpression> namespaces;
TypeclassIdentifier typeclass;
};
// ----------------- Comments [IGNORE] -----------------
// ----------------- Identifiers, constants, etc. -----------------
struct FloatNumberLiteral {
double value;
};
struct NumberLiteral {
int64_t value;
};
struct StringLiteral {
std::string value;
};
struct CharLiteral {
char value;
};
} // namespace interpereter::tokens