diff --git a/bootstrap/typecheck.cc b/bootstrap/typecheck.cc index a4f7175..f7b4cb3 100644 --- a/bootstrap/typecheck.cc +++ b/bootstrap/typecheck.cc @@ -70,28 +70,29 @@ std::any TypeCheckVisitor::visitFactor(xlangParser::FactorContext *ctx) { auto token = identifier->getSymbol(); auto name = token->getText(); if (ctx->LeftParen()) { - if (function_arity.find(name) == function_arity.end()) { + if (function_arity.find(name) != function_arity.end()) { + if (auto expr_list = ctx->exprList()) { + auto arity = function_arity[name]; + auto arg_num = expr_list->expr().size(); + if (arity != arg_num) { + errorlistener.typeError( + token->getLine(), token->getCharPositionInLine(), + "function '" + name + "' expects " + std::to_string(arity) + + " arguments, but got " + std::to_string(arg_num)); + } + } else { + if (auto arity = function_arity[name]) { + errorlistener.typeError( + token->getLine(), token->getCharPositionInLine(), + "function '" + name + "' expects " + std::to_string(arity) + + " arguments, but got none"); + } + } + } else { errorlistener.typeError(token->getLine(), token->getCharPositionInLine(), "unknown function '" + name + "'"); } - if (auto expr_list = ctx->exprList()) { - auto arity = function_arity[name]; - auto arg_num = expr_list->expr().size(); - if (arity != arg_num) { - errorlistener.typeError( - token->getLine(), token->getCharPositionInLine(), - "function '" + name + "' expects " + std::to_string(arity) + - " arguments, but got " + std::to_string(arg_num)); - } - } else { - if (auto arity = function_arity[name]) { - errorlistener.typeError( - token->getLine(), token->getCharPositionInLine(), - "function '" + name + "' expects " + std::to_string(arity) + - " arguments, but got none"); - } - } visitChildren(ctx); } else { if (!inScope(name)) {