lang_2023/include/interpreter_tree.hpp

545 lines
12 KiB
C++
Raw Normal View History

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