type_check_visitor fixes, array access and function call syntax change

This commit is contained in:
ProgramSnail 2023-05-07 09:25:38 +03:00
parent f7080ba856
commit c1dec6a0d1
16 changed files with 426 additions and 311 deletions

Binary file not shown.

View file

@ -985,7 +985,7 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
size_t child_count = parse_node.NamedChildCount();
if (child_count > excluded_child_count) { // always true (repeat1)
if (child_count > excluded_child_count) {
bool parameters_ended = false;
node->arguments.resize(child_count - excluded_child_count);

View file

@ -421,7 +421,7 @@ void PrintVisitor::Visit(ReferenceExpression* node) {
void PrintVisitor::Visit(AccessExpression* node) {
out_ << "[AccessExpression] (";
Visit(node->name.get());
out_ << ") : (";
out_ << ") ` (";
Visitor::Visit(node->id);
out_ << ')';
}
@ -447,15 +447,26 @@ void PrintVisitor::Visit(FunctionCallExpression* node) {
out_ << "] (";
bool is_first = true;
for (auto& parameter : node->parameters) {
if (!is_first) {
out_ << ", ";
}
is_first = false;
Visit(parameter.get());
out_ << ", ";
}
out_ << ") (";
out_ << ") : (";
is_first = true;
for (auto& argument : node->arguments) {
if (!is_first) {
out_ << ", ";
}
is_first = false;
Visitor::Visit(argument);
out_ << ", ";
}
out_ << ")";
}

View file

@ -524,12 +524,85 @@ void TypeCheckVisitor::Visit(LoopControlExpression& node) { // enum
// Operators
// TODO
void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
// TODO: Check, that type is not abstract ??
auto maybe_operator_id = namespace_visitor_.FindFunction(std::nullopt, node->operator_name);
if (maybe_operator_id.has_value()) {
const info::definition::Function& operator_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value());
if (!operator_info.declaration.has_value()) {
error_handling::HandleTypecheckError("Operator declaration not found", node->base);
}
if (!operator_info.definition.has_value()) { // TODO: builtin, etc.
error_handling::HandleTypecheckError("Operator definition not found", node->base);
}
if (operator_info.argument_count != 3) { // 2 + return type
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
}
if (operator_info.declaration->parameters.size() > 0) {
error_handling::HandleTypecheckError("Operator with parameters", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[0]);
utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->left_expression);
if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) {
error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[1]);
utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->right_expression);
if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) {
error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
return;
}
Visitor::Visit(node->left_expression);
auto maybe_left_type = context_manager_.GetType<info::type::DefinedType>(current_type_);
utils::ValueType left_value_type = context_manager_.GetValueType(current_type_);
if (!maybe_left_type.has_value()) {
error_handling::HandleTypecheckError("Operator not found (type is not DefinedType)", node->base);
}
auto maybe_left_type_info = namespace_visitor_.GetGlobalInfo()->GetTypeInfo<info::definition::AnyType>(maybe_left_type.value()->GetTypeId());
std::string left_type_name = maybe_left_type_info.value().type.type;
auto maybe_const_operator_id = namespace_visitor_.FindMethod(std::nullopt,
left_type_name,
node->operator_name,
utils::IsConstModifier::Const);
if (left_value_type != utils::ValueType::Const) {
maybe_operator_id = namespace_visitor_.FindMethod(std::nullopt,
left_type_name,
node->operator_name,
utils::IsConstModifier::Var);
}
if (!maybe_operator_id.has_value() && !maybe_const_operator_id.has_value()) {
error_handling::HandleTypecheckError("Operator not found (method name not found)", node->base);
}
if (maybe_operator_id.has_value() && maybe_const_operator_id.has_value()) {
error_handling::HandleTypecheckError("Ambigious operator (const and var method)", node->base);
}
if (!maybe_operator_id.has_value()) {
error_handling::HandleTypecheckError("Operator not found", node->base);
maybe_operator_id = maybe_const_operator_id;
}
const info::definition::Function& operator_info = namespace_visitor_.GetGlobalInfo()->GetFunctionInfo(maybe_operator_id.value());
@ -542,7 +615,7 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
error_handling::HandleTypecheckError("Operator definition not found", node->base);
}
if (operator_info.argument_count != 2) {
if (operator_info.argument_count != 2) { // argument + return type
error_handling::HandleTypecheckError("Operator wrong argument count", node->base);
}
@ -550,21 +623,15 @@ void TypeCheckVisitor::Visit(BinaryOperatorExpression* node) {
error_handling::HandleTypecheckError("Operator with parameters", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[0]);
utils::IdType left_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->left_expression);
if (!context_manager_.AddTypeRequirement(current_type_, left_expression_type)) {
error_handling::HandleTypecheckError("Operator left expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types[1]);
Visitor::Visit(*operator_info.declaration.value().argument_types[9]);
utils::IdType right_expression_type = current_type_; // TODO: type in context of deduced types
Visitor::Visit(node->right_expression);
if (!context_manager_.AddTypeRequirement(current_type_, right_expression_type)) {
error_handling::HandleTypecheckError("Operator right expression has wrong type", node->base);
}
Visitor::Visit(*operator_info.declaration.value().argument_types.back());
}
void TypeCheckVisitor::Visit(UnaryOperatorExpression* node) {
@ -748,7 +815,7 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
context_manager_.AddTypeRequirement(current_type_, argument_type);
}
Visitor::Visit(*function_declaration.argument_types.back()); // add return type to info ??
Visitor::Visit(*function_declaration.argument_types.back());
current_type_ = TypeInContext(current_type_, context);
}