Store Nix integers as longs

So on 64-bit systems, integers are now 64-bit.

Fixes #158.
This commit is contained in:
Eelco Dolstra 2013-08-19 12:35:03 +02:00
parent 297b762513
commit d308aeaf53
9 changed files with 25 additions and 23 deletions

View file

@ -968,7 +968,7 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
{
PathSet context;
std::ostringstream s;
int n = 0;
NixInt n = 0;
bool first = true;
ValueType firstType;
@ -1021,7 +1021,7 @@ void EvalState::strictForceValue(Value & v)
}
int EvalState::forceInt(Value & v)
NixInt EvalState::forceInt(Value & v)
{
forceValue(v);
if (v.type != tInt)

View file

@ -159,7 +159,7 @@ public:
void strictForceValue(Value & v);
/* Force `v', and then verify that it has the expected type. */
int forceInt(Value & v);
NixInt forceInt(Value & v);
bool forceBool(Value & v);
inline void forceAttrs(Value & v);
inline void forceList(Value & v);

View file

@ -110,8 +110,10 @@ or { return OR_KW; }
\+\+ { return CONCAT; }
{ID} { yylval->id = strdup(yytext); return ID; }
{INT} { int n = atoi(yytext); /* !!! overflow */
yylval->n = n;
{INT} { errno = 0;
yylval->n = strtol(yytext, 0, 10);
if (errno != 0)
throw ParseError(format("invalid integer `%1%'") % yytext);
return INT;
}

View file

@ -75,9 +75,9 @@ std::ostream & operator << (std::ostream & str, Expr & e);
struct ExprInt : Expr
{
int n;
NixInt n;
Value v;
ExprInt(int n) : n(n) { mkInt(v, n); };
ExprInt(NixInt n) : n(n) { mkInt(v, n); };
COMMON_METHODS
Value * maybeThunk(EvalState & state, Env & env);
};

View file

@ -238,7 +238,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
nix::ExprAttrs * attrs;
nix::Formals * formals;
nix::Formal * formal;
int n;
nix::NixInt n;
char * id; // !!! -> Symbol
char * path;
char * uri;

View file

@ -1024,7 +1024,7 @@ static void prim_mul(EvalState & state, Value * * args, Value & v)
static void prim_div(EvalState & state, Value * * args, Value & v)
{
int i2 = state.forceInt(*args[1]);
NixInt i2 = state.forceInt(*args[1]);
if (i2 == 0) throw EvalError("division by zero");
mkInt(v, state.forceInt(*args[0]) / i2);
}

View file

@ -31,14 +31,17 @@ struct PrimOp;
struct Symbol;
typedef long NixInt;
struct Value
{
ValueType type;
union
union
{
int integer;
NixInt integer;
bool boolean;
/* Strings in the evaluator carry a so-called `context' which
is a list of strings representing store paths. This is to
allow users to write things like
@ -63,7 +66,7 @@ struct Value
const char * s;
const char * * context; // must be in sorted order
} string;
const char * path;
Bindings * attrs;
struct {
@ -97,7 +100,7 @@ static inline void clearValue(Value & v)
}
static inline void mkInt(Value & v, int n)
static inline void mkInt(Value & v, NixInt n)
{
clearValue(v);
v.type = tInt;

View file

@ -1083,14 +1083,6 @@ bool statusOk(int status)
}
string int2String(int n)
{
std::ostringstream str;
str << n;
return str.str();
}
bool hasSuffix(const string & s, const string & suffix)
{
return s.size() >= suffix.size() && string(s, s.size() - suffix.size()) == suffix;

View file

@ -319,7 +319,12 @@ template<class N> bool string2Int(const string & s, N & n)
return str && str.get() == EOF;
}
string int2String(int n);
template<class N> string int2String(N n)
{
std::ostringstream str;
str << n;
return str.str();
}
/* Return true iff `s' ends in `suffix'. */