mini-refactor "lambda.name or anonymous lambda" logic

Change-Id: I08d39c4ad5b967de526c0d5c5e6299256c7967f3
This commit is contained in:
Qyriad 2024-05-31 13:12:14 -06:00
parent 010d93393e
commit 19a93dd025
2 changed files with 29 additions and 8 deletions

View file

@ -1595,7 +1595,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
if (!j) {
if (!i.def) {
error<TypeError>("function '%1%' called without required argument '%2%'",
(lambda.name ? std::string(symbols[lambda.name]) : "anonymous lambda"),
lambda.getName(symbols),
symbols[i.name])
.atPos(lambda.pos)
.withTrace(pos, "from call site")
@ -1621,7 +1621,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
formalNames.insert(symbols[formal.name]);
auto suggestions = Suggestions::bestMatches(formalNames, symbols[i.name]);
error<TypeError>("function '%1%' called with unexpected argument '%2%'",
(lambda.name ? std::string(symbols[lambda.name]) : "anonymous lambda"),
lambda.getName(symbols),
symbols[i.name])
.atPos(lambda.pos)
.withTrace(pos, "from call site")
@ -1642,9 +1642,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
? makeDebugTraceStacker(
*this, *lambda.body, env2, positions[lambda.pos],
"while calling %s",
lambda.name
? concatStrings("'", symbols[lambda.name], "'")
: "anonymous lambda")
lambda.getQuotedName(symbols))
: nullptr;
lambda.body->eval(*this, env2, vCur);
@ -1654,9 +1652,7 @@ void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value &
e,
lambda.pos,
"while calling %s",
lambda.name
? concatStrings("'", symbols[lambda.name], "'")
: "anonymous lambda");
lambda.getQuotedName(symbols));
if (pos) addErrorTrace(e, pos, "from call site");
}
throw;

View file

@ -297,6 +297,31 @@ struct ExprLambda : Expr
std::string showNamePos(const EvalState & state) const;
inline bool hasFormals() const { return formals != nullptr; }
PosIdx getPos() const override { return pos; }
/** Returns the name of the lambda,
* or "anonymous lambda" if it doesn't have one.
*/
inline std::string getName(SymbolTable const & symbols) const
{
if (this->name) {
return symbols[this->name];
}
return "anonymous lambda";
}
/** Returns the name of the lambda in single quotes,
* or "anonymous lambda" if it doesn't have one.
*/
inline std::string getQuotedName(SymbolTable const & symbols) const
{
if (this->name) {
return concatStrings("'", symbols[this->name], "'");
}
return "anonymous lambda";
}
COMMON_METHODS
};