mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-05 22:48:43 +00:00
part of expected type type check process modification: fixes, check function
This commit is contained in:
parent
ffa9c47107
commit
3907da619e
2 changed files with 73 additions and 39 deletions
|
|
@ -15,74 +15,74 @@ namespace type_check {
|
|||
nodes::TypeCheckResult
|
||||
type_check_expression(const nodes::Expression &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
// --- flow control
|
||||
|
||||
nodes::TypeCheckResult type_check_match(const nodes::Match &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
// --- containers
|
||||
|
||||
nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
// --- modifiers
|
||||
|
||||
nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_definition(const nodes::NameDefinition &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_loop_control(const nodes::LoopControl &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
// --- other
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_expression(const nodes::NameExpression &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_constructor(const nodes::Constructor &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state,
|
||||
const Arguments &arguments = {});
|
||||
const Arguments &arguments);
|
||||
|
||||
} // namespace type_check
|
||||
|
|
|
|||
|
|
@ -12,6 +12,25 @@
|
|||
|
||||
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 = "Different type with expected one") {
|
||||
if (!expected_type.has_value()) {
|
||||
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 ??
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult
|
||||
type_check_expression(const nodes::Expression &expression,
|
||||
SourcesManager &sources_manager, State &state,
|
||||
|
|
@ -38,7 +57,7 @@ type_check_expression(const nodes::Expression &expression,
|
|||
case 5: // NameDefinition
|
||||
return type_check_name_definition(
|
||||
*expression.get<nodes::NameDefinition>().value(), sources_manager,
|
||||
state);
|
||||
state, arguments);
|
||||
case 6: // Access
|
||||
return type_check_access(*expression.get<nodes::Access>().value(),
|
||||
sources_manager, state, arguments);
|
||||
|
|
@ -111,10 +130,13 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
|||
|
||||
switch (expression.get_type()) {
|
||||
case nodes::Loop::LOOP:
|
||||
// TODO
|
||||
break;
|
||||
case nodes::Loop::WHILE:
|
||||
condition_result = type_check_expression(
|
||||
*expression.get_condition().value(), sources_manager, state);
|
||||
*expression.get_condition().value(), sources_manager, state,
|
||||
Arguments{{sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::BOOL)}});
|
||||
|
||||
if (condition_result.value().is_invalid()) {
|
||||
return condition_result.value();
|
||||
|
|
@ -175,9 +197,9 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
|||
|
||||
// TODO: modifier checks ??, modifiers ??
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->add_array_of(
|
||||
expression_result.get()));
|
||||
return type_same_to_expected(
|
||||
sources_manager.get_type_storage()->add_array_of(expression_result.get()),
|
||||
arguments.expected_type, expression, *sources_manager.get_error_log());
|
||||
} // IN PROGRESS
|
||||
|
||||
// --- containers
|
||||
|
|
@ -214,9 +236,10 @@ nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
|
|||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->add_array_of(
|
||||
last_expression_result.value().get()));
|
||||
return type_same_to_expected(sources_manager.get_type_storage()->add_array_of(
|
||||
last_expression_result.value().get()),
|
||||
arguments.expected_type, expression,
|
||||
*sources_manager.get_error_log());
|
||||
} // IN PROGRESS
|
||||
|
||||
// TODO
|
||||
|
|
@ -234,9 +257,9 @@ nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
|
|||
case nodes::Container::ARRAY:
|
||||
return type_check_array(expression, sources_manager, state, arguments);
|
||||
case nodes::Container::BLOCK:
|
||||
return type_check_block(expression, sources_manager,
|
||||
state, arguments); // TODO: check that expression brought
|
||||
// type are same, -> brought_type
|
||||
return type_check_block(expression, sources_manager, state,
|
||||
arguments); // TODO: check that expression brought
|
||||
// type are same, -> brought_type
|
||||
}
|
||||
} // IN PROGRESS
|
||||
|
||||
|
|
@ -274,9 +297,10 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
|
|||
break;
|
||||
}
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
return type_same_to_expected(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNIT));
|
||||
builtin::types::Type::UNIT),
|
||||
arguments.expected_type, expression, *sources_manager.get_error_log());
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
|
@ -322,8 +346,9 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
|
|||
|
||||
// TODO: modifier checks ??
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
value_result.get().get()->get_parameter_proxy(0));
|
||||
return type_same_to_expected(value_result.get().get()->get_parameter_proxy(0),
|
||||
arguments.expected_type, expression,
|
||||
*sources_manager.get_error_log());
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
|
||||
|
|
@ -353,8 +378,9 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
|
|||
|
||||
// TODO: modifier checks ??
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
value_result.get().get()->get_parameter_proxy(index));
|
||||
return type_same_to_expected(
|
||||
value_result.get().get()->get_parameter_proxy(index),
|
||||
arguments.expected_type, expression, *sources_manager.get_error_log());
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
||||
|
|
@ -363,19 +389,22 @@ nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
|||
const Arguments &arguments) {
|
||||
switch (expression.get_type()) {
|
||||
case nodes::Access::ARRAY:
|
||||
return type_check_array_access(expression, sources_manager, state);
|
||||
return type_check_array_access(expression, sources_manager, state,
|
||||
arguments);
|
||||
case nodes::Access::TUPLE:
|
||||
return type_check_tuple_access(expression, sources_manager, state);
|
||||
return type_check_tuple_access(expression, sources_manager, state,
|
||||
arguments);
|
||||
}
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult type_check_loop_control(const nodes::LoopControl &,
|
||||
SourcesManager &sources_manager,
|
||||
State &,
|
||||
const Arguments &arguments) {
|
||||
return nodes::TypeCheckResult(
|
||||
nodes::TypeCheckResult
|
||||
type_check_loop_control(const nodes::LoopControl &expression,
|
||||
SourcesManager &sources_manager, State &,
|
||||
const Arguments &arguments) {
|
||||
return type_same_to_expected(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNIT));
|
||||
builtin::types::Type::UNIT),
|
||||
arguments.expected_type, expression, *sources_manager.get_error_log());
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult
|
||||
|
|
@ -385,6 +414,10 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
|||
auto modified_result = type_check_expression(*expression.get_expression(),
|
||||
sources_manager, state);
|
||||
|
||||
if (modified_result.is_invalid()) {
|
||||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
|
||||
if (nodes::utils::is_suffix_modifier(
|
||||
expression.get_modifier())) { // optional, result
|
||||
// '?' - open optional / result in -> (execute or not execute pattern
|
||||
|
|
@ -411,7 +444,8 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
|||
modified_result.get(), expression.get_modifier()));
|
||||
}
|
||||
|
||||
return modified_result;
|
||||
return type_same_to_expected(modified_result.get(), arguments.expected_type,
|
||||
expression, *sources_manager.get_error_log());
|
||||
} // IN PROGRESS
|
||||
|
||||
// --- other
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue