Type checking for functions (narrowing soon)
This commit is contained in:
@@ -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) {
|
void TypeChecker::checkSetNodeType(Node& node) {
|
||||||
@@ -562,6 +590,9 @@ namespace Solstice {
|
|||||||
case NodeType::LesserThan:
|
case NodeType::LesserThan:
|
||||||
checkLesserThanType(node);
|
checkLesserThanType(node);
|
||||||
break;
|
break;
|
||||||
|
case NodeType::FunctionCall:
|
||||||
|
checkFunctionCallNodeType(node);
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ namespace Solstice {
|
|||||||
void narrowBinaryNode(Node& node, std::unordered_map<TypePair, Type>& overloads);
|
void narrowBinaryNode(Node& node, std::unordered_map<TypePair, Type>& overloads);
|
||||||
void narrowFunctionCallNode(Node& node);
|
void narrowFunctionCallNode(Node& node);
|
||||||
|
|
||||||
|
void checkFunctionCallNodeType(Node& node);
|
||||||
|
|
||||||
void checkAddType(Node& node);
|
void checkAddType(Node& node);
|
||||||
void checkSubtractType(Node& node);
|
void checkSubtractType(Node& node);
|
||||||
void checkMultiplyType(Node& node);
|
void checkMultiplyType(Node& node);
|
||||||
|
|||||||
Reference in New Issue
Block a user