fixes, name refactoring

This commit is contained in:
ProgramSnail 2024-01-04 19:29:29 +03:00
parent 4f04dd9995
commit fe652bc1c2
11 changed files with 62 additions and 27 deletions

View file

@ -47,7 +47,7 @@ set(
) )
set( set(
PRINTERS TYPECHECKERS
src/basic_type_check.cpp src/basic_type_check.cpp
src/expression_type_check.cpp src/expression_type_check.cpp
src/type_check_utils.cpp src/type_check_utils.cpp
@ -59,7 +59,7 @@ add_executable(lang src/main.cpp
${NODES} ${NODES}
${BUILDERS} ${BUILDERS}
${PRINTERS} ${PRINTERS}
# ${TYPECHECK} # ${TYPECHECKERS}
include/tree_sitter_wrapper.hpp include/tree_sitter_wrapper.hpp
deps/tree-sitter-lang/src/tree_sitter/parser.h deps/tree-sitter-lang/src/tree_sitter/parser.h

View file

@ -73,7 +73,7 @@ public:
// //
size_t get_annotations_info_size() const { return annotations_info_.size(); } size_t annotations_info_size() const { return annotations_info_.size(); }
std::string *get_annotation(size_t id) { std::string *get_annotation(size_t id) {
return &annotations_info_[id].first; return &annotations_info_[id].first;

View file

@ -261,7 +261,7 @@ public:
// //
size_t get_constraints_size() const { return constraints_.size(); } size_t constraints_size() const { return constraints_.size(); }
Constraint *get_constraint(size_t id) { return &constraints_.at(id); } Constraint *get_constraint(size_t id) { return &constraints_.at(id); }
@ -289,7 +289,7 @@ public:
// //
size_t get_arguments_size() const { return arguments_.size(); } size_t arguments_size() const { return arguments_.size(); }
Argument *get_argument(size_t id) { return &arguments_.at(id); } Argument *get_argument(size_t id) { return &arguments_.at(id); }
@ -363,7 +363,7 @@ public:
// //
size_t get_arguments_size() const { return arguments_.size(); } size_t arguments_size() const { return arguments_.size(); }
Identifier *get_argument(size_t id) { return &arguments_.at(id); } Identifier *get_argument(size_t id) { return &arguments_.at(id); }

View file

@ -70,7 +70,7 @@ public:
// //
size_t get_parametrs_size() const { return parameters_.size(); } size_t parameters_size() const { return parameters_.size(); }
Type *get_parameter(size_t id) { return parameters_.at(id).get(); } Type *get_parameter(size_t id) { return parameters_.at(id).get(); }

View file

@ -82,7 +82,7 @@ public:
// //
size_t get_statements_size() const { return statements_.size(); } size_t statements_size() const { return statements_.size(); }
nodes::Statement *get_statement(size_t id) { return &statements_.at(id); } nodes::Statement *get_statement(size_t id) { return &statements_.at(id); }

View file

@ -53,7 +53,7 @@ nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
SourcesManager &sources_manager, SourcesManager &sources_manager,
nodes::MaybeTypeProxy expected_type) { nodes::MaybeTypeProxy expected_type) {
auto const type = get_literal_type(literal, sources_manager); auto const type = get_literal_type(literal, sources_manager);
if (expected_type.has_value() && type != expected_type.value() { if (expected_type.has_value() && type != expected_type.value()) {
return nodes::TypeCheckResult::construct_invalid_result(); return nodes::TypeCheckResult::construct_invalid_result();
// nodes::TypeCheckResult::fail_with(type, expected_type, literal); // TODO: create error ?? // nodes::TypeCheckResult::fail_with(type, expected_type, literal); // TODO: create error ??
} }

View file

@ -443,9 +443,44 @@ type_check_constructor(const nodes::Constructor &expression,
} }
const nodes::TypeDefinition *type_definition = maybe_type_definition.value(); const nodes::TypeDefinition *type_definition = maybe_type_definition.value();
// TODO: extract types from definition if (!type_definition->get_type().has_value()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression,
"Type defenition for constructor type not found (declaration only)",
error_handling::ErrorType::TYPE_CHECK));
return nodes::TypeCheckResult::construct_invalid_result();
}
// TODO: chack that is not typeclass ??
// TODO: type arguments substitution (deduce argument type values ??)
for (size_t i = 0; i < type_definition->arguments_size(); ++i) {
// TODO: ...
}
nodes::TypeProxy type = type_definition->get_type().value();
// TODO: work with different parametric types: tuple, variant, ...
if (type.get()->parameters_size() != expression.arguments_size()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression,
"Constructor arguments count is " +
std::string{expression.arguments_size() <
type.get()->parameters_size()
? "less"
: "more"} +
" then type fields count",
error_handling::ErrorType::TYPE_CHECK));
return nodes::TypeCheckResult::construct_invalid_result();
}
// for tuple
for (size_t i = 0; i < expression.arguments_size(); ++i) { for (size_t i = 0; i < expression.arguments_size(); ++i) {
const auto annotation = expression.get_argument_annotation(i); const auto annotation = expression.get_argument_annotation(i);
auto argument_type = type_check_expression( auto argument_type = type_check_expression(
*expression.get_argument_value(i), sources_manager, state, *expression.get_argument_value(i), sources_manager, state,
/* TODO: type from definition by annotation */); /* TODO: type from definition by annotation */);

View file

@ -56,8 +56,8 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) {
other_function_definition.docs_.get_description().has_value()) { other_function_definition.docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }
if (docs_.get_annotations_info_size() > 0 && if (docs_.annotations_info_size() > 0 &&
other_function_definition.docs_.get_annotations_info_size() > 0) { other_function_definition.docs_.annotations_info_size() > 0) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }
@ -137,8 +137,8 @@ FunctionDefinition::combine(FunctionDefinition &&other_function_definition) {
// combine docs // combine docs
// all docs should be in one definition // all docs should be in one definition
if (other_function_definition.docs_.get_description().has_value() || if (other_function_definition.docs_.get_description().has_value() ||
other_function_definition.docs_.get_annotations_info_size() > 0) { other_function_definition.docs_.annotations_info_size() > 0) {
if (docs_.get_annotations_info_size() > 0 || if (docs_.annotations_info_size() > 0 ||
docs_.get_description().has_value()) { docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }
@ -221,8 +221,8 @@ CombineResult TypeDefinition::combine(TypeDefinition &&other_type_definition) {
other_type_definition.docs_.get_description().has_value()) { other_type_definition.docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }
if (docs_.get_annotations_info_size() > 0 && if (docs_.annotations_info_size() > 0 &&
other_type_definition.docs_.get_annotations_info_size() > 0) { other_type_definition.docs_.annotations_info_size() > 0) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }
@ -234,8 +234,8 @@ CombineResult TypeDefinition::combine(TypeDefinition &&other_type_definition) {
// combine docs // combine docs
// all docs should be in one definition // all docs should be in one definition
if (other_type_definition.docs_.get_description().has_value() || if (other_type_definition.docs_.get_description().has_value() ||
other_type_definition.docs_.get_annotations_info_size() > 0) { other_type_definition.docs_.annotations_info_size() > 0) {
if (docs_.get_annotations_info_size() > 0 || if (docs_.annotations_info_size() > 0 ||
docs_.get_description().has_value()) { docs_.get_description().has_value()) {
return CombineResult::MORE_THEN_ONE_DOCS_ERROR; return CombineResult::MORE_THEN_ONE_DOCS_ERROR;
} }

View file

@ -22,7 +22,7 @@ void print_docs(const nodes::SymbolDocs &docs, Printer &printer) {
printer.new_indent_line(); printer.new_indent_line();
} }
for (size_t i = 0; i < docs.get_annotations_info_size(); ++i) { for (size_t i = 0; i < docs.annotations_info_size(); ++i) {
print_annotation(*docs.get_annotation(i), printer); print_annotation(*docs.get_annotation(i), printer);
printer.space(); printer.space();
printer.print(*docs.get_annotation_info(i)); printer.print(*docs.get_annotation_info(i));

View file

@ -92,7 +92,7 @@ void print_type_definition(const nodes::TypeDefinition &statement,
print_identifier(*statement.get_name(), printer); print_identifier(*statement.get_name(), printer);
for (size_t i = 0; i < statement.get_arguments_size(); ++i) { for (size_t i = 0; i < statement.arguments_size(); ++i) {
printer.space(); printer.space();
print_identifier(*statement.get_argument(i), printer); print_identifier(*statement.get_argument(i), printer);
} }
@ -116,7 +116,7 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
Printer &printer) { Printer &printer) {
print_docs(*statement.get_docs(), printer); print_docs(*statement.get_docs(), printer);
for (size_t i = 0; i < statement.get_constraints_size(); ++i) { for (size_t i = 0; i < statement.constraints_size(); ++i) {
print_constraint(*statement.get_constraint(i), printer); print_constraint(*statement.get_constraint(i), printer);
printer.new_indent_line(); printer.new_indent_line();
} }
@ -140,7 +140,7 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
print_modifier(statement.get_return_modifier(), printer); // ! or ? print_modifier(statement.get_return_modifier(), printer); // ! or ?
for (size_t i = 0; i < statement.get_arguments_size(); ++i) { for (size_t i = 0; i < statement.arguments_size(); ++i) {
if (!statement.get_argument(i)->get_name().has_value()) { if (!statement.get_argument(i)->get_name().has_value()) {
break; break;
} }
@ -171,12 +171,12 @@ void print_function_definition(const nodes::FunctionDefinition &statement,
} }
// all arguments are typed or are untyped in the same time // all arguments are typed or are untyped in the same time
if (statement.get_arguments_size() > 0 && if (statement.arguments_size() > 0 &&
statement.get_argument(0)->get_type().has_value()) { statement.get_argument(0)->get_type().has_value()) {
printer.print(" :"); printer.print(" :");
for (size_t i = 0; i < statement.get_arguments_size(); ++i) { for (size_t i = 0; i < statement.arguments_size(); ++i) {
printer.space(); printer.space();
if (statement.get_argument(i)->get_annotation().has_value()) { if (statement.get_argument(i)->get_annotation().has_value()) {
print_annotation(*statement.get_argument(i)->get_annotation().value(), print_annotation(*statement.get_argument(i)->get_annotation().value(),

View file

@ -18,11 +18,11 @@ void print_type(const nodes::Type &type, printers::Printer &printer) {
print_identifier(*type.get_name(), printer); print_identifier(*type.get_name(), printer);
if (type.get_parametrs_size() > 0) { if (type.parameters_size() > 0) {
printer.print("["); printer.print("[");
for (size_t i = 0; i < type.get_parametrs_size(); ++i) { for (size_t i = 0; i < type.parameters_size(); ++i) {
print_type(*type.get_parameter(i), printer); print_type(*type.get_parameter(i), printer);
if (i + 1 < type.get_parametrs_size()) { if (i + 1 < type.parameters_size()) {
printer.space(); printer.space();
} }
} }