mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-25 08:18:45 +00:00
fixes, part of == for TypeProxy done, cotex enchantments, type chack arguments
This commit is contained in:
parent
6dc9c9b5af
commit
f36ff3638b
8 changed files with 195 additions and 121 deletions
|
|
@ -2,63 +2,62 @@
|
|||
|
||||
namespace type_check {
|
||||
|
||||
nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
|
||||
SourcesManager &sources_manager) {
|
||||
nodes::TypeProxy get_literal_type(const nodes::Literal &literal,
|
||||
SourcesManager &sources_manager) {
|
||||
switch (literal.get_any()->index()) {
|
||||
case 0: // float
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::FLOAT));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::FLOAT);
|
||||
case 1: // double
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::DOUBLE));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::DOUBLE);
|
||||
case 2: // int32_t
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::INT));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::INT);
|
||||
case 3: // int64_t
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::LONG));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::LONG);
|
||||
case 4: // size_t
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::INDEX));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::INDEX);
|
||||
case 5: // std::string
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->add_array_of(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::CHAR)));
|
||||
case 6: // unicode_string
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->add_array_of(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNICODE)));
|
||||
case 7: // char
|
||||
return nodes::TypeCheckResult(
|
||||
return sources_manager.get_type_storage()->add_array_of(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::CHAR));
|
||||
case 8: // unicode
|
||||
return nodes::TypeCheckResult(
|
||||
case 6: // unicode_string
|
||||
return sources_manager.get_type_storage()->add_array_of(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNICODE));
|
||||
case 7: // char
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::CHAR);
|
||||
case 8: // unicode
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNICODE);
|
||||
case 9: // bool
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::BOOL));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::BOOL);
|
||||
case 10: // unit
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNIT));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNIT);
|
||||
case 11: // null
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::NULL_OPTION));
|
||||
return sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::NULL_OPTION);
|
||||
}
|
||||
|
||||
error_handling::handle_general_error("Unreachable");
|
||||
exit(1); // unreachable
|
||||
}
|
||||
|
||||
nodes::TypeCheckResult type_check_literal(const nodes::Literal &literal,
|
||||
SourcesManager &sources_manager,
|
||||
nodes::MaybeTypeProxy expected_type) {
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace type_check
|
||||
|
|
|
|||
|
|
@ -543,7 +543,7 @@ build_name_expression(parser::ParseTree::Node parser_node,
|
|||
build_node(parser_node), build_identifier(name_node),
|
||||
std::move(arguments),
|
||||
prefix_node.has_value() ? build_type(prefix_node.value(), type_storage)
|
||||
: std::optional<nodes::TypeProxy>(),
|
||||
: nodes::MaybeTypeProxy(),
|
||||
is_point_call, false);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -252,9 +252,9 @@ nodes::TypeDefinition build_type_definition(parser::ParseTree::Node parser_node,
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<nodes::TypeProxy> type =
|
||||
nodes::MaybeTypeProxy type =
|
||||
type_node.has_value() ? build_type(type_node.value(), type_storage)
|
||||
: std::optional<nodes::TypeProxy>();
|
||||
: nodes::MaybeTypeProxy();
|
||||
|
||||
std::unordered_set<std::string> annotations;
|
||||
|
||||
|
|
|
|||
|
|
@ -14,56 +14,58 @@ namespace type_check {
|
|||
|
||||
nodes::TypeCheckResult
|
||||
type_check_expression(const nodes::Expression &expression,
|
||||
SourcesManager &sources_manager, State &state) {
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments) {
|
||||
switch (expression.get_any()->index()) {
|
||||
// --- flow control
|
||||
case 0: // Match
|
||||
return type_check_match(*expression.get<nodes::Match>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
case 1: // Condition
|
||||
return type_check_condition(*expression.get<nodes::Condition>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
case 2: // Loop
|
||||
return type_check_loop(*expression.get<nodes::Loop>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
// --- containers
|
||||
case 3: // Container
|
||||
return type_check_container(*expression.get<nodes::Container>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
// --- modifiers
|
||||
case 4: // Return
|
||||
return type_check_return(*expression.get<nodes::Return>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
case 5: // NameDefinition
|
||||
return type_check_name_definition(
|
||||
*expression.get<nodes::NameDefinition>().value(), sources_manager,
|
||||
state);
|
||||
case 6: // Access
|
||||
return type_check_access(*expression.get<nodes::Access>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
case 7: // LoopControl
|
||||
return type_check_loop_control(
|
||||
*expression.get<nodes::LoopControl>().value(), sources_manager, state);
|
||||
*expression.get<nodes::LoopControl>().value(), sources_manager, state,
|
||||
arguments);
|
||||
case 8: // ModifierExpression
|
||||
return type_check_modifier_expression(
|
||||
*expression.get<nodes::ModifierExpression>().value(), sources_manager,
|
||||
state);
|
||||
state, arguments);
|
||||
// --- other
|
||||
case 9: // NameExpression
|
||||
return type_check_name_expression(
|
||||
*expression.get<nodes::NameExpression>().value(), sources_manager,
|
||||
state);
|
||||
state, arguments);
|
||||
case 10: // Constructor
|
||||
return type_check_constructor(*expression.get<nodes::Constructor>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
case 11: // Lambda
|
||||
return type_check_lambda(*expression.get<nodes::Lambda>().value(),
|
||||
sources_manager, state);
|
||||
sources_manager, state, arguments);
|
||||
// --- literal
|
||||
case 12: // Literal
|
||||
// TODO
|
||||
return type_check_literal(*expression.get<nodes::Literal>().value(),
|
||||
sources_manager);
|
||||
sources_manager, arguments.expected_type);
|
||||
// --- empty lines
|
||||
case 13: // Extra
|
||||
return nodes::TypeCheckResult(
|
||||
|
|
@ -81,16 +83,21 @@ type_check_expression(const nodes::Expression &expression,
|
|||
// TODO
|
||||
nodes::TypeCheckResult type_check_match(const nodes::Match &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {} // IN PROGRESS
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
} // IN PROGRESS
|
||||
|
||||
// TODO
|
||||
nodes::TypeCheckResult type_check_condition(const nodes::Condition &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {} // IN PROGRESS
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
} // IN PROGRESS
|
||||
|
||||
nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
// TODO: ranges ??
|
||||
|
||||
std::optional<nodes::TypeCheckResult> condition_result;
|
||||
|
|
@ -176,7 +183,8 @@ nodes::TypeCheckResult type_check_loop(const nodes::Loop &expression,
|
|||
|
||||
nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
std::optional<nodes::TypeCheckResult> last_expression_result;
|
||||
|
||||
for (size_t i = 0; i < expression.expressions_size(); ++i) {
|
||||
|
|
@ -214,11 +222,14 @@ nodes::TypeCheckResult type_check_array(const nodes::Container &expression,
|
|||
// TODO
|
||||
nodes::TypeCheckResult type_check_block(const nodes::Container &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {} // IN PROGRESS
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
} // IN PROGRESS
|
||||
|
||||
nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
switch (expression.get_type()) {
|
||||
case nodes::Container::ARRAY:
|
||||
return type_check_array(expression, sources_manager, state);
|
||||
|
|
@ -233,7 +244,8 @@ nodes::TypeCheckResult type_check_container(const nodes::Container &expression,
|
|||
|
||||
nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
auto returned_result = type_check_expression(*expression.get_expression(),
|
||||
sources_manager, state);
|
||||
|
||||
|
|
@ -243,30 +255,23 @@ 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() != *returned_result.get().get()) {
|
||||
sources_manager.get_error_log()->add_error(
|
||||
error_handling::ErrorLog::ErrorMessage(
|
||||
expression, "Different brought type to current one",
|
||||
error_handling::ErrorType::TYPE_CHECK));
|
||||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
} else {
|
||||
state.brought_type = returned_result.get();
|
||||
if (state.bring_type(returned_result.get())) {
|
||||
sources_manager.get_error_log()->add_error(
|
||||
error_handling::ErrorLog::ErrorMessage(
|
||||
expression, "Different brought type to current one",
|
||||
error_handling::ErrorType::TYPE_CHECK));
|
||||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
break;
|
||||
case nodes::Return::RETURN:
|
||||
if (state.returned_type.has_value()) {
|
||||
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",
|
||||
error_handling::ErrorType::TYPE_CHECK));
|
||||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
} else {
|
||||
state.returned_type = returned_result.get();
|
||||
if (!state.return_type(returned_result.get())) {
|
||||
sources_manager.get_error_log()->add_error(
|
||||
error_handling::ErrorLog::ErrorMessage(
|
||||
expression, "Different returned type to current one",
|
||||
error_handling::ErrorType::TYPE_CHECK));
|
||||
return nodes::TypeCheckResult::construct_invalid_result();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return nodes::TypeCheckResult(
|
||||
|
|
@ -277,12 +282,13 @@ nodes::TypeCheckResult type_check_return(const nodes::Return &expression,
|
|||
// TODO
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_definition(const nodes::NameDefinition &expression,
|
||||
SourcesManager &sources_manager, State &state) {
|
||||
} // IN PROGRESS
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments) {} // IN PROGRESS
|
||||
|
||||
nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
|
||||
auto index_result =
|
||||
type_check_expression(*expression.get_index(), sources_manager, state);
|
||||
|
|
@ -322,7 +328,8 @@ nodes::TypeCheckResult type_check_array_access(const nodes::Access &expression,
|
|||
|
||||
nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
auto value_result =
|
||||
type_check_expression(*expression.get_value(), sources_manager, state);
|
||||
|
||||
|
|
@ -352,7 +359,8 @@ nodes::TypeCheckResult type_check_tuple_access(const nodes::Access &expression,
|
|||
|
||||
nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
switch (expression.get_type()) {
|
||||
case nodes::Access::ARRAY:
|
||||
return type_check_array_access(expression, sources_manager, state);
|
||||
|
|
@ -363,7 +371,8 @@ nodes::TypeCheckResult type_check_access(const nodes::Access &expression,
|
|||
|
||||
nodes::TypeCheckResult type_check_loop_control(const nodes::LoopControl &,
|
||||
SourcesManager &sources_manager,
|
||||
State &) {
|
||||
State &,
|
||||
const Arguments &arguments) {
|
||||
return nodes::TypeCheckResult(
|
||||
sources_manager.get_type_storage()->primitive_type(
|
||||
builtin::types::Type::UNIT));
|
||||
|
|
@ -371,7 +380,8 @@ nodes::TypeCheckResult type_check_loop_control(const nodes::LoopControl &,
|
|||
|
||||
nodes::TypeCheckResult
|
||||
type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
||||
SourcesManager &sources_manager, State &state) {
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments) {
|
||||
auto modified_result = type_check_expression(*expression.get_expression(),
|
||||
sources_manager, state);
|
||||
|
||||
|
|
@ -409,18 +419,20 @@ type_check_modifier_expression(const nodes::ModifierExpression &expression,
|
|||
// TODO
|
||||
nodes::TypeCheckResult
|
||||
type_check_name_expression(const nodes::NameExpression &expression,
|
||||
SourcesManager &sources_manager, State &state) {
|
||||
} // IN PROGRESS
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments) {} // IN PROGRESS
|
||||
|
||||
// TODO
|
||||
nodes::TypeCheckResult
|
||||
type_check_constructor(const nodes::Constructor &expression,
|
||||
SourcesManager &sources_manager, State &state) {
|
||||
} // IN PROGRESS
|
||||
SourcesManager &sources_manager, State &state,
|
||||
const Arguments &arguments) {} // IN PROGRESS
|
||||
|
||||
// TODO
|
||||
nodes::TypeCheckResult type_check_lambda(const nodes::Lambda &expression,
|
||||
SourcesManager &sources_manager,
|
||||
State &state) {} // IN PROGRESS
|
||||
State &state,
|
||||
const Arguments &arguments) {
|
||||
} // IN PROGRESS
|
||||
|
||||
} // namespace type_check
|
||||
|
|
|
|||
|
|
@ -6,4 +6,8 @@ Type *TypeProxy::get() { return type_storage_->get_type(id_); }
|
|||
|
||||
const Type *TypeProxy::get() const { return type_storage_->get_type(id_); }
|
||||
|
||||
bool TypeProxy::operator==(const TypeProxy& other) const {
|
||||
return *get() == *other.get();
|
||||
}
|
||||
|
||||
}; // namespace nodes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue