grammar refactoring: interpreter_tree, parse_token_types fixed

This commit is contained in:
ProgramSnail 2023-04-09 13:43:01 +03:00
parent 0d98183953
commit ff893d3ebd
2 changed files with 221 additions and 250 deletions

View file

@ -36,45 +36,31 @@ struct Namespace;
// Definitions -----------------
struct ImportStatement;
struct UsageDefinition;
struct AliasDefinition;
struct VariableDefinition;
struct AliasDefinitionStatement;
struct VariableDefinitionStatement;
struct FunctionDeclaration;
struct FunctionDefinition;
struct AliasTypeDefinition;
struct TypeDefinition;
struct TypeclassDefinition;
struct FunctionDefinitionStatement;
struct TypeDefinitionStatement;
struct AbstractTypeDefinitionStatement;
struct TypeclassDefinitionStatement;
using SourceStatement = std::variant<
std::unique_ptr<ImportStatement>,
std::unique_ptr<UsageDefinition>,
std::unique_ptr<AliasDefinition>,
std::unique_ptr<VariableDefinition>, // ??
std::unique_ptr<AliasDefinitionStatement>,
std::unique_ptr<FunctionDeclaration>,
std::unique_ptr<FunctionDefinition>,
std::unique_ptr<AliasTypeDefinition>,
std::unique_ptr<TypeDefinition>,
std::unique_ptr<TypeclassDefinition>,
std::unique_ptr<FunctionDefinitionStatement>,
std::unique_ptr<TypeDefinitionStatement>,
std::unique_ptr<AbstractTypeDefinitionStatement>,
std::unique_ptr<TypeclassDefinitionStatement>,
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>>;
using ImportSymbol = AnyIdentifier;
struct DefinedName;
struct DefinedAnnotatedName;
struct DefinedType;
struct DefinedTypeclass;
struct FunctionDefinition;
struct TypeDefinition;
struct DefinitionParameter;
struct DefinitionArgument;
// Flow control -----------------
@ -99,30 +85,34 @@ struct Block;
//
struct NameSuperExpression;
struct NameExpression;
struct ScopedStatement;
enum class LoopControlExpression {
Break,
Continue,
};
using SubExpressionToken = std::variant<
std::unique_ptr<NameSuperExpression>,
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<SubExpressionToken>,
std::unique_ptr<ArrayExpression>,
std::unique_ptr<ReferenceExpression>>;
//
enum class LoopControlExpression {
Break,
Continue,
};
struct ReturnExpression;
using PrefixedExpression = std::variant<
@ -156,73 +146,95 @@ using SuperExpression = std::variant<
//
struct ScopedStatement; // ?? scoped statement is _superexpression ??
struct ScopedStatement;
// Operators
struct BinaryOperatorExpression;
struct UnaryOperatorExpression;
struct ReferenceExpression;
// Simple Expressions
// Other Expressions
struct FunctionCallExpression;
struct TypeExpression;
using FunctionArgument = std::variant<
SubExpressionToken,
std::unique_ptr<TypeExpression>>;
struct TupleExpression;
struct VariantExpression;
struct ReturnExpression;
// Lambda
struct TypeConstructor;
struct LambdaFunction;
struct ArrayExpression;
// Name
struct NameSuperExpression;
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 TypeConstructor;
// // TypeOrAnyType <-> AnyType
struct FunctionType;
struct TupleType;
struct VariantType;
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<VariantType>,
std::unique_ptr<FunctionType>>;
// // TypeIdentifierDefinition <-> some '.' + TypeIdentifier
using TypeIdentifierDefinition = std::string;
using ScopedAnyType = AnyType;
struct AnnotatedType;
// // TypeAnnotations - inplace ??
struct ParametrizedType;
struct TypeExpression;
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>>;
// Typeclass
struct AnnotatedTypeclass;
struct ParametrizedTypeclass;
struct TypeclassExpression;
// Comments [IGNORE] -----------------
// Identifiers, constants, etc. -----------------
using ExtendedName = std::string;
struct FloatNumberLiteral;
struct NumberLiteral;
struct StringLiteral;
@ -236,143 +248,115 @@ using Literal = std::variant<
//
using NameSubSuperExpression = std::variant<
using NameSubExpression = std::variant<
std::unique_ptr<NameIdentifier>,
std::unique_ptr<Literal>,
std::unique_ptr<SuperExpression>>;
enum class ReferenceType {
Reference,
UniqueReference,
};
////////////////////////////////////////////////////////////////////////////////////////////
// ----------------- Sources -----------------
struct SourceFile : public Node {
struct SourceFile {
std::vector<std::variant<SourceStatement, Partition>> statements;
};
struct Sources : public Node {
struct Sources {
std::vector<SourceStatement> statements;
};
// ----------------- Namespaces, partittions -----------------
struct Partition : public Node {
enum PartitionType {
struct Namespace {
bool is_const;
std::optional<ExtendedName> name;
TypeIdentifier type;
std::unique_ptr<Sources> scope;
};
struct Partition {
enum PartitionName {
Test,
Interface,
Core,
Lib,
Module, // rename ??
Module,
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
PartitionName name;
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 {
struct ImportStatement {
std::optional<TypeIdentifier> name;
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;
struct AliasDefinitionStatement {
TypeIdentifier type;
std::vector<AbstractTypeIdentifier> parameters;
std::unique_ptr<ParametrizedType> value;
};
struct VariableDefinition : public Node {
bool is_const; // default value ??
NameIdentifier name;
struct VariableDefinitionStatement {
enum { Var, Const } modifier;
AnyName name;
SuperExpression value;
};
struct FunctionDeclaration : public Node {
struct FunctionDeclaration {
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<FunctionDeclarationType> argument_types;
std::unique_ptr<FunctionType> type;
};
struct FunctionDefinition : public Node {
std::unique_ptr<DefinedName> name;
struct FunctionDefinitionStatement {
std::unique_ptr<FunctionDefinition> definition;
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;
struct TypeDefinitionStatement {
enum { Struct, Class } modifier;
std::unique_ptr<TypeDefinition> definition;
AnyType value;
};
struct TypeclassDefinition : public Node {
std::unique_ptr<DefinedTypeclass> typeclass;
struct AbstractTypeDefinitionStatement {
enum { Basic, Abstract } modifier;
std::unique_ptr<AnnotatedType> definition;
};
struct TypeclassDefinitionStatement {
std::unique_ptr<TypeDefinition> definition;
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
struct FunctionDefinition {
enum { Operator, Function } modifier;
NameOrOperatorIdentifier name;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
std::vector<ExtendedName> arguments;
};
struct DefinedAnnotatedName : public Node {
NameIdentifier name;
std::variant<
std::unique_ptr<DefinedType>,
std::unique_ptr<DefinedTypeclass>> type;
};
struct DefinedType : public Node {
struct TypeDefinition {
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 {
struct DefinitionParameter {
AbstractTypeIdentifier type;
std::vector<std::unique_ptr<ParametrizedTypeclass>> typeclasses;
};
struct DefinitionArgument : public Node {
NameIdentifier name;
std::vector<std::unique_ptr<ParametrizedType>> types; // TODO: one type
std::vector<std::unique_ptr<TypeclassUsage>> typeclasses;
};
// ----------------- Flow control -----------------
@ -383,33 +367,33 @@ struct MatchCase {
std::optional<Expression> statement;
};
struct Match : public Node {
struct Match {
Expression value;
std::vector<MatchCase> matches;
};
struct Condition : public Node {
struct Condition {
std::vector<Expression> conditions; // if, elif
std::vector<Expression> statements; // if, elif, else
};
struct DoWhileLoop : public Node {
struct DoWhileLoop {
Expression condition;
Expression statement;
};
struct WhileLoop : public Node {
struct WhileLoop {
Expression condition;
Expression statement;
};
struct ForLoop : public Node {
struct ForLoop {
AnyName variable;
Expression interval;
Expression statement;
};
struct LoopLoop : public Node {
struct LoopLoop {
Expression statement;
};
@ -417,140 +401,137 @@ struct LoopLoop : public Node {
using BlockStatement = std::variant<
std::unique_ptr<Expression>,
std::unique_ptr<AliasDefinition>,
std::unique_ptr<VariableDefinition>,
std::unique_ptr<VariableDefinitionStatement>,
std::unique_ptr<FlowControl>,
std::unique_ptr<PrefixedExpression>>;
struct Block : public Node {
struct Block {
std::vector<BlockStatement> statements;
};
struct ScopedStatement : public Node {
struct ScopedStatement {
SuperExpression statement;
};
// Operators -----------------
struct BinaryOperatorExpression : public Node {
struct BinaryOperatorExpression {
OperatorIdentifier operator_name;
SubExpression left_expression;
SubExpression right_expression;
};
struct UnaryOperatorExpression : public Node { // TODO fix prescendence
struct UnaryOperatorExpression {
OperatorIdentifier operator_name;
Expression expression;
};
struct ReferenceExpression {
std::vector<ReferenceType> references;
ScopedStatement expression;
}
// Simple Expressions -----------------
using FunctionArgument = std::variant<
std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeSubExpression>>;
struct FunctionCallExpression : public Node {
std::unique_ptr<NameSuperExpression> name;
struct FunctionCallExpression {
std::unique_ptr<NameExpression> name;
std::vector<FunctionArgument> arguments;
};
struct TupleExpression : public Node {
struct TupleExpression {
std::vector<SubExpression> expressions;
};
struct VariantExpression : public Node {
struct VariantExpression {
std::vector<SubExpression> expressions;
};
struct ReturnExpression : public Node {
struct ReturnExpression {
Expression expression;
};
// Lambda -----------------
struct TypeConstructor {
std::unique_ptr<ParametrizedType> type;
std::vector<std::pair<ExtendedName, SubExpression>> parameters;
};
struct LambdaFunction : public Node {
struct LambdaFunction {
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<std::unique_ptr<DefinitionArgument>> arguments;
std::vector<ExtendedName> arguments;
Expression expression;
};
struct ArrayExpression {
std::vector<Expression> elements;
};
// Name -----------------
struct NameSuperExpression : public Node {
struct NameExpression {
std::vector<TypeSubExpression> namespaces;
std::vector<NameSubSuperExpression> expressions;
std::vector<NameSubExpression> expressions;
};
struct NameExpression : public Node {
std::vector<TypeSubExpression> namespaces;
std::vector<NameIdentifier> names;
struct TupleName {
std::vector<std::unique_ptr<ScopedAnyName>> names;
};
struct TupleName : public Node {
std::vector<std::unique_ptr<AnnotatedName>> names;
struct VariantName {
std::vector<std::unique_ptr<ScopedAnyName>> names;
};
struct VariantName : public Node {
std::vector<std::unique_ptr<AnnotatedName>> names;
};
struct AnnotatedName : public Node {
struct AnnotatedName {
NameIdentifier name;
std::optional<std::unique_ptr<ParametrizedType>> type; // optional
std::optional<std::unique_ptr<ScopedAnyType>> type;
};
// TODO ?? mark all optional fields ??
// Type -----------------
struct TypeConstructor : public Node {
std::unique_ptr<ParametrizedType> type;
std::vector<std::pair<NameIdentifier, SubExpression>> parameters;
struct FunctionType {
std::vector<ScopedAnyType> types;
};
struct TupleType : public Node {
std::optional<TypeIdentifierDefinition> type;
std::vector<std::pair<std::optional<NameIdentifier>, AnyType>> entities;
struct TupleType {
std::optional<Constructor> type;
std::vector<std::pair<std::optional<ExtendedName>, ExtendedScopedAnyType>> entities;
};
struct VariantType : public Node {
std::optional<TypeIdentifierDefinition> type;
std::vector<std::variant<TypeIdentifierDefinition, std::unique_ptr<TupleType>>> constructors;
struct VariantType {
std::optional<Constructor> type;
std::vector<std::variant<Constructor, std::unique_ptr<TupleType>>> constructors;
};
struct AnnotatedType : public Node {
struct AnnotatedType {
std::unique_ptr<TypeExpression> type_expression;
std::vector<std::unique_ptr<ParametrizedTypeclass>> annotations;
std::vector<std::unique_ptr<TypeclassUsage>> annotations;
};
using TypeParameter = std::variant<
std::unique_ptr<TypeExpression>,
std::unique_ptr<ParametrizedType>,
std::unique_ptr<Expression>>;
struct ParametrizedType : public Node {
struct ParametrizedType {
std::unique_ptr<TypeExpression> type_expression;
std::vector<TypeParameter> parameters;
};
struct TypeExpression : public Node {
struct TypeExpression {
std::vector<TypeSubExpression> namespaces;
AnyTypeIdentifier type;
std::optional<size_t> ArraySize; // if array; 0 - dynamic size
};
struct ExtendedScopedAnyType {
std::vector<ReferenceType> references;
AnyType type;
}
// Typeclass -----------------
struct AnnotatedTypeclass : public Node {
std::unique_ptr<TypeclassExpression> typeclass_expression;
std::vector<std::unique_ptr<ParametrizedTypeclass>> annotations;
};
struct ParametrizedTypeclass : public Node {
struct ParametrizedTypeclass {
std::unique_ptr<TypeclassExpression> typeclass_expression;
std::vector<TypeParameter> parameters;
};
struct TypeclassExpression : public Node {
struct TypeclassExpression {
std::vector<TypeSubExpression> namespaces;
TypeclassIdentifier typeclass;
};
@ -558,19 +539,19 @@ struct TypeclassExpression : public Node {
// ----------------- Comments [IGNORE] -----------------
// ----------------- Identifiers, constants, etc. -----------------
struct FloatNumberLiteral : public Node {
struct FloatNumberLiteral {
double value;
};
struct NumberLiteral : public Node {
struct NumberLiteral {
int64_t value;
};
struct StringLiteral : public Node {
struct StringLiteral {
std::string value;
};
struct CharLiteral : public Node {
struct CharLiteral {
char value;
};