mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
functions arguments and parameters are separated now
This commit is contained in:
parent
f7e985a448
commit
a11ddbd25f
8 changed files with 49 additions and 98 deletions
|
|
@ -90,8 +90,6 @@ private:
|
||||||
|
|
||||||
void Visit(FunctionCallExpression* node) override;
|
void Visit(FunctionCallExpression* node) override;
|
||||||
|
|
||||||
void Visit(FunctionArgument& node) override; // variant
|
|
||||||
|
|
||||||
void Visit(TupleExpression* node) override;
|
void Visit(TupleExpression* node) override;
|
||||||
void Visit(VariantExpression* node) override;
|
void Visit(VariantExpression* node) override;
|
||||||
void Visit(ReturnExpression* node) override;
|
void Visit(ReturnExpression* node) override;
|
||||||
|
|
|
||||||
|
|
@ -232,12 +232,6 @@ struct ParametrizedTypeclass;
|
||||||
|
|
||||||
struct ParametrizedType;
|
struct ParametrizedType;
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
using FunctionArgument = std::variant<
|
|
||||||
SubExpressionToken,
|
|
||||||
std::unique_ptr<TypeExpression>>;
|
|
||||||
|
|
||||||
// Comments [IGNORE] -----------------
|
// Comments [IGNORE] -----------------
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
|
|
@ -468,7 +462,8 @@ struct FunctionCallExpression {
|
||||||
std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
|
std::optional<std::variant<std::unique_ptr<SubExpressionToken>,
|
||||||
std::unique_ptr<TypeExpression>>> prefix;
|
std::unique_ptr<TypeExpression>>> prefix;
|
||||||
ExtendedName name;
|
ExtendedName name;
|
||||||
std::vector<FunctionArgument> arguments;
|
std::vector<std::unique_ptr<TypeExpression>> parameters;
|
||||||
|
std::vector<SubExpressionToken> arguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TupleExpression {
|
struct TupleExpression {
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,6 @@ protected:
|
||||||
|
|
||||||
virtual void Visit(FunctionCallExpression* node);
|
virtual void Visit(FunctionCallExpression* node);
|
||||||
|
|
||||||
virtual void Visit(FunctionArgument& node); // variant
|
|
||||||
|
|
||||||
virtual void Visit(TupleExpression* node);
|
virtual void Visit(TupleExpression* node);
|
||||||
virtual void Visit(VariantExpression* node);
|
virtual void Visit(VariantExpression* node);
|
||||||
virtual void Visit(ReturnExpression* node);
|
virtual void Visit(ReturnExpression* node);
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
Subproject commit 1875ec1f091eaa96d62cc5ceee4c0adc7bb6d4a9
|
Subproject commit 910d1b4d312002a769869064e83250513257b63e
|
||||||
|
|
@ -905,26 +905,6 @@ void BuildVisitor::Visit(AccessExpression* node) {
|
||||||
|
|
||||||
// Other expressions
|
// Other expressions
|
||||||
|
|
||||||
void BuildVisitor::Visit(FunctionArgument& node) {
|
|
||||||
auto parse_node = current_node_;
|
|
||||||
|
|
||||||
current_node_ = parse_node.NthNamedChild(0);
|
|
||||||
|
|
||||||
std::string current_node_type = current_node_.GetType();
|
|
||||||
|
|
||||||
if (current_node_type == parser::tokens::SubExpressionToken) {
|
|
||||||
node = SubExpressionToken();
|
|
||||||
Visit(std::get<SubExpressionToken>(node));
|
|
||||||
} else if (current_node_type == parser::tokens::TypeExpression) {
|
|
||||||
node = std::make_unique<TypeExpression>();
|
|
||||||
Visit(std::get<std::unique_ptr<TypeExpression>>(node).get());
|
|
||||||
} else {
|
|
||||||
// error
|
|
||||||
}
|
|
||||||
|
|
||||||
current_node_ = parse_node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BuildVisitor::Visit(FunctionCallExpression* node) {
|
void BuildVisitor::Visit(FunctionCallExpression* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
|
@ -953,11 +933,24 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
|
||||||
size_t child_count = parse_node.NamedChildCount();
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
if (child_count > excluded_child_count) { // always true (repeat1)
|
if (child_count > excluded_child_count) { // always true (repeat1)
|
||||||
|
bool parameters_ended = false;
|
||||||
|
|
||||||
node->arguments.resize(child_count - excluded_child_count);
|
node->arguments.resize(child_count - excluded_child_count);
|
||||||
|
|
||||||
for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
|
for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
|
current_node_ = parse_node.NthNamedChild(i + excluded_child_count);
|
||||||
Visit(node->arguments[i]);
|
|
||||||
|
if (current_node_.GetType() != parser::tokens::TypeExpression) {
|
||||||
|
parameters_ended = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parameters_ended) {
|
||||||
|
node->parameters.push_back(std::make_unique<TypeExpression>());
|
||||||
|
Visit(node->parameters.back().get());
|
||||||
|
} else {
|
||||||
|
node->arguments.emplace_back();
|
||||||
|
Visit(node->arguments.back());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -438,6 +438,13 @@ void PrintVisitor::Visit(FunctionCallExpression* node) {
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
|
|
||||||
out_ << "] (";
|
out_ << "] (";
|
||||||
|
|
||||||
|
for (auto& parameter : node->parameters) {
|
||||||
|
Visit(parameter.get());
|
||||||
|
out_ << ", ";
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << ") (";
|
||||||
for (auto& argument : node->arguments) {
|
for (auto& argument : node->arguments) {
|
||||||
Visitor::Visit(argument);
|
Visitor::Visit(argument);
|
||||||
out_ << ", ";
|
out_ << ", ";
|
||||||
|
|
|
||||||
|
|
@ -539,60 +539,31 @@ void TypeCheckVisitor::Visit(FunctionCallExpression* node) {
|
||||||
}
|
}
|
||||||
info::definition::FunctionDeclaration function_declaration = maybe_function_declaration.value();
|
info::definition::FunctionDeclaration function_declaration = maybe_function_declaration.value();
|
||||||
|
|
||||||
// TODO: ??
|
//
|
||||||
// // Visitor::Visit(function_declaration.node->type.get());
|
|
||||||
// //
|
if (function_declaration.parameters.size() != node->parameters.size()) {
|
||||||
// // std::optional<info::type::FunctionType*> maybe_type_value = context_manager_.GetType<info::type::FunctionType>(current_type_);
|
error_handling::HandleTypecheckError("Mismatched parameter count in function call expression");
|
||||||
// // if (!maybe_type_value.has_value()) {
|
}
|
||||||
// // error_handling::HandleInternalError("Type of function is not FunctionType", "TypeCheckVisitor.FunctionCallExpresssion");
|
for (size_t i = 0; i < node->parameters.size(); ++i) {
|
||||||
// // }
|
Visit(node->parameters[i].get());
|
||||||
// //
|
|
||||||
// // info::type::FunctionType* type_value = maybe_type_value.value();
|
std::string parameter_name = function_declaration.parameters[i].type;
|
||||||
|
|
||||||
|
if (context.count(parameter_name) != 0) {
|
||||||
|
error_handling::HandleInternalError("Local abstract types with same name in one context", "TypeCheckVisitor.FunctionCallExpresssion"); // TypecheckError ??
|
||||||
|
}
|
||||||
|
context[parameter_name] = current_type_;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
size_t argument_num = 0;
|
if (function_declaration.argument_types.size() + 1 != node->arguments.size()) {
|
||||||
size_t paramater_num = 0;
|
error_handling::HandleTypecheckError("Mismatched argument count in function call expression");
|
||||||
|
}
|
||||||
// TODO: parameters and arguments should be separated, parameters go first
|
|
||||||
for (size_t i = 0; i < node->arguments.size(); ++i) {
|
for (size_t i = 0; i < node->arguments.size(); ++i) {
|
||||||
Visitor::Visit(node->arguments[i]);
|
Visitor::Visit(node->arguments[i]);
|
||||||
if (std::holds_alternative<std::unique_ptr<TypeExpression>>(node->arguments[i])) {
|
Visitor::Visit(*function_declaration.argument_types[i]);
|
||||||
if (function_declaration.parameters.size() <= paramater_num) {
|
context_manager_.AddTypeRequirement(current_type_, TypeInContext(current_type_, context));
|
||||||
error_handling::HandleTypecheckError("Mismatched parameter count in function call expression");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string parameter_name = function_declaration.parameters[paramater_num].type;
|
|
||||||
|
|
||||||
if (context.count(parameter_name) != 0) {
|
|
||||||
error_handling::HandleInternalError("Local abstract types with same name in one context", "TypeCheckVisitor.FunctionCallExpresssion"); // TypecheckError ??
|
|
||||||
}
|
|
||||||
|
|
||||||
context[parameter_name] = current_type_;
|
|
||||||
|
|
||||||
++paramater_num;
|
|
||||||
} else if (std::holds_alternative<SubExpressionToken>(node->arguments[i])) {
|
|
||||||
if (function_declaration.argument_types.size() <= argument_num) {
|
|
||||||
error_handling::HandleTypecheckError("Mismatched argument count in function call expression");
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: not full context, if arguments and parameters not sepoarated
|
|
||||||
|
|
||||||
Visitor::Visit(*function_declaration.argument_types[argument_num]);
|
|
||||||
context_manager_.AddTypeRequirement(current_type_, TypeInContext(current_type_, context));
|
|
||||||
|
|
||||||
++argument_num;
|
|
||||||
} else {
|
|
||||||
// error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (function_declaration.parameters.size() != paramater_num) {
|
|
||||||
error_handling::HandleTypecheckError("Mismatched parameter count in function call expression");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (function_declaration.argument_types.size() + 1 != argument_num) {
|
|
||||||
error_handling::HandleTypecheckError("Mismatched argument count in function call expression");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Visitor::Visit(*function_declaration.argument_types.back()); // add return type to info ??
|
Visitor::Visit(*function_declaration.argument_types.back()); // add return type to info ??
|
||||||
|
|
|
||||||
|
|
@ -248,22 +248,6 @@ void Visitor::Visit(SuperExpression& node) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple Expressions
|
|
||||||
|
|
||||||
void Visitor::Visit(FunctionArgument& node) {
|
|
||||||
switch (node.index()) {
|
|
||||||
case 0:
|
|
||||||
Visit(std::get<SubExpressionToken>(node));
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
Visit(std::get<std::unique_ptr<TypeExpression>>(node).get());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// error
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
|
|
||||||
void Visitor::Visit(AnyName& node) {
|
void Visitor::Visit(AnyName& node) {
|
||||||
|
|
@ -548,6 +532,11 @@ void Visitor::Visit(FunctionCallExpression* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
|
|
||||||
|
for (auto& parameter : node->parameters) {
|
||||||
|
Visit(parameter.get());
|
||||||
|
}
|
||||||
|
|
||||||
for (auto& argument : node->arguments) {
|
for (auto& argument : node->arguments) {
|
||||||
Visit(argument);
|
Visit(argument);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue