or references, prining improvements, comments now printed, fixes

This commit is contained in:
ProgramSnail 2023-07-31 22:07:32 +03:00
parent 73263193a9
commit 5e70f0015f
19 changed files with 354 additions and 429 deletions

View file

@ -56,6 +56,10 @@ nodes::Identifier build_operator(parser::ParseTree::Node parser_node);
nodes::Identifier build_placeholder(parser::ParseTree::Node parser_node);
// --- extra
nodes::Extra build_extra(parser::ParseTree::Node parser_node);
// --- empty lines
nodes::EmptyLines build_empty_lines(parser::ParseTree::Node parser_node);

View file

@ -7,14 +7,28 @@
namespace nodes {
// replace enum with structure ??
enum class Modifier {
OUT, // -> x
IN, // <- x
REF, // <> x // IN and OUT
OR_OUT, // |-> // OUT or NONE
OR_IN, // <-| // IN or NONE
OPTIONAL, // x?
RESULT, // x!
IN, // <- x
REF, // <> x
CONST, // -- x (or nothing sometimes)
OUT, // -> x
//
IN_OR_REF, // <-|<> x
IN_OR_CONST, // <-|-- x
REF_OR_OUT, // <>|-> x
CONST_OR_OUT, // --|-> x
REF_OR_CONST, // <>|-- x
IN_OR_OUT, // <-|-> x
//
IN_OR_REF_OR_OUT, // <-|<>|-> x
IN_OR_CONST_OR_OUT, // <-|--|-> x
IN_OR_REF_OR_CONST, // <-|<>|-- x
REF_OR_CONST_OR_OUT, //<>|--|-> x
//
IN_OR_REF_OR_CONST_OR_OUT, //<-|<>|--|-> x
OPTIONAL, // x?
RESULT, // x!
NONE,
};
@ -86,23 +100,51 @@ public:
IdentifierType get_type() const { return type_; }
//
std::string *get() { return &value_; }
const std::string *get() const { return &value_; }
//
void append_before(const std::string &name) { value_ = name + "." + value_; }
void append_after(const std::string &name) {
value_ += ".";
value_ += name;
}
private:
IdentifierType type_;
std::string value_;
};
class EmptyLines : public Node {
class Extra : public Node {
public:
EmptyLines(Node node, size_t size) : Node(node), size_(size) {}
Extra(Node node, std::string &&content)
: Node(node), content_(std::move(content)) {}
size_t size() const { return size_; }
Extra(Node node, const std::string &content)
: Node(node), content_(content) {}
std::string *content() { return &content_; }
const std::string *content() const { return &content_; }
private:
size_t size_;
std::string content_;
};
class EmptyLines : public Node {
public:
EmptyLines(Node node, size_t line_count)
: Node(node), line_count_(line_count) {}
size_t line_count() const { return line_count_; }
private:
size_t line_count_;
};
} // namespace nodes

View file

@ -42,6 +42,12 @@ public:
print_spaces(indentation);
}
void to_indentation_level() {
if (indentation_level_ > current_position_) {
print_spaces(indentation_level_ - current_position_);
}
}
void indent() { indentation_level_ += tab_width_; }
void deindent() { indentation_level_ -= tab_width_; }
@ -118,7 +124,8 @@ private:
size_t indentation_level_ = 0;
};
void print_modifier(const nodes::Modifier &modifier, Printer &printer);
void print_modifier(const nodes::Modifier &modifier, Printer &printer,
bool const_is_none = false);
void print_literal(const nodes::Literal &literal, Printer &printer);
@ -126,6 +133,8 @@ void print_identifier(const nodes::Identifier &identifier, Printer &printer);
void print_annotation(const std::string &annotation, Printer &printer);
void print_extra(const nodes::Extra &extra, Printer &printer);
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer);
} // namespace printers

View file

@ -561,6 +561,9 @@ private:
// --- literal
Literal,
// --- extra
Extra,
// --- empty lines
EmptyLines>
expression_;

View file

@ -16,10 +16,11 @@ build_source_file(parser::ParseTree::Node parser_node,
nodes::TypeStorage &type_storage, names::NameTree &name_tree);
// copy of statement inserted into name_tree
nodes::Statement build_statement(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage,
names::NameTree &name_tree);
nodes::Statement
build_statement(parser::ParseTree::Node parser_node,
std::optional<nodes::Identifier> &previous_defined_type_name,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage, names::NameTree &name_tree);
nodes::Import build_import(parser::ParseTree::Node parser_node);
@ -27,19 +28,12 @@ nodes::Constraint build_constraint(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::TypeDefinition
build_type_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::FunctionDefinition
build_function_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::TypeclassDefinition
build_typeclass_definition(parser::ParseTree::Node parser_node,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
nodes::TypeDefinition build_type_definition(parser::ParseTree::Node parser_node,
nodes::TypeStorage &type_storage);
nodes::FunctionDefinition build_function_definition(
parser::ParseTree::Node parser_node,
const std::optional<nodes::Identifier> &previous_defined_type_name,
nodes::ExpressionStorage &expression_storage,
nodes::TypeStorage &type_storage);
} // namespace builders

View file

@ -73,12 +73,6 @@ private:
class FunctionDefinition : public Node {
public:
enum MethodModifier {
STATIC,
LET,
VAR,
};
class Argument {
public:
Argument(const std::optional<std::string> &annotation, Identifier &&name,
@ -237,16 +231,21 @@ public:
FunctionDefinition(Node node, SymbolDocs &&docs,
std::vector<Constraint> &&constraints,
Modifier return_modifier, MethodModifier method_modifier,
Modifier return_modifier, bool is_method,
const std::optional<Identifier> &name_prefix,
const Identifier &name, std::vector<Argument> &&arguments,
bool are_annotations_same_to_names,
std::optional<ExpressionProxy> expression)
: Node(node), docs_(std::move(docs)),
constraints_(std::move(constraints)), return_modifier_(return_modifier),
method_modifier_(method_modifier), name_(name),
is_method_(is_method), name_(name), full_name_(name),
arguments_(std::move(arguments)),
are_annotations_same_to_names_(are_annotations_same_to_names),
expression_(expression) {}
expression_(expression) {
if (name_prefix.has_value()) {
full_name_.append_before(*name_prefix.value().get());
}
}
//
@ -268,7 +267,7 @@ public:
Modifier get_return_modifier() const { return return_modifier_; }
MethodModifier get_method_modifier() const { return method_modifier_; }
bool is_method() const { return is_method_; }
//
@ -278,6 +277,12 @@ public:
//
Identifier *get_full_name() { return &full_name_; }
const Identifier *get_full_name() const { return &full_name_; }
//
size_t get_arguments_size() const { return arguments_.size(); }
Argument *get_argument(size_t id) { return &arguments_.at(id); }
@ -316,8 +321,9 @@ private:
SymbolDocs docs_;
std::vector<Constraint> constraints_;
Modifier return_modifier_;
MethodModifier method_modifier_;
bool is_method_;
Identifier name_;
Identifier full_name_;
std::vector<Argument> arguments_;
bool are_annotations_same_to_names_; // needed for easier prinitng process
std::optional<ExpressionProxy> expression_;
@ -326,16 +332,12 @@ private:
class TypeDefinition : public Node {
public:
TypeDefinition(Node node, SymbolDocs &&docs, bool is_on_heap,
const Identifier &name, std::vector<Identifier> &&arguments,
std::optional<VariantType> &&type,
std::vector<FunctionDefinition> &&methods)
const Identifier &name, std::vector<Identifier> &&typeclasses,
std::vector<Identifier> &&arguments,
std::optional<VariantType> &&type)
: Node(node), docs_(std::move(docs)), is_on_heap_(is_on_heap),
name_(name), arguments_(std::move(arguments)), type_(std::move(type)),
methods_(std::move(methods)) {
for (size_t i = 0; i < methods.size(); ++i) {
methods_by_name_[*methods_[i].get_name()->get()] = i;
}
}
name_(name), typeclasses_(typeclasses),
arguments_(std::move(arguments)), type_(std::move(type)) {}
//
@ -379,13 +381,7 @@ public:
//
size_t get_methods_size() const { return methods_.size(); }
FunctionDefinition *get_method(size_t id) { return &methods_.at(id); }
const FunctionDefinition *get_method(size_t id) const {
return &methods_.at(id);
}
bool is_typeclass() { return name_.get_type() == Identifier::TYPECLASS; }
//
@ -397,73 +393,9 @@ private:
SymbolDocs docs_;
bool is_on_heap_;
Identifier name_;
std::vector<Identifier> typeclasses_;
std::vector<Identifier> arguments_;
std::optional<VariantType> type_; // TupleType is VariantType with one variant
std::unordered_map<std::string, size_t>
methods_by_name_; // methods can't be overloaded ??
std::vector<FunctionDefinition> methods_;
};
class TypeclassDefinition : public Node {
public:
TypeclassDefinition(Node node, SymbolDocs &&docs, const Identifier &name,
std::vector<Identifier> &&base_typeclasses,
std::vector<FunctionDefinition> &&methods)
: Node(node), docs_(std::move(docs)), name_(name),
base_typeclasses_(std::move(base_typeclasses)),
methods_(std::move(methods)) {
for (size_t i = 0; i < methods.size(); ++i) {
methods_by_name_[*methods_[i].get_name()->get()] = i;
}
}
//
SymbolDocs *get_docs() { return &docs_; }
const SymbolDocs *get_docs() const { return &docs_; }
//
Identifier *get_name() { return &name_; }
const Identifier *get_name() const { return &name_; }
//
size_t get_base_typeclasses_size() const { return base_typeclasses_.size(); }
Identifier *get_base_typeclass(size_t id) {
return &base_typeclasses_.at(id);
}
const Identifier *get_base_typeclass(size_t id) const {
return &base_typeclasses_.at(id);
}
//
size_t get_methods_size() const { return methods_.size(); }
FunctionDefinition *get_method(size_t id) { return &methods_.at(id); }
const FunctionDefinition *get_method(size_t id) const {
return &methods_.at(id);
}
//
bool is_same_to(const TypeclassDefinition &other_typeclass_definition) const;
CombineResult combine(TypeclassDefinition &&other_typeclass_definition);
private:
SymbolDocs docs_;
Identifier name_;
std::vector<Identifier> base_typeclasses_;
std::unordered_map<std::string, size_t>
methods_by_name_; // methods can't be overloaded ??
std::vector<FunctionDefinition> methods_;
};
class Statement {
@ -498,8 +430,7 @@ public:
CombineResult combine(Statement &&other_statement);
private:
std::variant<Import, TypeDefinition, FunctionDefinition, TypeclassDefinition,
EmptyLines>
std::variant<Import, TypeDefinition, FunctionDefinition, Extra, EmptyLines>
expression_;
};

View file

@ -1,7 +1,6 @@
#pragma once
#include "basic_printers.hpp"
#include "name_tree.hpp"
#include "statement_nodes.hpp"
#include <vector>
@ -23,7 +22,4 @@ void print_type_definition(const nodes::TypeDefinition &statement,
void print_function_definition(const nodes::FunctionDefinition &statement,
Printer &printer);
void print_typeclass_definition(const nodes::TypeclassDefinition &statement,
Printer &printer);
} // namespace printers

View file

@ -14,9 +14,8 @@ enum class Type {
// --- definitions
TYPE_DEFINITION,
FUNCTION_DEFINITION,
TYPECLASS_DEFINITION,
TYPE_DEFINITION, // datatype or typeclass
// --- flow control
@ -62,6 +61,8 @@ enum class Type {
DEFINITION_INFO,
ANNOTATION_INFO,
EXTRA,
EMPTY_LINES,
// --- tokens
@ -100,9 +101,8 @@ const static std::string CONSTRAINT = "constraint";
// --- definitions
const static std::string TYPE_DEFINITION = "type_definition";
const static std::string FUNCTION_DEFINITION = "function_definition";
const static std::string TYPECLASS_DEFINITION = "typeclass_definition";
const static std::string TYPE_DEFINITION = "type_definition";
// --- flow control
@ -148,6 +148,8 @@ const static std::string TYPE = "type";
const static std::string DEFINITION_INFO = "definition_info";
const static std::string ANNOTATION_INFO = "annotation_info";
const static std::string EXTRA = "extra";
const static std::string EMPTY_LINES = "empty_lines";
// --- tokens
@ -180,12 +182,10 @@ inline Type string_to_type(const std::string &str) {
return Type::IMPORT;
} else if (str == CONSTRAINT) {
return Type::CONSTRAINT;
} else if (str == TYPE_DEFINITION) {
return Type::TYPE_DEFINITION;
} else if (str == FUNCTION_DEFINITION) {
return Type::FUNCTION_DEFINITION;
} else if (str == TYPECLASS_DEFINITION) {
return Type::TYPECLASS_DEFINITION;
} else if (str == TYPE_DEFINITION) {
return Type::TYPE_DEFINITION;
} else if (str == CASE) {
return Type::CASE;
} else if (str == MATCH) {
@ -232,6 +232,8 @@ inline Type string_to_type(const std::string &str) {
return Type::DEFINITION_INFO;
} else if (str == ANNOTATION_INFO) {
return Type::ANNOTATION_INFO;
} else if (str == EXTRA) {
return Type::EXTRA;
} else if (str == EMPTY_LINES) {
return Type::EMPTY_LINES;
} else if (str == PLACEHOLDER) {