forked from lix-project/lix
Overload the ‘+’ operator to support integer addition
This commit is contained in:
parent
511455965e
commit
159e621d1a
|
@ -968,31 +968,39 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
|
||||||
{
|
{
|
||||||
PathSet context;
|
PathSet context;
|
||||||
std::ostringstream s;
|
std::ostringstream s;
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
bool first = true, isPath = false;
|
bool first = true;
|
||||||
Value vStr;
|
ValueType firstType;
|
||||||
|
|
||||||
foreach (vector<Expr *>::iterator, i, *es) {
|
foreach (vector<Expr *>::iterator, i, *es) {
|
||||||
(*i)->eval(state, env, vStr);
|
Value vTmp;
|
||||||
|
(*i)->eval(state, env, vTmp);
|
||||||
|
|
||||||
/* If the first element is a path, then the result will also
|
/* If the first element is a path, then the result will also
|
||||||
be a path, we don't copy anything (yet - that's done later,
|
be a path, we don't copy anything (yet - that's done later,
|
||||||
since paths are copied when they are used in a derivation),
|
since paths are copied when they are used in a derivation),
|
||||||
and none of the strings are allowed to have contexts. */
|
and none of the strings are allowed to have contexts. */
|
||||||
if (first) {
|
if (first) {
|
||||||
isPath = !forceString && vStr.type == tPath;
|
firstType = vTmp.type;
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
s << state.coerceToString(vStr, context, false, !isPath);
|
if (firstType == tInt && !forceString) {
|
||||||
|
if (vTmp.type != tInt)
|
||||||
|
throwEvalError("cannot add %1% to an integer", showType(vTmp));
|
||||||
|
n += vTmp.integer;
|
||||||
|
} else
|
||||||
|
s << state.coerceToString(vTmp, context, false, firstType != tPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPath && !context.empty())
|
if (firstType == tInt)
|
||||||
throwEvalError("a string that refers to a store path cannot be appended to a path, in `%1%'", s.str());
|
mkInt(v, n);
|
||||||
|
else if (firstType == tPath) {
|
||||||
if (isPath)
|
if (!context.empty())
|
||||||
|
throwEvalError("a string that refers to a store path cannot be appended to a path, in `%1%'", s.str());
|
||||||
mkPath(v, s.str().c_str());
|
mkPath(v, s.str().c_str());
|
||||||
else
|
} else
|
||||||
mkString(v, s.str(), context);
|
mkString(v, s.str(), context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
1275
|
1854
|
||||||
|
|
|
@ -16,6 +16,6 @@ let {
|
||||||
range = range_ [];
|
range = range_ [];
|
||||||
*/
|
*/
|
||||||
|
|
||||||
body = sum (range 1 50);
|
body = sum (range 1 50) + 123 + 456;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue