api improvements, part of expression type checks

This commit is contained in:
ProgramSnail 2024-01-04 14:30:57 +03:00
parent a2abb598ac
commit 512d011f72
4 changed files with 96 additions and 9 deletions

View file

@ -111,6 +111,8 @@ nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
SourcesManager &sources_manager,
State &state,
const Arguments &arguments) {
// TODO: extract functiona argument names
// typecheck in statements ??
} // IN PROGRESS
nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
@ -128,7 +130,6 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
nodes::TypeCheckResult expression_result = type_check_expression(
*expression.get_expression(), sources_manager, state, {});
// detect maximum amount of errors
switch (expression.get_type()) {
case nodes::Loop::LOOP: // infinity loop, no params
break;
@ -229,6 +230,9 @@ nodes::TypeCheckResult type_check_block(const nodes::Container &expression,
SourcesManager &sources_manager,
State &state,
const Arguments &arguments) {
ContextHolder context_holder(state, expression,
*sources_manager.get_error_log());
} // IN PROGRESS
nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
@ -289,7 +293,36 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
nodes::TypeCheckResult
type_check_name_definition(const nodes::NameDefinition &expression,
SourcesManager &sources_manager, State &state,
const Arguments &arguments) {} // IN PROGRESS
const Arguments &arguments) {
if (!arguments.expected_type.has_value()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Can't deduce type of new variable from context",
error_handling::ErrorType::TYPE_CHECK));
}
// defined name modifier should be -> (or contain -> ??)
// TODO: deal with ...|...|... modifiers
const auto &expected_type = arguments.expected_type.value();
if (!expected_type.get()->is_modifier(
nodes::Modifier::OUT)) { // TODO: utils::modifier_contains_OUT
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Type of lambda function should be function",
error_handling::ErrorType::TYPE_CHECK));
}
// TODO: remove modifier ??
if (!state.insert_variable(*expression.get_name()->get(), expected_type,
expression.get_modifier())) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Variable is already defined in this context",
error_handling::ErrorType::TYPE_CHECK));
}
return nodes::TypeCheckResult{expected_type};
} // IN PROGRESS
nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
SourcesManager &sources_manager,
@ -421,13 +454,50 @@ type_check_name_expression(const nodes::NameExpression &expression,
nodes::TypeCheckResult
type_check_constructor(const nodes::Constructor &expression,
SourcesManager &sources_manager, State &state,
const Arguments &arguments) {} // IN PROGRESS
const Arguments &arguments) {
// TODO: find definition
for (size_t i = 0; i < expression.arguments_size(); ++i) {
const auto annotation = expression.get_argument_annotation(i);
auto argument_type = type_check_expression(
*expression.get_argument_value(i), sources_manager, state,
/* TODO: type from definition by annotation */);
}
// TODO: expect types of arguments by type definition
return type_same_to_expected(expression.get_type_proxy(),
arguments.expected_type, expression,
*sources_manager.get_error_log());
// TODO: add <- modifiier to type ??
} // IN PROGRESS
// TODO
nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
SourcesManager &sources_manager,
State &state,
const Arguments &arguments) {
if (!arguments.expected_type.has_value()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Can't deduce type of lambda function from context",
error_handling::ErrorType::TYPE_CHECK));
}
const auto &expected_type = arguments.expected_type.value();
if (!expected_type.get()->is_builtin(builtin::types::Type::FUNCTION)) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Type of lambda function should be function",
error_handling::ErrorType::TYPE_CHECK));
}
// TODO: define vars
// TODO: expect returned type as type of internal expression
// TODO: merge returned and brought type
// TODO: add context
// auto returned_type = type_check_expression(
// *expression.get_expression(), sources_manager, state, Arguments{});
return nodes::TypeCheckResult{expected_type};
} // IN PROGRESS
} // namespace type_check