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

@ -25,8 +25,8 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) {
if (return_modifier_ != other_function_definition.return_modifier_) {
return CombineResult::DIFFERNENT_MODIFIER_ERROR;
}
if (method_modifier_ != other_function_definition.method_modifier_) {
return CombineResult::DIFFERNENT_MODIFIER_ERROR;
if (is_method_ != other_function_definition.is_method_) {
return CombineResult::DIFFERNENT_MODIFIER_ERROR; // other error type ??
}
if (are_annotations_same_to_names_ !=
@ -228,98 +228,6 @@ CombineResult TypeDefinition::combine(TypeDefinition &&other_type_definition) {
type_ = std::move(other_type_definition.type_);
}
// TODO: can be in incorrect state if error occure here
// combine methods ??
methods_.reserve(methods_.size() + other_type_definition.methods_.size());
for (size_t i = 0; i < other_type_definition.methods_.size(); ++i) {
auto method_iter = methods_by_name_.find(
*other_type_definition.methods_[i].get_name()->get());
if (method_iter == methods_by_name_.end()) {
methods_by_name_[*other_type_definition.methods_[i].get_name()->get()] =
methods_.size();
methods_.push_back(std::move(other_type_definition.methods_[i]));
} else {
auto method_combine_result = methods_[method_iter->second].combine(
std::move(other_type_definition.methods_[i]));
if (method_combine_result != CombineResult::OK) {
return method_combine_result;
}
}
}
return CombineResult::OK;
}
bool TypeclassDefinition::is_same_to(
const TypeclassDefinition &other_typeclass_definition) const {
if (*name_.get() != *other_typeclass_definition.name_.get()) {
return false;
}
return true;
}
CombineResult
TypeclassDefinition::combine(TypeclassDefinition &&other_typeclass_definition) {
// should be same in all definitions of one typeclass
if (*name_.get() != *other_typeclass_definition.name_.get()) {
return CombineResult::DIFFERENT_NAME_ERROR;
}
// only one definition should have documentation
if (docs_.get_description().has_value() &&
other_typeclass_definition.docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
}
if (docs_.get_annotations_info_size() > 0 &&
other_typeclass_definition.docs_.get_annotations_info_size() > 0) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
}
// only one definition should define base typeclasses
if (!base_typeclasses_.empty() &&
!other_typeclass_definition.base_typeclasses_.empty()) {
return CombineResult::MORE_THEN_ONE_DEFINITION_BODY_ERROR;
}
// combine docs
// all docs should be in one definition
if (other_typeclass_definition.docs_.get_description().has_value() ||
other_typeclass_definition.docs_.get_annotations_info_size() > 0) {
if (docs_.get_annotations_info_size() > 0 ||
docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
}
docs_ = std::move(other_typeclass_definition.docs_);
}
if (!other_typeclass_definition.base_typeclasses_.empty()) {
base_typeclasses_ = std::move(other_typeclass_definition.base_typeclasses_);
}
// TODO: can be in incorrect state if error occure here
// combine methods ??
methods_.reserve(methods_.size() +
other_typeclass_definition.methods_.size());
for (size_t i = 0; i < other_typeclass_definition.methods_.size(); ++i) {
auto method_iter = methods_by_name_.find(
*other_typeclass_definition.methods_[i].get_name()->get());
if (method_iter == methods_by_name_.end()) {
methods_by_name_
[*other_typeclass_definition.methods_[i].get_name()->get()] =
methods_.size();
methods_.push_back(std::move(other_typeclass_definition.methods_[i]));
} else {
auto method_combine_result = methods_[method_iter->second].combine(
std::move(other_typeclass_definition.methods_[i]));
if (method_combine_result != CombineResult::OK) {
return method_combine_result;
}
}
}
return CombineResult::OK;
}
@ -339,11 +247,7 @@ bool Statement::is_same_to(const Statement &other_statement) const {
return std::get<FunctionDefinition>(expression_)
.is_same_to(std::move(
std::get<FunctionDefinition>(other_statement.expression_)));
case 3: // TypeclassDefinition
return std::get<TypeclassDefinition>(expression_)
.is_same_to(std::move(
std::get<TypeclassDefinition>(other_statement.expression_)));
case 4: // EmptyLines
case 3: // EmptyLines
return false;
default:
break;
@ -369,11 +273,7 @@ CombineResult Statement::combine(Statement &&other_statement) {
return std::get<FunctionDefinition>(expression_)
.combine(std::move(
std::get<FunctionDefinition>(other_statement.expression_)));
case 3: // TypeclassDefinition
return std::get<TypeclassDefinition>(expression_)
.combine(std::move(
std::get<TypeclassDefinition>(other_statement.expression_)));
case 4: // EmptyLines
case 3: // EmptyLines
return CombineResult::STATEMENTS_CANT_BE_COMBINED_ERROR;
default:
break;