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;
};

View file

@ -8,124 +8,116 @@ namespace parser::tokens {
const std::string SourceFile = "source_file";
const std::string Sources = "sources";
const std::string SourceStatement = "source_statement";
// Namespaces, partitions -----------------
const std::string Partition = "partition";
const std::string Namespace = "namespace";
const std::string Partition = "partition";
const std::string PartitionName = "partition_name";
// Definitions -----------------
const std::string ImportStatement = "import_statement";
const std::string UsageDefinition = "usage_definition";
const std::string AliasDefinition = "alias_definition";
const std::string VariableDefinition = "variable_definition";
const std::string AliasDefinitionStatement = "alias_definition_statement";
const std::string VariableDefinitionStatement = "variable_definition_statement";
const std::string FunctionDeclaration = "function_declaration";
const std::string FunctionDefinition = "function_definition";
const std::string AliasTypeDefinition = "alias_type_definition";
const std::string TypeDefinition = "type_definition";
const std::string TypeclassDefinition = "typeclass_definition";
const std::string SourceStatement = "source_statement";
const std::string ImportSymbol = "import_symbol";
const std::string FunctionDefinitionStatement = "function_definition_statement";
const std::string TypeDefinitionStatement = "type_definition_statement";
const std::string AbstractTypeDefinitionStatement = "abstract_type_definition_statement";
const std::string TypeclassDefinitionStatement = "typeclass_definition_statement";
// Definition parts
const std::string DefinedName = "defined_name";
const std::string DefinedAnnotatedName = "defined_annotated_name";
const std::string DefinedType = "defined_type";
const std::string DefinedTypeclass = "defined_typeclass";
const std::string ImportSymbol = "import_symbol";
const std::string FunctionDefinition = "function_definition";
const std::string TypeDefinition = "type_definition";
const std::string DefinitionParameter = "definition_parameter";
const std::string DefinitionArgument = "definition_argument";
const std::string FunctionDeclarationType = "function_declaration_type";
// Flow control -----------------
const std::string MatchCase = "match_case";
const std::string Match = "match";
const std::string Condition = "condition";
const std::string DoWhileLoop = "do_while_loop";
const std::string WhileLoop = "while_loop";
const std::string ForLoop = "for_loop";
const std::string LoopLoop = "loop_loop";
const std::string FlowControl = "flow_control";
// Statements, expressions, blocks, etc. -----------------
const std::string BlockStatement = "block_statement";
const std::string Block = "block";
const std::string ScopedStatement = "scoped_statement";
const std::string LoopControlExpression = "loop_control_expression";
const std::string SubExpressionToken = "subexpression_token";
const std::string SubExpression = "subexpression";
const std::string PrefixedExpression = "prefixed_expression";
const std::string Expression = "expression";
const std::string SuperExpression = "superexpression";
const std::string BlockStatement = "block_statement";
const std::string ScopedStatement = "scoped_statement";
// Operators
const std::string BinaryOperatorExpression = "binary_operator_expression";
const std::string UnaryOperatorExpression = "unary_operator_expression";
const std::string ReferenceExpression = "reference_expression";
// Simple Expressions
// Other expressions
const std::string FunctionArgument = "function_argument";
const std::string FunctionCallExpression = "function_call_expression";
const std::string TupleExpression = "tuple_expression";
const std::string VariantExpression = "variant_expression";
const std::string ReturnExpression = "return_expression";
const std::string FunctionArgument = "function_argument";
// Lambda
const std::string TypeConstructor = "type_constructor";
const std::string LambdaFunction = "lambda_function";
const std::string ArrayExpression = "array_expression";
const std::string LoopControlExpression = "loop_control_expression";
// Name
const std::string NameSuperExpression = "name_superexpression";
const std::string NameExpression = "name_expression";
const std::string TupleName = "tuple_name";
const std::string VariantName = "variant_name";
const std::string AnnotatedName = "annotated_name";
const std::string NameSubExpression = "name_subexpression";
const std::string AnyName = "any_name";
const std::string ScopedAnyName = "scoped_any_name";
// Type, typeclass, etc. -----------------
// Type
const std::string TypeConstructor = "type_constructor";
const std::string FunctionType = "function_type";
const std::string TupleType = "tuple_type";
const std::string VariantType = "variant_type";
const std::string AnnotatedType = "annotated_type";
const std::string ParametrizedType = "parametrized_type";
const std::string TypeExpression = "type_expression";
const std::string Constructor = "constructor";
const std::string AnyType = "any_type";
const std::string TypeSubExpression = "type_subexpression";
const std::string TypeParameter = "type_parameter";
const std::string ScopedAnyType = "scoped_any_type";
const std::string ExtendedScopedAnyType = "extended_scoped_any_type";
// Typeclass
const std::string AnnotatedTypeclass = "annotated_typeclass";
const std::string TypeclassUsage = "typeclass_usage";
const std::string ParametrizedTypeclass = "parametrized_typeclass";
const std::string TypeclassExpression = "typeclass_expression";
// Typeclass & Type
const std::string TypeParameter = "type_parameter";
const std::string TypeSubExpression = "type_subexpression";
// Identifiers, constants, etc. -----------------
const std::string TypeIdentifierDefinition = "type_identifier_definition";
const std::string ExtendedName = "extended_name";
const std::string TypeclassIdentifier = "typeclass_identifier";
const std::string NameIdentifier = "name_identifier";
const std::string TypeIdentifier = "type_identifier";
const std::string AbstractTypeIdentifier = "abstract_type_identifier";
const std::string OperatorIdentifier = "operator_identifier";
//
const std::string OperatorIdentifier = "operator";
const std::string FloatNumberLiteral = "float_number_literal";
const std::string NumberLiteral = "number_literal";
@ -134,6 +126,4 @@ const std::string CharLiteral = "char_literal";
const std::string Literal = "literal";
const std::string NameSubSuperExpression = "name_subsuperexpression";
} // namespace parser::tokens