TypeProxy changed to handle modifiers internally

This commit is contained in:
ProgramSnail 2023-08-13 12:47:32 +03:00
parent 5f8d5c5569
commit fe6507ae12
7 changed files with 222 additions and 114 deletions

View file

@ -59,6 +59,6 @@ nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
error_handling::handle_general_error("Unreachable");
exit(1); // unreachable
} // IN PROGRESS: modifiers ??
}
} // namespace type_check

View file

@ -87,12 +87,13 @@ nodes::TypeProxy build_array_type(parser::ParseTree::Node parser_node,
build_node(parser_node));
}
// TODO: add different reference types
// '^' type
nodes::TypeProxy build_reference_type(parser::ParseTree::Node parser_node,
nodes::TypeStorage &type_storage) {
nodes::TypeProxy type =
build_type(parser_node.nth_named_child(0), type_storage);
type.get()->set_is_on_heap(true);
type.get()->set_modifier(nodes::Modifier::REF);
return type;
}
@ -124,7 +125,7 @@ nodes::TypeProxy build_modified_type(parser::ParseTree::Node parser_node,
return type_storage.add_type(
nodes::Type(nodes::Identifier(build_node(parser_node),
nodes::Identifier::SIMPLE_TYPE, identifier),
std::move(parameters), false));
std::move(parameters)));
}
// type_identifier ('[' type+ ']')?
@ -141,7 +142,7 @@ nodes::TypeProxy build_simple_type(parser::ParseTree::Node parser_node,
}
return type_storage.add_type(
nodes::Type(build_identifier(name_node), std::move(parameters), false));
nodes::Type(build_identifier(name_node), std::move(parameters)));
}
} // namespace builders

View file

@ -63,7 +63,7 @@ type_check_expression(const nodes::Expression &expression,
case 12: // Literal
// TODO
return type_check_literal(*expression.get<nodes::Literal>().value(),
sources_manager, state);
sources_manager);
// --- empty lines
case 13: // Extra
return nodes::TypeCheckResult(
@ -113,7 +113,7 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
return condition_result.value();
}
if (condition_result.value().get().get_type()->to_builtin() !=
if (condition_result.value().get().get()->to_builtin() !=
builtin::types::Type::BOOL) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
@ -149,7 +149,7 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
// }
// TODO: ranges ??
if (condition_result.value().get().get_type()->to_builtin() !=
if (condition_result.value().get().get()->to_builtin() !=
builtin::types::Type::ARRAY) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
@ -165,12 +165,11 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
return expression_result;
}
// TODO: modifier checks ??
// TODO: modifier checks ??, modifiers ??
return nodes::TypeCheckResult(
nodes::ModifiedTypeProxy(sources_manager.get_type_storage()->add_array_of(
expression_result.get().get_type_proxy()),
expression_result.get().get_modifier()));
sources_manager.get_type_storage()->add_array_of(
expression_result.get()));
} // IN PROGRESS
// --- containers
@ -187,7 +186,8 @@ nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
if (!last_expression_result.has_value()) {
last_expression_result = expression_result;
} else {
if (last_expression_result.value().get() != expression_result.get()) {
if (*last_expression_result.value().get().get() !=
*expression_result.get().get()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
*expression.get_expression(i),
@ -206,10 +206,9 @@ nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
return nodes::TypeCheckResult::construct_invalid_result();
}
return nodes::TypeCheckResult(nodes::ModifiedTypeProxy(
return nodes::TypeCheckResult(
sources_manager.get_type_storage()->add_array_of(
last_expression_result.value().get().get_type_proxy()),
last_expression_result.value().get().get_modifier()));
last_expression_result.value().get()));
} // IN PROGRESS
// TODO
@ -245,8 +244,7 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
switch (expression.get_type()) {
case nodes::Return::BRING:
if (state.brought_type.has_value()) {
if (*state.brought_type.value().get_type() !=
*returned_result.get().get_type()) {
if (*state.brought_type.value().get() != *returned_result.get().get()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Different brought type to current one",
@ -259,8 +257,7 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
break;
case nodes::Return::RETURN:
if (state.returned_type.has_value()) {
if (*state.returned_type.value().get_type() !=
*returned_result.get().get_type()) {
if (*state.returned_type.value().get() != *returned_result.get().get()) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Different returned type to current one",
@ -301,8 +298,7 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
return value_result;
}
if (index_result.get().get_type()->to_builtin() !=
builtin::types::Type::INDEX) {
if (index_result.get().get()->to_builtin() != builtin::types::Type::INDEX) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Can access only by Index",
@ -310,8 +306,7 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
return nodes::TypeCheckResult::construct_invalid_result();
}
if (value_result.get().get_type()->to_builtin() !=
builtin::types::Type::ARRAY) {
if (value_result.get().get()->to_builtin() != builtin::types::Type::ARRAY) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Can apply array access only to Array",
@ -322,7 +317,7 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
// TODO: modifier checks ??
return nodes::TypeCheckResult(
value_result.get().get_type()->get_parameter_proxy(0));
value_result.get().get()->get_parameter_proxy(0));
}
nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
@ -341,8 +336,7 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
->get<size_t>() // Index type
.value();
if (value_result.get().get_type()->to_builtin() !=
builtin::types::Type::TUPLE) {
if (value_result.get().get()->to_builtin() != builtin::types::Type::TUPLE) {
sources_manager.get_error_log()->add_error(
error_handling::ErrorLog::ErrorMessage(
expression, "Can apply tuple access only to Tuple",
@ -353,7 +347,7 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
// TODO: modifier checks ??
return nodes::TypeCheckResult(
value_result.get().get_type()->get_parameter_proxy(index));
value_result.get().get()->get_parameter_proxy(index));
}
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
@ -388,12 +382,11 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
// '!' - open optional / result -> value / panic
switch (modified_result.get().get_type()->to_builtin()) {
switch (modified_result.get().get()->to_builtin()) {
case builtin::types::Type::OPTIONAL:
case builtin::types::Type::RESULT:
modified_result.set(nodes::ModifiedTypeProxy(
modified_result.get().get_type()->get_parameter_proxy(0),
modified_result.get().get_modifier()));
// TODO: how to unwrap external modifier ??
modified_result.set(modified_result.get().get()->get_parameter_proxy(0));
break;
default:
sources_manager.get_error_log()->add_error(
@ -404,7 +397,8 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
}
} else {
// TODO: check that modifier can be applied
modified_result.get().set_modifier(expression.get_modifier());
modified_result.set(sources_manager.get_type_storage()->add_modification_of(
modified_result.get(), expression.get_modifier()));
}
return modified_result;

View file

@ -7,7 +7,8 @@ namespace printers {
// TODO: better printing format for builtin types
void print_type(const nodes::Type &type, printers::Printer &printer) {
if (type.is_on_heap()) {
// TODO: more modifier types
if (type.get_modifier() == nodes::Modifier::REF) {
printer.print("^");
}