Type checking for functions (narrowing soon)

This commit is contained in:
2026-08-03 20:23:17 +10:00
parent 5fd82fd322
commit fa0b3ce34d
2 changed files with 35 additions and 2 deletions

View File

@@ -395,8 +395,36 @@ namespace Solstice {
}
void TypeChecker::narrowFunctionCallNode(Node& node) {
void TypeChecker::checkFunctionCallNodeType(Node& node) {
// get function
auto name = node.children[0].getIdentifier();
if (!name.has_value()) {
throw std::runtime_error("identifier node does not contain identifier");
}
if (functions.find(*name) == functions.end()) {
throw std::runtime_error("unknown function " + *name);
}
auto& function = functions[*name];
// get types we are calling with
std::vector<Type> args;
for (auto& child : node.children[1].children) {
checkNodeType(child);
auto childType = child.ptype.getOnlyType();
if (!childType.has_value()) {
throw std::runtime_error("cannot call function with ambiguous type");
}
args.push_back(*childType);
}
// find argument type
if (function.returnTypes.find(args) == function.returnTypes.end()) {
throw std::runtime_error("no matching function call to " + *name);
}
node.ptype = {{function.returnTypes[args]}};
}
void TypeChecker::checkSetNodeType(Node& node) {
@@ -562,6 +590,9 @@ namespace Solstice {
case NodeType::LesserThan:
checkLesserThanType(node);
break;
case NodeType::FunctionCall:
checkFunctionCallNodeType(node);
break;
}
}

View File

@@ -49,6 +49,8 @@ namespace Solstice {
void narrowBinaryNode(Node& node, std::unordered_map<TypePair, Type>& overloads);
void narrowFunctionCallNode(Node& node);
void checkFunctionCallNodeType(Node& node);
void checkAddType(Node& node);
void checkSubtractType(Node& node);
void checkMultiplyType(Node& node);