lang_2023/include/interpreter_tree.hpp
2023-03-26 16:19:30 +03:00

567 lines
12 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <variant>
#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<
ImportStatement,
UsageDefinition,
AliasDefinition,
VariableDefinition, // ??
FunctionDeclaration,
FunctionDefinition,
AliasTypeDefinition,
TypeDefinition,
TypeclassDefinition,
Namespace>;
// Definition parts
struct ParametrizedType;
struct TupleType;
struct VariantType;
struct ParametrizedTypeclass;
using FunctionDeclarationType = std::variant<
ParametrizedType,
TupleType,
VariantType,
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<
Match,
Condition,
WhileLoop,
ForLoop,
LoopLoop>;
// Statements, expressions, blocks, etc. -----------------
struct Block;
//
struct NameExpression;
struct ScopedStatement;
using SubExpressionToken = std::variant<
NameExpression,
ScopedStatement>;
//
struct FunctionCallExpression;
struct BinaryOperatorExpression;
using SubExpression = std::variant<
FunctionCallExpression,
BinaryOperatorExpression,
SubExpressionToken>;
//
struct ReturnExpression;
enum class LoopControlExpression {
Break,
Continue,
};
using PrefixedExpression = std::variant<
ReturnExpression,
LoopControlExpression,
Block>;
//
struct LambdaFunction;
struct TypeConstructor;
struct UnaryOperatorExpression;
using Expression = std::variant<
LambdaFunction,
TypeConstructor,
PrefixedExpression,
UnaryOperatorExpression,
SubExpression>;
//
struct TupleExpression;
struct VariantExpression;
using SuperExpression = std::variant<
FlowControl,
TupleExpression,
VariantExpression,
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<
AnnotatedName,
TupleName,
VariantName>;
// Type
struct TypeConstructor;
// // TypeOrAnyType <-> AnyType
struct TupleType;
struct VariantType;
struct VariantType;
using AnyType = std::variant<
ParametrizedType,
TupleType,
VariantType>;
// // TypeIdentifierDefinition <-> some '.' + TypeIdentifier
using TypeIdentifierDefinition = std::string;
struct AnnotatedType;
// // TypeAnnotations - inplace ??
struct ParametrizedType;
struct TypeExpression;
using TypeSubExpression = std::variant<
AnyTypeIdentifier,
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<
FloatNumberLiteral,
NumberLiteral,
StringLiteral,
CharLiteral>;
//
using NameSubSuperExpression = std::variant<
NameIdentifier,
Literal,
SuperExpression>;
// ----------------- Sources -----------------
struct SourceFile : public Node {
std::vector<std::variant<SourceStatement, Partition>> statements;
};
struct Sources : public Node {
std::vector<SourceStatement> statemets;
};
// ----------------- Namespaces, partittions -----------------
struct Partition : public Node {
enum class PartitionType {
Test,
Interface,
Core,
Lib,
Module, // rename ??
Exe,
};
PartitionType type;
std::unique_ptr<Sources> scope;
};
struct Namespace : public Node {
std::unique_ptr<std::variant<DefinedAnnotatedName, DefinedType>> name;
// TODO add const / var specification
std::unique_ptr<Sources> scope;
};
// ----------------- Definitions -----------------
struct TypeclassExpression;
struct TypeExpression;
struct NameExpression;
using ImportSymbol = std::variant<
TypeclassExpression,
TypeExpression,
NameExpression>;
//
struct ImportStatement : public Node {
std::string module_name;
std::vector<std::unique_ptr<ImportSymbol>> symbols;
};
struct UsageDefinition : public Node {
TypeIdentifier module_identifier;
std::unique_ptr<ImportStatement> import_statement;
};
struct AliasDefinition : public Node {
std::unique_ptr<DefinedType> name;
std::unique_ptr<ParametrizedType> value;
};
struct VariableDefinition : public Node {
bool is_const; // default value ??
NameIdentifier name;
std::unique_ptr<SuperExpression> value;
};
struct FunctionDeclaration : public Node {
NameIdentifier name;
std::vector<std::unique_ptr<FunctionDeclarationType>> argument_types;
};
struct FunctionDefintion : public Node {
std::unique_ptr<DefinedName> name;
std::unique_ptr<SuperExpression> value;
};
struct AliasTypeDefinition : public Node {
std::unique_ptr<DefinedType> name;
std::unique_ptr<ParametrizedType> value;
};
struct TypeDefinition : public Node {
std::unique_ptr<DefinedType> name;
std::unique_ptr<AnyType> value;
};
struct TypeclassDefinition : public Node {
std::unique_ptr<DefinedTypeclass> name;
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::unique_ptr<std::variant<DefinedType, 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 abstract_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 {
std::unique_ptr<Expression> value;
std::unique_ptr<Expression> condition;
std::unique_ptr<Expression> statement;
};
struct Match : public Node {
std::unique_ptr<Expression> value;
std::vector<MatchCase> matches;
};
struct Condition : public Node {
std::vector<std::unique_ptr<Expression>> conditions; // if, elif
std::vector<std::unique_ptr<Expression>> stetemets; // if, elif, else
};
struct DoWhileLoop : public Node {
std::unique_ptr<Expression> consition;
std::unique_ptr<Expression> statement;
};
struct WhileLoop : public Node {
std::unique_ptr<Expression> condition;
std::unique_ptr<Expression> statement;
};
struct ForLoop : public Node {
std::unique_ptr<AnyName> variable;
std::unique_ptr<Expression> interval;
std::unique_ptr<Expression> statement;
};
struct LoopLoop : public Node {
std::unique_ptr<Expression> statement;
};
// ----------------- Statements, expressions, blocks, etc. -----------------
using BlockStatement = std::variant<
Expression,
AliasDefinition,
VariableDefinition,
FlowControl,
PrefixedExpression>;
struct Block : public Node {
std::vector<std::unique_ptr<BlockStatement>> statements;
};
struct ScopedStatement : public Node {
std::unique_ptr<SuperExpression> statement;
};
// Operators -----------------
struct BinaryOperatorExpression : public Node {
OperatorIdentifier operator_name;
std::unique_ptr<SubExpression> left_expression;
std::unique_ptr<SubExpression> right_expression;
};
struct UnaryOperatorExpression : public Node { // TODO fix prescendence
OperatorIdentifier operator_name;
std::unique_ptr<Expression> expression;
};
// Simple Expressions -----------------
using FunctionArgument = std::variant<SubExpressionToken, TypeSubExpression>;
struct FunctionCallExpression : public Node {
std::unique_ptr<NameSuperExpression> name;
std::vector<std::unique_ptr<FunctionArgument>> arguments;
};
struct TupleExpression : public Node {
std::vector<std::unique_ptr<SubExpression>> expressions;
};
struct VariantExpression : public Node {
std::vector<std::unique_ptr<SubExpression>> expressions;
};
struct ReturnExpression : public Node {
std::unique_ptr<Expression> expression;
};
// Lambda -----------------
struct LambdaFunction : public Node {
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
std::unique_ptr<Expression> expression;
};
// Name -----------------
struct NameSuperExpression : public Node {
std::vector<std::unique_ptr<TypeSubExpression>> namespaces;
std::vector<std::unique_ptr<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::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, std::unique_ptr<SubExpression>>> parameters;
};
struct TupleType : public Node {
TypeIdentifierDefinition type; // optional
std::vector<std::pair<NameIdentifier, std::unique_ptr<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<ParametrizedType, Expression>;
struct ParametrizedType : public Node {
std::unique_ptr<TypeExpression> type_expression;
std::vector<std::unique_ptr<TypeParameter>> parameters;
};
struct TypeExpression : public Node {
std::vector<std::unique_ptr<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<std::unique_ptr<TypeParameter>> parameters;
};
struct TypeclassExpression : public Node {
std::vector<std::unique_ptr<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