ERROR type, check of passed type, many expected types in Arguments

This commit is contained in:
ProgramSnail 2024-02-22 18:46:01 +03:00
parent a5fc0c7ee7
commit 005fb6aaf3
7 changed files with 200 additions and 112 deletions

View file

@ -51,13 +51,9 @@ nodes::TypeProxy get_literal_type(const nodes::Literal &literal,
nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
SourcesManager &sources_manager,
nodes::MaybeTypeProxy expected_type) {
const Arguments &arguments) {
auto const type = get_literal_type(literal, sources_manager);
if (expected_type.has_value() && type != expected_type.value()) {
return nodes::TypeCheckResult::construct_invalid_result();
// nodes::TypeCheckResult::fail_with(type, expected_type, literal); // TODO: create error ??
}
return nodes::TypeCheckResult(type);
return type_same_to_expected(type, arguments, literal, sources_manager);
}
} // namespace type_check

View file

@ -68,7 +68,7 @@ type_check_expression(const nodes::Expression &expression,
case 12: // Literal
// TODO
return type_check_literal(*expression.get<nodes::Literal>().value(),
sources_manager, arguments.get_expected());
sources_manager, arguments);
// --- empty lines
case 13: // Extra
return nodes::TypeCheckResult(
@ -153,10 +153,10 @@ nodes::TypeCheckResult type_check_match(const nodes::Match &expression,
builtin::types::Type::UNIT)};
}
return type_same_to_expected(sources_manager.get_type_storage()->add_array_of(
expression_result.value().get()),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(
sources_manager.get_type_storage()->add_array_of(
expression_result.value().get()),
arguments, expression, sources_manager);
}
nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
@ -196,10 +196,10 @@ nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
expression_result = nodes::TypeCheckResult::construct_invalid_result();
}
return type_same_to_expected(sources_manager.get_type_storage()->add_array_of(
expression_result.value().get()),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(
sources_manager.get_type_storage()->add_array_of(
expression_result.value().get()),
arguments, expression, sources_manager);
}
nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
@ -261,9 +261,9 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
// TODO: modifier checks ??, modifiers ??
return type_same_to_expected(
return type_check_from_arguments(
sources_manager.get_type_storage()->add_array_of(expression_result.get()),
arguments.get_expected(), expression, *sources_manager.get_error_log());
arguments, expression, sources_manager);
} // IN PROGRESS
// --- containers
@ -296,10 +296,10 @@ nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
return nodes::TypeCheckResult::construct_invalid_result();
}
return type_same_to_expected(sources_manager.get_type_storage()->add_array_of(
last_expression_result.value().get()),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(
sources_manager.get_type_storage()->add_array_of(
last_expression_result.value().get()),
arguments, expression, sources_manager);
}
nodes::TypeCheckResult type_check_block(const nodes::Container &expression,
@ -327,10 +327,10 @@ nodes::TypeCheckResult type_check_block(const nodes::Container &expression,
sources_manager.get_type_storage()->primitive_type(
builtin::types::Type::UNIT));
return type_same_to_expected(sources_manager.get_type_storage()->add_array_of(
block_brought_type.get()),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(
sources_manager.get_type_storage()->add_array_of(
block_brought_type.get()),
arguments, expression, sources_manager);
}
nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
@ -375,10 +375,10 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
break;
}
return type_same_to_expected(
return type_check_from_arguments(
sources_manager.get_type_storage()->primitive_type(
builtin::types::Type::UNIT),
arguments.get_expected(), expression, *sources_manager.get_error_log());
arguments, expression, sources_manager);
}
nodes::TypeCheckResult
@ -410,10 +410,10 @@ type_check_name_definition(const nodes::NameDefinition &expression,
}
// Return BOOL as any := / =: expression
return type_same_to_expected(
return type_check_from_arguments(
sources_manager.get_type_storage()->primitive_type(
builtin::types::Type::BOOL),
arguments.get_expected(), expression, *sources_manager.get_error_log());
arguments, expression, sources_manager);
}
nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
@ -439,9 +439,9 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
// TODO: modifier checks ??
return type_same_to_expected(value_result.get().get()->get_parameter_proxy(0),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(
value_result.get().get()->get_parameter_proxy(0), arguments, expression,
sources_manager);
}
nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
@ -464,9 +464,9 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
// TODO: modifier checks ??
return type_same_to_expected(
value_result.get().get()->get_parameter_proxy(index),
arguments.get_expected(), expression, *sources_manager.get_error_log());
return type_check_from_arguments(
value_result.get().get()->get_parameter_proxy(index), arguments,
expression, sources_manager);
}
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
@ -487,10 +487,10 @@ nodes::TypeCheckResult
type_check_loop_control(const nodes::LoopControl &expression,
SourcesManager &sources_manager, State &,
const Arguments &arguments) {
return type_same_to_expected(
return type_check_from_arguments(
sources_manager.get_type_storage()->primitive_type(
builtin::types::Type::UNIT),
arguments.get_expected(), expression, *sources_manager.get_error_log());
arguments, expression, sources_manager);
}
nodes::TypeCheckResult
@ -528,8 +528,8 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
modified_result.get(), expression.get_modifier()));
}
return type_same_to_expected(modified_result.get(), arguments.get_expected(),
expression, *sources_manager.get_error_log());
return type_check_from_arguments(modified_result.get(), arguments, expression,
sources_manager);
} // IN PROGRESS
// --- other
@ -628,9 +628,8 @@ type_check_name_expression(const nodes::NameExpression &expression,
// TODO: invert modifier ??
// TODO: generic types should be deduced from arguments
return type_same_to_expected(returned->get_type_proxy().value(),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(returned->get_type_proxy().value(),
arguments, expression, sources_manager);
}
// checks for universal call syntax ??
@ -680,41 +679,67 @@ type_check_constructor(const nodes::Constructor &expression,
// TODO: work with generics (type_definition->arguments, ...)
// for tuple
for (size_t i = 0; i < expression.arguments_size();
++i) { // TODO: deduce order by annotations
const auto annotation = expression.get_argument_annotation(i);
const auto expected_annotation =
type.get()->get_parameter(i)->get_annotation();
const auto builtin_type = type.get()->to_builtin();
if (annotation.has_value() != expected_annotation.has_value()) {
switch (builtin_type) {
case builtin::types::Type::TUPLE:
// for tuple
for (size_t i = 0; i < expression.arguments_size();
++i) { // TODO: deduce order by annotations
const auto annotation = expression.get_argument_annotation(i);
const auto expected_annotation =
type.get()->get_parameter(i)->get_annotation();
type_check_error(
"Wrong type constructor argument annotation: should be " +
std::string{expected_annotation.has_value()
? *expected_annotation.value()
: "[none]"},
*expression.get_argument_value(i), sources_manager);
if (annotation.has_value() != expected_annotation.has_value()) {
type_check_error(
"Wrong type constructor argument annotation: should be " +
std::string{expected_annotation.has_value()
? *expected_annotation.value()
: "[none]"},
*expression.get_argument_value(i), sources_manager);
}
if (annotation.has_value() &&
*annotation.value() != *expected_annotation.value()) {
type_check_error(
"Wrong function argument type annotation: " + *annotation.value() +
" instead of " + *expected_annotation.value(),
*expression.get_argument_value(i), sources_manager);
}
type_check_expression(
*expression.get_argument_value(i), sources_manager, state,
Arguments{}.expect(type.get()->get_parameter_proxy(i)));
// TODO: do something with argument type ??
}
if (annotation.has_value() &&
*annotation.value() != *expected_annotation.value()) {
type_check_error(
"Wrong function argument type annotation: " + *annotation.value() +
" instead of " + *expected_annotation.value(),
*expression.get_argument_value(i), sources_manager);
}
type_check_expression(
*expression.get_argument_value(i), sources_manager, state,
Arguments{}.expect(type.get()->get_parameter_proxy(i)));
// TODO: do something with argument type ??
break;
case builtin::types::Type::VARIANT:
// TODO: expect one of types
break;
case builtin::types::Type::OPTIONAL:
// TODO: expect type (generic) or NULL
break;
case builtin::types::Type::RESULT:
// TODO: expect ERROR or type (generic)
break;
case builtin::types::Type::ERROR:
// TODO: expect type (generic)
break;
case builtin::types::Type::FUNCTION:
// TODO: built from one value (function)
break;
case builtin::types::Type::NONE:
// TODO: built from one value
break;
default: // array, basic types
type_check_error("Type can't be constructed", expression, sources_manager);
break;
}
// TODO: deduce generic parts in type
return type_same_to_expected(expression.get_type_proxy(),
arguments.get_expected(), expression,
*sources_manager.get_error_log());
return type_check_from_arguments(expression.get_type_proxy(), arguments,
expression, sources_manager);
// TODO: add <- modifiier to type ??
} // IN PROGRESS
@ -724,12 +749,19 @@ nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
SourcesManager &sources_manager,
State &state,
const Arguments &arguments) {
if (!arguments.get_expected().has_value()) {
type_check_error("Can't deduce type of lambda function from context",
if (arguments.get_expected().empty()) {
type_check_error("Can't deduce type of lambda function from context: no "
"one type expected",
expression, sources_manager);
}
const auto expected_type = arguments.get_expected().value();
if (arguments.get_expected().size() != 1) {
type_check_error("Can't deduce type of lambda function from context; too "
"much possible types",
expression, sources_manager);
}
const auto expected_type = arguments.get_expected().front();
if (!expected_type.get()->is_builtin(builtin::types::Type::FUNCTION)) {
type_check_error("Type of lambda function should be function", expression,
sources_manager);
@ -742,7 +774,10 @@ nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
// auto returned_type = type_check_expression(
// *expression.get_expression(), sources_manager, state, Arguments{});
return nodes::TypeCheckResult{expected_type}; // TODO: same to expected ??
// TODO: needed ?? (only passed type check required ??)
return type_check_from_arguments(
expected_type, arguments, expression,
sources_manager); // TODO: same to expected ??
} // IN PROGRESS
} // namespace type_check

View file

@ -1,24 +1,54 @@
#include "type_check_utils.hpp"
#include <algorithm>
namespace type_check {
nodes::TypeCheckResult type_same_to_expected(
nodes::TypeProxy type, nodes::MaybeTypeProxy expected_type,
const nodes::Node &node, error_handling::ErrorLog &error_log,
const std::string &message) {
if (!expected_type.has_value()) {
// pass type -> compare types, return bool
// no pass type -> return type
nodes::TypeProxy check_same_to_pass_type_in_arguments(
nodes::TypeProxy type, const Arguments &arguments, const nodes::Node &node,
SourcesManager &sources_manager, const std::string &message) {
if (!arguments.get_passed().has_value()) {
return type;
}
if (type != arguments.get_passed().value()) {
type_check_error(message, node, sources_manager);
}
return sources_manager.get_type_storage()->primitive_type(
builtin::types::Type::BOOL);
}
bool check_no_pass_type_in_arguments(const Arguments &arguments,
const nodes::Node &node,
SourcesManager &sources_manager,
const std::string &message) {
if (arguments.get_passed().has_value()) {
type_check_error(message, node, sources_manager);
return false;
}
return true;
}
nodes::TypeCheckResult
type_same_to_expected(nodes::TypeProxy type,
const Arguments& arguments,
const nodes::Node &node, SourcesManager &sources_manager,
const std::string &message) {
const auto& expected = arguments.get_expected();
if (expected.empty()) {
return nodes::TypeCheckResult{type};
}
if (type != expected_type.value()) {
error_log.add_error(error_handling::ErrorLog::ErrorMessage(
node, message, error_handling::ErrorType::TYPE_CHECK));
return nodes::TypeCheckResult::construct_invalid_result();
}
// TODO: use 'can cast to' (for modifiers), instead '=='
return nodes::TypeCheckResult{
expected_type.value()}; // TODO: retern type or expected type ??
if (std::all_of(expected.begin(), expected.end(), [type](nodes::TypeProxy expected_type) { return type != expected_type; })) {
type_check_error(message, node, sources_manager);
}
return nodes::TypeCheckResult{expected.front()}; // any can be choosen
}
template <typename T>
@ -72,15 +102,4 @@ void type_check_error(const std::string &message, const nodes::Node &node,
node, message, error_handling::ErrorType::TYPE_CHECK));
}
bool check_no_pass_type_in_arguments(const Arguments &arguments,
const nodes::Node &node,
SourcesManager &sources_manager) {
if (arguments.get_passed().has_value()) {
type_check_error("Type can't be passed to this node", node,
sources_manager);
return false;
}
return true;
}
} // namespace type_check