lang_2023/include/interpreter_tree.hpp

604 lines
13 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>
// for clangd
#include "utils.hpp"
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
2023-04-25 13:22:23 +03:00
using ImportSymbol = AnyIdentifier; // can be extended name
2023-03-26 15:20:53 +03:00
struct FunctionDefinition;
struct TypeDefinition;
struct AnnotatedAbstractType;
// TypeIdentifier <-> AbstractTypeIdentifier <-> std::string
using AnnotatedType = AnnotatedAbstractType;
using AnyAnnotatedType = AnnotatedType;
2023-03-26 15:20:53 +03:00
// Flow control -----------------
2023-04-25 21:21:36 +03:00
struct TypeConstructorPatternParameter;
2023-04-25 13:22:23 +03:00
struct TypeConstructorPattern;
2023-03-26 15:20:53 +03:00
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 FloatNumberLiteral;
struct NumberLiteral;
struct StringLiteral;
struct CharLiteral;
2023-04-26 01:02:53 +03:00
struct UnitLiteral;
using Literal = std::variant<
std::unique_ptr<FloatNumberLiteral>,
std::unique_ptr<NumberLiteral>,
std::unique_ptr<StringLiteral>,
2023-04-26 01:02:53 +03:00
std::unique_ptr<CharLiteral>,
std::unique_ptr<UnitLiteral>>;
2023-03-26 15:20:53 +03:00
//
struct NameExpression;
2023-03-26 15:20:53 +03:00
struct ScopedStatement;
struct AccessExpression;
2023-03-26 15:20:53 +03:00
using SubExpressionToken = std::variant<
std::unique_ptr<NameExpression>,
std::unique_ptr<ScopedStatement>,
std::unique_ptr<AccessExpression>,
std::unique_ptr<Literal>>;
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<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>,
2023-04-25 13:22:23 +03:00
std::unique_ptr<ArrayExpression>,
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;
2023-03-26 15:20:53 +03:00
struct TupleExpression;
struct VariantExpression;
struct ReturnExpression;
2023-04-25 21:21:36 +03:00
struct TypeConstructorParameter;
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;
2023-04-25 13:22:23 +03:00
struct TypeExpression;
struct TypeExpression;
using Constructor = std::string;
// // ScopedAnyType <-> AnyType
2023-03-26 15:20:53 +03:00
using AnyType = std::variant<
2023-04-25 13:22:23 +03:00
std::unique_ptr<TypeExpression>,
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;
2023-04-25 13:22:23 +03:00
using TypeclassSubExpression = std::variant<
std::unique_ptr<TypeclassIdentifier>,
std::unique_ptr<ParametrizedTypeclass>>;
// Typeclass & Type
2023-04-25 13:22:23 +03:00
struct ParametrizedType;
using TypeSubExpression = std::variant<
std::unique_ptr<AnyTypeIdentifier>,
std::unique_ptr<ParametrizedType>>;
//
using FunctionArgument = std::variant<
SubExpressionToken,
TypeSubExpression>;
2023-03-26 15:20:53 +03:00
// Comments [IGNORE] -----------------
// Identifiers, constants, etc. -----------------
struct ExtendedName {
std::string name;
};
2023-04-25 13:22:23 +03:00
using PatternToken = std::variant<
std::unique_ptr<ExtendedName>,
std::unique_ptr<Literal>,
std::unique_ptr<TypeConstructorPattern>>;
using Pattern = std::variant<
std::unique_ptr<TypeConstructorPattern>,
std::unique_ptr<PatternToken>>;
////////////////////////////////////////////////////////////////////////////////////////////
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;
std::optional<utils::IdType> type_id_;
};
struct Partition {
enum PartitionName {
2023-03-26 15:20:53 +03:00
Test,
Interface,
2023-04-26 01:02:53 +03:00
Code,
2023-03-26 15:20:53 +03:00
};
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-04-25 13:22:23 +03:00
std::unique_ptr<TypeExpression> value;
std::vector<utils::IdType> parameter_graph_ids_;
utils::IdType type_id_;
2023-03-26 15:20:53 +03:00
};
struct VariableDefinitionStatement {
enum { Var, Const } modifier;
enum { Move, Assign } assignment_modifier;
AnyName name;
SuperExpression value;
2023-03-26 15:20:53 +03:00
};
struct FunctionDeclaration {
2023-04-25 13:22:23 +03:00
ExtendedName name;
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
std::unique_ptr<FunctionType> type;
utils::IdType function_id_;
2023-03-26 15:20:53 +03:00
};
struct FunctionDefinitionStatement {
bool is_inline;
std::unique_ptr<FunctionDefinition> definition;
SuperExpression value;
utils::IdType function_id_;
std::vector<utils::IdType> argument_graph_ids_;
utils::IdType return_type_graph_id_;
2023-03-26 15:20:53 +03:00
};
struct TypeDefinitionStatement {
enum { Struct, Class } modifier;
std::unique_ptr<TypeDefinition> definition;
AnyType value;
utils::IdType type_id_;
2023-03-26 15:20:53 +03:00
};
struct AbstractTypeDefinitionStatement {
enum { Basic, Abstract } modifier;
std::unique_ptr<AnnotatedType> type;
utils::IdType type_graph_id_;
utils::IdType type_id_;
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;
utils::IdType typeclass_id_;
2023-03-26 15:20:53 +03:00
};
// Definition parts -----------------
struct FunctionDefinition {
enum { Operator, Function } modifier;
2023-04-25 13:22:23 +03:00
ExtendedName name;
std::vector<std::unique_ptr<AnnotatedAbstractType>> 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<AnnotatedAbstractType>> parameters;
2023-03-26 15:20:53 +03:00
};
struct AnnotatedAbstractType {
AbstractTypeIdentifier type;
2023-04-25 14:52:38 +03:00
std::vector<std::unique_ptr<TypeclassExpression>> typeclasses;
utils::IdType type_graph_id_;
2023-03-26 15:20:53 +03:00
};
// ----------------- Flow control -----------------
2023-04-25 21:21:36 +03:00
struct TypeConstructorPatternParameter {
std::optional<ExtendedName> name;
PatternToken value;
};
2023-04-25 13:22:23 +03:00
struct TypeConstructorPattern {
TypeIdentifier constructor;
2023-04-25 21:21:36 +03:00
std::vector<TypeConstructorPatternParameter> parameters;
2023-04-25 13:22:23 +03:00
};
2023-03-26 15:20:53 +03:00
struct MatchCase {
2023-04-25 13:22:23 +03:00
Pattern 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<utils::ReferenceType> references;
std::unique_ptr<ScopedStatement> expression;
};
2023-03-26 15:20:53 +03:00
struct AccessExpression {
std::unique_ptr<NameExpression> name;
SubExpressionToken id;
};
// Other Expressions -----------------
2023-03-26 15:20:53 +03:00
struct FunctionCallExpression {
2023-04-25 21:21:36 +03:00
std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeExpression>>> prefix;
2023-04-25 13:22:23 +03:00
ExtendedName 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
};
2023-04-25 21:21:36 +03:00
struct TypeConstructorParameter {
enum AssignmentModifier { Move, Assign };
2023-04-25 21:21:36 +03:00
std::optional<ExtendedName> name;
std::optional<AssignmentModifier> asignment_modifier;
2023-04-26 01:02:53 +03:00
SubExpression value;
2023-04-25 21:21:36 +03:00
};
struct TypeConstructor {
2023-04-25 13:22:23 +03:00
std::unique_ptr<TypeExpression> constructor;
2023-04-25 21:21:36 +03:00
std::vector<TypeConstructorParameter> parameters;
};
2023-03-26 15:20:53 +03:00
struct LambdaFunction {
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
std::vector<ExtendedName> arguments;
2023-03-27 03:10:04 +03:00
Expression expression;
2023-04-22 19:30:16 +03:00
std::vector<utils::IdType> argument_graph_ids_;
utils::IdType return_type_graph_id_;
2023-03-26 15:20:53 +03:00
};
struct ArrayExpression {
2023-04-25 13:22:23 +03:00
std::vector<SubExpression> elements;
2023-03-26 15:20:53 +03:00
};
// Name -----------------
struct NameExpression {
2023-04-25 13:22:23 +03:00
std::vector<ExtendedName> names;
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 TypeExpression {
2023-04-25 15:10:48 +03:00
std::vector<TypeSubExpression> path;
TypeSubExpression type;
std::optional<size_t> array_size; // if array; 0 - dynamic size
utils::IdType type_id_;
2023-03-26 15:20:53 +03:00
};
struct ExtendedScopedAnyType {
std::vector<utils::ReferenceType> references;
AnyType type;
};
2023-03-26 15:20:53 +03:00
// Typeclass -----------------
2023-03-26 15:20:53 +03:00
struct TypeclassExpression {
2023-04-25 15:10:48 +03:00
std::vector<TypeSubExpression> path;
2023-04-25 13:22:23 +03:00
TypeclassSubExpression typeclass;
utils::IdType type_id_;
2023-03-26 15:20:53 +03:00
};
2023-04-25 13:22:23 +03:00
struct ParametrizedTypeclass {
2023-04-25 14:52:38 +03:00
TypeclassIdentifier typeclass;
std::vector<std::unique_ptr<TypeExpression>> parameters;
2023-04-25 13:22:23 +03:00
};
// Typeclass & Type -----------------
struct ParametrizedType {
2023-04-25 14:52:38 +03:00
AnyTypeIdentifier type;
std::vector<std::unique_ptr<TypeExpression>> parameters;
2023-04-25 13:22:23 +03:00
};
2023-03-26 15:20:53 +03:00
// ----------------- 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-26 01:02:53 +03:00
struct UnitLiteral {};
2023-04-02 15:10:32 +03:00
} // namespace interpereter::tokens