lang_2023/include/interpreter_tree.hpp
2023-03-29 11:42:00 +03:00

580 lines
13 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <variant>
#include <optional>
#include <memory>
// for clangd
#include "node.hpp"
namespace interpreter {
namespace 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, partittions -----------------
struct Partition;
struct Namespace;
// Definitions -----------------
struct ImportStatement;
struct UsageDefinition;
struct AliasDefinition;
struct VariableDefinition;
struct FunctionDeclaration;
struct FunctionDefinition;
struct AliasTypeDefinition;
struct TypeDefinition;
struct TypeclassDefinition;
using SourceStatement = std::variant<
std::unique_ptr<ImportStatement>,
std::unique_ptr<UsageDefinition>,
std::unique_ptr<AliasDefinition>,
std::unique_ptr<VariableDefinition>, // ??
std::unique_ptr<FunctionDeclaration>,
std::unique_ptr<FunctionDefinition>,
std::unique_ptr<AliasTypeDefinition>,
std::unique_ptr<TypeDefinition>,
std::unique_ptr<TypeclassDefinition>,
std::unique_ptr<Namespace>>;
// Definition parts
struct ParametrizedType;
struct TupleType;
struct VariantType;
struct ParametrizedTypeclass;
using FunctionDeclarationType = std::variant<
std::unique_ptr<ParametrizedType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>,
std::unique_ptr<ParametrizedTypeclass>>;
struct DefinedName;
struct DefinedAnnotatedName;
struct DefinedType;
struct DefinedTypeclass;
struct DefinitionParameter;
struct DefinitionArgument;
// 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;
enum class LoopControlExpression {
Break,
Continue,
};
using SubExpressionToken = std::variant<
std::unique_ptr<NameExpression>,
std::unique_ptr<ScopedStatement>>;
//
struct FunctionCallExpression;
struct BinaryOperatorExpression;
using SubExpression = std::variant<
std::unique_ptr<FunctionCallExpression>,
std::unique_ptr<BinaryOperatorExpression>,
std::unique_ptr<SubExpressionToken>>;
//
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; // ?? scoped statement is _superexpression ??
// Operators
struct BinaryOperatorExpression;
struct UnaryOperatorExpression;
// Simple Expressions
struct FunctionCallExpression;
struct TupleExpression;
struct VariantExpression;
struct ReturnExpression;
// Lambda
struct LambdaFunction;
// Name
struct NameSuperExpression;
struct NameExpression;
struct TupleName;
struct VariantName;
struct AnnotatedName;
using AnyName = std::variant<
std::unique_ptr<AnnotatedName>,
std::unique_ptr<TupleName>,
std::unique_ptr<VariantName>>;
// Type
struct TypeConstructor;
// // TypeOrAnyType <-> AnyType
struct TupleType;
struct VariantType;
struct VariantType;
using AnyType = std::variant<
std::unique_ptr<ParametrizedType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>>;
// // TypeIdentifierDefinition <-> some '.' + TypeIdentifier
using TypeIdentifierDefinition = std::string;
struct AnnotatedType;
// // TypeAnnotations - inplace ??
struct ParametrizedType;
struct TypeExpression;
using TypeSubExpression = std::variant<
std::unique_ptr<AnyTypeIdentifier>,
std::unique_ptr<ParametrizedType>>;
// Typeclass
struct AnnotatedTypeclass;
struct ParametrizedTypeclass;
struct TypeclassExpression;
// Comments [IGNORE] -----------------
// Identifiers, constants, etc. -----------------
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 NameSubSuperExpression = std::variant<
std::unique_ptr<NameIdentifier>,
std::unique_ptr<Literal>,
std::unique_ptr<SuperExpression>>;
// ----------------- Sources -----------------
struct SourceFile : public Node {
std::vector<std::variant<SourceStatement, Partition>> statements;
};
struct Sources : public Node {
std::vector<SourceStatement> statements;
};
// ----------------- Namespaces, partittions -----------------
struct Partition : public Node {
enum PartitionType {
Test,
Interface,
Core,
Lib,
Module, // rename ??
Exe,
};
PartitionType type;
std::unique_ptr<Sources> scope;
};
struct Namespace : public Node {
bool is_const;
std::variant<
std::unique_ptr<DefinedAnnotatedName>,
std::unique_ptr<DefinedType>> name;
// TODO add const / var specification
std::unique_ptr<Sources> scope;
};
// ----------------- Definitions -----------------
struct TypeclassExpression;
struct TypeExpression;
struct NameExpression;
using ImportSymbol = std::variant<
std::unique_ptr<TypeclassExpression>,
std::unique_ptr<TypeExpression>,
std::unique_ptr<NameExpression>>;
//
struct ImportStatement : public Node {
std::string module_name;
std::vector<ImportSymbol> symbols;
};
struct UsageDefinition : public Node {
TypeIdentifier name; // rename ??
std::unique_ptr<ImportStatement> import_statement;
};
struct AliasDefinition : public Node {
std::unique_ptr<DefinedType> type;
std::unique_ptr<ParametrizedType> value;
};
struct VariableDefinition : public Node {
bool is_const; // default value ??
NameIdentifier name;
SuperExpression value;
};
struct FunctionDeclaration : public Node {
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<FunctionDeclarationType> argument_types;
};
struct FunctionDefinition : public Node {
std::unique_ptr<DefinedName> name;
SuperExpression value;
};
struct AliasTypeDefinition : public Node {
std::unique_ptr<DefinedType> type;
std::unique_ptr<ParametrizedType> value;
};
struct TypeDefinition : public Node {
bool is_class; // class vs struct
std::unique_ptr<DefinedType> type;
AnyType value;
};
struct TypeclassDefinition : public Node {
std::unique_ptr<DefinedTypeclass> typeclass;
std::vector<std::unique_ptr<FunctionDeclaration>> requirements;
};
// Definition parts -----------------
struct DefinedName : public Node {
bool is_operator = false; // other format ??
NameOrOperatorIdentifier name; // function name or operator
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
};
struct DefinedAnnotatedName : public Node {
NameIdentifier name;
std::variant<
std::unique_ptr<DefinedType>,
std::unique_ptr<DefinedTypeclass>> type;
};
struct DefinedType : public Node {
std::unique_ptr<AnnotatedType> type;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
};
struct DefinedTypeclass : public Node {
std::unique_ptr<AnnotatedTypeclass> typeclass;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
};
struct DefinitionParameter : public Node {
AbstractTypeIdentifier type;
std::vector<std::unique_ptr<ParametrizedTypeclass>> typeclasses;
};
struct DefinitionArgument : public Node {
NameIdentifier name;
std::vector<std::unique_ptr<ParametrizedType>> types;
};
// ----------------- Flow control -----------------
struct MatchCase {
Expression value;
std::optional<Expression> condition;
std::optional<Expression> statement;
};
struct Match : public Node {
Expression value;
std::vector<MatchCase> matches;
};
struct Condition : public Node {
std::vector<Expression> conditions; // if, elif
std::vector<Expression> statements; // if, elif, else
};
struct DoWhileLoop : public Node {
Expression condition;
Expression statement;
};
struct WhileLoop : public Node {
Expression condition;
Expression statement;
};
struct ForLoop : public Node {
AnyName variable;
Expression interval;
Expression statement;
};
struct LoopLoop : public Node {
Expression statement;
};
// ----------------- Statements, expressions, blocks, etc. -----------------
using BlockStatement = std::variant<
std::unique_ptr<Expression>,
std::unique_ptr<AliasDefinition>,
std::unique_ptr<VariableDefinition>,
std::unique_ptr<FlowControl>,
std::unique_ptr<PrefixedExpression>>;
struct Block : public Node {
std::vector<BlockStatement> statements;
};
struct ScopedStatement : public Node {
SuperExpression statement;
};
// Operators -----------------
struct BinaryOperatorExpression : public Node {
OperatorIdentifier operator_name;
SubExpression left_expression;
SubExpression right_expression;
};
struct UnaryOperatorExpression : public Node { // TODO fix prescendence
OperatorIdentifier operator_name;
Expression expression;
};
// Simple Expressions -----------------
using FunctionArgument = std::variant<
std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeSubExpression>>;
struct FunctionCallExpression : public Node {
std::unique_ptr<NameSuperExpression> name;
std::vector<FunctionArgument> arguments;
};
struct TupleExpression : public Node {
std::vector<SubExpression> expressions;
};
struct VariantExpression : public Node {
std::vector<SubExpression> expressions;
};
struct ReturnExpression : public Node {
Expression expression;
};
// Lambda -----------------
struct LambdaFunction : public Node {
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
Expression expression;
};
// Name -----------------
struct NameSuperExpression : public Node {
std::vector<TypeSubExpression> namespaces;
std::vector<NameSubSuperExpression> expressions; // last is not SuperExpression
};
struct NameExpression : public Node {
std::vector<TypeSubExpression> namespaces;
std::vector<NameIdentifier> names;
};
struct TupleName : public Node {
std::vector<std::unique_ptr<AnnotatedName>> names;
};
struct VariantName : public Node {
std::vector<std::unique_ptr<AnnotatedName>> names;
};
struct AnnotatedName : public Node {
NameIdentifier name;
std::optional<std::unique_ptr<ParametrizedType>> type; // optional
};
// TODO ?? mark all optional fields ??
// Type -----------------
struct TypeConstructor : public Node {
std::unique_ptr<ParametrizedType> type;
std::vector<std::pair<NameIdentifier, SubExpression>> parameters;
};
struct TupleType : public Node {
TypeIdentifierDefinition type; // optional
std::vector<std::pair<NameIdentifier, AnyType>> entities; // NameIdentifier is optional
};
struct VariantType : public Node {
TypeIdentifierDefinition type; // optional
std::vector<std::pair<TypeIdentifierDefinition, std::unique_ptr<TupleType>>> constructors;
};
struct AnnotatedType : public Node {
std::unique_ptr<TypeExpression> type_expression;
std::vector<std::unique_ptr<ParametrizedTypeclass>> annotations;
};
using TypeParameter = std::variant<
std::unique_ptr<ParametrizedType>,
std::unique_ptr<Expression>>;
struct ParametrizedType : public Node {
std::unique_ptr<TypeExpression> type_expression;
std::vector<TypeParameter> parameters;
};
struct TypeExpression : public Node {
std::vector<TypeSubExpression> namespaces;
AnyTypeIdentifier type;
};
// Typeclass -----------------
struct AnnotatedTypeclass : public Node {
std::unique_ptr<TypeclassExpression> typeclass_expression;
std::vector<std::unique_ptr<ParametrizedTypeclass>> annotations;
};
struct ParametrizedTypeclass : public Node {
std::unique_ptr<TypeclassExpression> typeclass_expression;
std::vector<TypeParameter> parameters;
};
struct TypeclassExpression : public Node {
std::vector<TypeSubExpression> namespaces;
TypeclassIdentifier typeclass;
};
// ----------------- Comments [IGNORE] -----------------
// ----------------- Identifiers, constants, etc. -----------------
struct FloatNumberLiteral : public Node {
double value;
};
struct NumberLiteral : public Node {
int64_t value;
};
struct StringLiteral : public Node {
std::string value;
};
struct CharLiteral : public Node {
char value;
};
} // namespace tokens
} // namespace interpereter