part of print_visitor + interpreter_tree fixes

This commit is contained in:
ProgramSnail 2023-03-26 23:15:42 +03:00
parent f0a95ee2df
commit c8b52f9ade
8 changed files with 417 additions and 163 deletions

View file

@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include <variant>
#include <optional>
#include <memory>
// for clangd
@ -47,16 +48,16 @@ struct TypeDefinition;
struct TypeclassDefinition;
using SourceStatement = std::variant<
ImportStatement,
UsageDefinition,
AliasDefinition,
VariableDefinition, // ??
FunctionDeclaration,
FunctionDefinition,
AliasTypeDefinition,
TypeDefinition,
TypeclassDefinition,
Namespace>;
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
@ -65,10 +66,10 @@ struct TupleType;
struct VariantType;
struct ParametrizedTypeclass;
using FunctionDeclarationType = std::variant<
ParametrizedType,
TupleType,
VariantType,
ParametrizedTypeclass>;
std::unique_ptr<ParametrizedType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>,
std::unique_ptr<ParametrizedTypeclass>>;
struct DefinedName;
struct DefinedAnnotatedName;
@ -87,11 +88,12 @@ struct ForLoop;
struct LoopLoop;
using FlowControl = std::variant<
Match,
Condition,
WhileLoop,
ForLoop,
LoopLoop>;
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. -----------------
@ -102,9 +104,14 @@ struct Block;
struct NameExpression;
struct ScopedStatement;
enum class LoopControlExpression {
Break,
Continue,
};
using SubExpressionToken = std::variant<
NameExpression,
ScopedStatement>;
std::unique_ptr<NameExpression>,
std::unique_ptr<ScopedStatement>>;
//
@ -112,23 +119,18 @@ struct FunctionCallExpression;
struct BinaryOperatorExpression;
using SubExpression = std::variant<
FunctionCallExpression,
BinaryOperatorExpression,
SubExpressionToken>;
std::unique_ptr<FunctionCallExpression>,
std::unique_ptr<BinaryOperatorExpression>,
std::unique_ptr<SubExpressionToken>>;
//
struct ReturnExpression;
enum class LoopControlExpression {
Break,
Continue,
};
using PrefixedExpression = std::variant<
ReturnExpression,
LoopControlExpression,
Block>;
std::unique_ptr<ReturnExpression>,
std::unique_ptr<LoopControlExpression>,
std::unique_ptr<Block>>;
//
@ -137,11 +139,11 @@ struct TypeConstructor;
struct UnaryOperatorExpression;
using Expression = std::variant<
LambdaFunction,
TypeConstructor,
PrefixedExpression,
UnaryOperatorExpression,
SubExpression>;
std::unique_ptr<LambdaFunction>,
std::unique_ptr<TypeConstructor>,
std::unique_ptr<PrefixedExpression>,
std::unique_ptr<UnaryOperatorExpression>,
std::unique_ptr<SubExpression>>;
//
@ -149,10 +151,10 @@ struct TupleExpression;
struct VariantExpression;
using SuperExpression = std::variant<
FlowControl,
TupleExpression,
VariantExpression,
Expression>;
std::unique_ptr<FlowControl>,
std::unique_ptr<TupleExpression>,
std::unique_ptr<VariantExpression>,
std::unique_ptr<Expression>>;
//
@ -183,9 +185,9 @@ struct VariantName;
struct AnnotatedName;
using AnyName = std::variant<
AnnotatedName,
TupleName,
VariantName>;
std::unique_ptr<AnnotatedName>,
std::unique_ptr<TupleName>,
std::unique_ptr<VariantName>>;
// Type
@ -198,9 +200,9 @@ struct VariantType;
struct VariantType;
using AnyType = std::variant<
ParametrizedType,
TupleType,
VariantType>;
std::unique_ptr<ParametrizedType>,
std::unique_ptr<TupleType>,
std::unique_ptr<VariantType>>;
// // TypeIdentifierDefinition <-> some '.' + TypeIdentifier
using TypeIdentifierDefinition = std::string;
@ -211,8 +213,8 @@ struct ParametrizedType;
struct TypeExpression;
using TypeSubExpression = std::variant<
AnyTypeIdentifier,
ParametrizedType>;
std::unique_ptr<AnyTypeIdentifier>,
std::unique_ptr<ParametrizedType>>;
// Typeclass
@ -229,17 +231,17 @@ struct StringLiteral;
struct CharLiteral;
using Literal = std::variant<
FloatNumberLiteral,
NumberLiteral,
StringLiteral,
CharLiteral>;
std::unique_ptr<FloatNumberLiteral>,
std::unique_ptr<NumberLiteral>,
std::unique_ptr<StringLiteral>,
std::unique_ptr<CharLiteral>>;
//
using NameSubSuperExpression = std::variant<
NameIdentifier,
Literal,
SuperExpression>;
std::unique_ptr<NameIdentifier>,
std::unique_ptr<Literal>,
std::unique_ptr<SuperExpression>>;
// ----------------- Sources -----------------
@ -248,13 +250,13 @@ struct SourceFile : public Node {
};
struct Sources : public Node {
std::vector<SourceStatement> statemets;
std::vector<SourceStatement> statements;
};
// ----------------- Namespaces, partittions -----------------
struct Partition : public Node {
enum class PartitionType {
enum PartitionType {
Test,
Interface,
Core,
@ -268,7 +270,9 @@ struct Partition : public Node {
};
struct Namespace : public Node {
std::unique_ptr<std::variant<DefinedAnnotatedName, DefinedType>> name;
std::variant<
std::unique_ptr<DefinedAnnotatedName>,
std::unique_ptr<DefinedType>> name;
// TODO add const / var specification
std::unique_ptr<Sources> scope;
};
@ -280,41 +284,42 @@ struct TypeExpression;
struct NameExpression;
using ImportSymbol = std::variant<
TypeclassExpression,
TypeExpression,
NameExpression>;
std::unique_ptr<TypeclassExpression>,
std::unique_ptr<TypeExpression>,
std::unique_ptr<NameExpression>>;
//
struct ImportStatement : public Node {
std::string module_name;
std::vector<std::unique_ptr<ImportSymbol>> symbols;
std::vector<ImportSymbol> symbols;
};
struct UsageDefinition : public Node {
TypeIdentifier module_identifier;
TypeIdentifier name; // rename ??
std::unique_ptr<ImportStatement> import_statement;
};
struct AliasDefinition : public Node {
std::unique_ptr<DefinedType> name;
std::unique_ptr<DefinedType> type;
std::unique_ptr<ParametrizedType> value;
};
struct VariableDefinition : public Node {
bool is_const; // default value ??
NameIdentifier name;
std::unique_ptr<SuperExpression> value;
SuperExpression value;
};
struct FunctionDeclaration : public Node {
NameIdentifier name;
std::vector<std::unique_ptr<FunctionDeclarationType>> argument_types;
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
std::vector<FunctionDeclarationType> argument_types;
};
struct FunctionDefintion : public Node {
struct FunctionDefinition : public Node {
std::unique_ptr<DefinedName> name;
std::unique_ptr<SuperExpression> value;
SuperExpression value;
};
struct AliasTypeDefinition : public Node {
@ -324,7 +329,7 @@ struct AliasTypeDefinition : public Node {
struct TypeDefinition : public Node {
std::unique_ptr<DefinedType> name;
std::unique_ptr<AnyType> value;
AnyType value;
};
struct TypeclassDefinition : public Node {
@ -343,7 +348,9 @@ struct DefinedName : public Node {
struct DefinedAnnotatedName : public Node {
NameIdentifier name;
std::unique_ptr<std::variant<DefinedType, DefinedTypeclass>> type;
std::variant<
std::unique_ptr<DefinedType>,
std::unique_ptr<DefinedTypeclass>> type;
};
struct DefinedType : public Node {
@ -359,7 +366,7 @@ struct DefinedTypeclass : public Node {
};
struct DefinitionParameter : public Node {
AbstractTypeIdentifier abstract_type;
AbstractTypeIdentifier type;
std::vector<std::unique_ptr<ParametrizedTypeclass>> typeclasses;
};
@ -371,74 +378,76 @@ struct DefinitionArgument : public Node {
// ----------------- Flow control -----------------
struct MatchCase {
std::unique_ptr<Expression> value;
std::unique_ptr<Expression> condition;
std::unique_ptr<Expression> statement;
Expression value;
std::optional<Expression> condition;
std::optional<Expression> statement;
};
struct Match : public Node {
std::unique_ptr<Expression> value;
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
std::vector<Expression> conditions; // if, elif
std::vector<Expression> statements; // if, elif, else
};
struct DoWhileLoop : public Node {
std::unique_ptr<Expression> consition;
std::unique_ptr<Expression> statement;
Expression condition;
Expression statement;
};
struct WhileLoop : public Node {
std::unique_ptr<Expression> condition;
std::unique_ptr<Expression> statement;
Expression condition;
Expression statement;
};
struct ForLoop : public Node {
std::unique_ptr<AnyName> variable;
std::unique_ptr<Expression> interval;
std::unique_ptr<Expression> statement;
AnyName variable;
Expression interval;
Expression statement;
};
struct LoopLoop : public Node {
std::unique_ptr<Expression> statement;
Expression statement;
};
// ----------------- Statements, expressions, blocks, etc. -----------------
using BlockStatement = std::variant<
Expression,
AliasDefinition,
VariableDefinition,
FlowControl,
PrefixedExpression>;
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<std::unique_ptr<BlockStatement>> statements;
std::vector<BlockStatement> statements;
};
struct ScopedStatement : public Node {
std::unique_ptr<SuperExpression> statement;
SuperExpression statement;
};
// Operators -----------------
struct BinaryOperatorExpression : public Node {
OperatorIdentifier operator_name;
std::unique_ptr<SubExpression> left_expression;
std::unique_ptr<SubExpression> right_expression;
SubExpression left_expression;
SubExpression right_expression;
};
struct UnaryOperatorExpression : public Node { // TODO fix prescendence
OperatorIdentifier operator_name;
std::unique_ptr<Expression> expression;
Expression expression;
};
// Simple Expressions -----------------
using FunctionArgument = std::variant<SubExpressionToken, TypeSubExpression>;
using FunctionArgument = std::variant<
std::unique_ptr<SubExpressionToken>,
std::unique_ptr<TypeSubExpression>>;
struct FunctionCallExpression : public Node {
std::unique_ptr<NameSuperExpression> name;
@ -514,7 +523,9 @@ struct AnnotatedType : public Node {
std::vector<std::unique_ptr<ParametrizedTypeclass>> annotations;
};
using TypeParameter = std::variant<ParametrizedType, Expression>;
using TypeParameter = std::variant<
std::unique_ptr<ParametrizedType>,
std::unique_ptr<Expression>>;
struct ParametrizedType : public Node {
std::unique_ptr<TypeExpression> type_expression;