* Don't capitalise the primop functions.

This commit is contained in:
Eelco Dolstra 2007-01-29 15:15:37 +00:00
parent 18e6096105
commit c558b1583c

View file

@ -22,7 +22,7 @@ namespace nix {
*************************************************************/ *************************************************************/
static Expr primBuiltins(EvalState & state, const ATermVector & args) static Expr prim_builtins(EvalState & state, const ATermVector & args)
{ {
/* Return an attribute set containing all primops. This allows /* Return an attribute set containing all primops. This allows
Nix expressions to test for new primops and take appropriate Nix expressions to test for new primops and take appropriate
@ -47,20 +47,20 @@ static Expr primBuiltins(EvalState & state, const ATermVector & args)
/* Boolean constructors. */ /* Boolean constructors. */
static Expr primTrue(EvalState & state, const ATermVector & args) static Expr prim_true(EvalState & state, const ATermVector & args)
{ {
return eTrue; return eTrue;
} }
static Expr primFalse(EvalState & state, const ATermVector & args) static Expr prim_false(EvalState & state, const ATermVector & args)
{ {
return eFalse; return eFalse;
} }
/* Return the null value. */ /* Return the null value. */
static Expr primNull(EvalState & state, const ATermVector & args) static Expr prim_null(EvalState & state, const ATermVector & args)
{ {
return makeNull(); return makeNull();
} }
@ -70,13 +70,13 @@ static Expr primNull(EvalState & state, const ATermVector & args)
that differs between platforms, so Nix expressions using that differs between platforms, so Nix expressions using
`__currentSystem' can evaluate to different values on different `__currentSystem' can evaluate to different values on different
platforms. */ platforms. */
static Expr primCurrentSystem(EvalState & state, const ATermVector & args) static Expr prim_currentSystem(EvalState & state, const ATermVector & args)
{ {
return makeStr(thisSystem); return makeStr(thisSystem);
} }
static Expr primCurrentTime(EvalState & state, const ATermVector & args) static Expr prim_currentTime(EvalState & state, const ATermVector & args)
{ {
return ATmake("Int(<int>)", time(0)); return ATmake("Int(<int>)", time(0));
} }
@ -89,7 +89,7 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args)
/* Load and evaluate an expression from path specified by the /* Load and evaluate an expression from path specified by the
argument. */ argument. */
static Expr primImport(EvalState & state, const ATermVector & args) static Expr prim_import(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
Path path = coerceToPath(state, args[0], context); Path path = coerceToPath(state, args[0], context);
@ -107,19 +107,8 @@ static Expr primImport(EvalState & state, const ATermVector & args)
} }
/* Convert the argument to a string. Paths are *not* copied to the
store, so `toString /foo/bar' yields `"/foo/bar"', not
`"/nix/store/whatever..."'. */
static Expr primToString(EvalState & state, const ATermVector & args)
{
PathSet context;
string s = coerceToString(state, args[0], context, true, false);
return makeStr(s, context);
}
/* Determine whether the argument is the null value. */ /* Determine whether the argument is the null value. */
static Expr primIsNull(EvalState & state, const ATermVector & args) static Expr prim_isNull(EvalState & state, const ATermVector & args)
{ {
return makeBool(matchNull(evalExpr(state, args[0]))); return makeBool(matchNull(evalExpr(state, args[0])));
} }
@ -177,7 +166,7 @@ static string relativise(Path pivot, Path p)
} }
static Expr primDependencyClosure(EvalState & state, const ATermVector & args) static Expr prim_dependencyClosure(EvalState & state, const ATermVector & args)
{ {
startNest(nest, lvlDebug, "finding dependencies"); startNest(nest, lvlDebug, "finding dependencies");
@ -272,7 +261,7 @@ static Expr primDependencyClosure(EvalState & state, const ATermVector & args)
} }
static Expr primAbort(EvalState & state, const ATermVector & args) static Expr prim_abort(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
throw Abort(format("evaluation aborted with the following error message: `%1%'") % throw Abort(format("evaluation aborted with the following error message: `%1%'") %
@ -281,14 +270,14 @@ static Expr primAbort(EvalState & state, const ATermVector & args)
/* Return an environment variable. Use with care. */ /* Return an environment variable. Use with care. */
static Expr primGetEnv(EvalState & state, const ATermVector & args) static Expr prim_getEnv(EvalState & state, const ATermVector & args)
{ {
string name = evalStringNoCtx(state, args[0]); string name = evalStringNoCtx(state, args[0]);
return makeStr(getEnv(name)); return makeStr(getEnv(name));
} }
static Expr primRelativise(EvalState & state, const ATermVector & args) static Expr prim_relativise(EvalState & state, const ATermVector & args)
{ {
PathSet context; /* !!! what to do? */ PathSet context; /* !!! what to do? */
Path pivot = coerceToPath(state, args[0], context); Path pivot = coerceToPath(state, args[0], context);
@ -364,7 +353,7 @@ static Hash hashDerivationModulo(EvalState & state, Derivation drv)
derivation; `drvPath' containing the path of the Nix expression; derivation; `drvPath' containing the path of the Nix expression;
and `type' set to `derivation' to indicate that this is a and `type' set to `derivation' to indicate that this is a
derivation. */ derivation. */
static Expr primDerivationStrict(EvalState & state, const ATermVector & args) static Expr prim_derivationStrict(EvalState & state, const ATermVector & args)
{ {
startNest(nest, lvlVomit, "evaluating derivation"); startNest(nest, lvlVomit, "evaluating derivation");
@ -535,7 +524,7 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args)
} }
static Expr primDerivationLazy(EvalState & state, const ATermVector & args) static Expr prim_derivationLazy(EvalState & state, const ATermVector & args)
{ {
Expr eAttrs = evalExpr(state, args[0]); Expr eAttrs = evalExpr(state, args[0]);
ATermMap attrs; ATermMap attrs;
@ -561,7 +550,7 @@ static Expr primDerivationLazy(EvalState & state, const ATermVector & args)
/* Convert the argument to a path. !!! obsolete? */ /* Convert the argument to a path. !!! obsolete? */
static Expr primToPath(EvalState & state, const ATermVector & args) static Expr prim_toPath(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
string path = coerceToPath(state, args[0], context); string path = coerceToPath(state, args[0], context);
@ -569,7 +558,7 @@ static Expr primToPath(EvalState & state, const ATermVector & args)
} }
static Expr primPathExists(EvalState & state, const ATermVector & args) static Expr prim_pathExists(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
Path path = coerceToPath(state, args[0], context); Path path = coerceToPath(state, args[0], context);
@ -581,7 +570,7 @@ static Expr primPathExists(EvalState & state, const ATermVector & args)
/* Return the base name of the given string, i.e., everything /* Return the base name of the given string, i.e., everything
following the last slash. */ following the last slash. */
static Expr primBaseNameOf(EvalState & state, const ATermVector & args) static Expr prim_baseNameOf(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
return makeStr(baseNameOf(coerceToString(state, args[0], context)), context); return makeStr(baseNameOf(coerceToString(state, args[0], context)), context);
@ -591,7 +580,7 @@ static Expr primBaseNameOf(EvalState & state, const ATermVector & args)
/* Return the directory of the given path, i.e., everything before the /* Return the directory of the given path, i.e., everything before the
last slash. Return either a path or a string depending on the type last slash. Return either a path or a string depending on the type
of the argument. */ of the argument. */
static Expr primDirOf(EvalState & state, const ATermVector & args) static Expr prim_dirOf(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
Expr e = evalExpr(state, args[0]); ATerm dummy; Expr e = evalExpr(state, args[0]); ATerm dummy;
@ -609,7 +598,7 @@ static Expr primDirOf(EvalState & state, const ATermVector & args)
/* Convert the argument (which can be any Nix expression) to an XML /* Convert the argument (which can be any Nix expression) to an XML
representation returned in a string. Not all Nix expressions can representation returned in a string. Not all Nix expressions can
be sensibly or completely represented (e.g., functions). */ be sensibly or completely represented (e.g., functions). */
static Expr primToXML(EvalState & state, const ATermVector & args) static Expr prim_toXML(EvalState & state, const ATermVector & args)
{ {
std::ostringstream out; std::ostringstream out;
PathSet context; PathSet context;
@ -620,7 +609,7 @@ static Expr primToXML(EvalState & state, const ATermVector & args)
/* Store a string in the Nix store as a source file that can be used /* Store a string in the Nix store as a source file that can be used
as an input by derivations. */ as an input by derivations. */
static Expr primToFile(EvalState & state, const ATermVector & args) static Expr prim_toFile(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
string name = evalStringNoCtx(state, args[0]); string name = evalStringNoCtx(state, args[0]);
@ -677,7 +666,7 @@ struct FilterFromExpr : PathFilter
}; };
static Expr primFilterSource(EvalState & state, const ATermVector & args) static Expr prim_filterSource(EvalState & state, const ATermVector & args)
{ {
PathSet context; PathSet context;
Path path = coerceToPath(state, args[1], context); Path path = coerceToPath(state, args[1], context);
@ -701,7 +690,7 @@ static Expr primFilterSource(EvalState & state, const ATermVector & args)
/* Return the names of the attributes in an attribute set as a sorted /* Return the names of the attributes in an attribute set as a sorted
list of strings. */ list of strings. */
static Expr primAttrNames(EvalState & state, const ATermVector & args) static Expr prim_attrNames(EvalState & state, const ATermVector & args)
{ {
ATermMap attrs; ATermMap attrs;
queryAllAttrs(evalExpr(state, args[0]), attrs); queryAllAttrs(evalExpr(state, args[0]), attrs);
@ -720,7 +709,7 @@ static Expr primAttrNames(EvalState & state, const ATermVector & args)
/* Dynamic version of the `.' operator. */ /* Dynamic version of the `.' operator. */
static Expr primGetAttr(EvalState & state, const ATermVector & args) static Expr prim_getAttr(EvalState & state, const ATermVector & args)
{ {
string attr = evalStringNoCtx(state, args[0]); string attr = evalStringNoCtx(state, args[0]);
return evalExpr(state, makeSelect(args[1], toATerm(attr))); return evalExpr(state, makeSelect(args[1], toATerm(attr)));
@ -728,14 +717,14 @@ static Expr primGetAttr(EvalState & state, const ATermVector & args)
/* Dynamic version of the `?' operator. */ /* Dynamic version of the `?' operator. */
static Expr primHasAttr(EvalState & state, const ATermVector & args) static Expr prim_hasAttr(EvalState & state, const ATermVector & args)
{ {
string attr = evalStringNoCtx(state, args[0]); string attr = evalStringNoCtx(state, args[0]);
return evalExpr(state, makeOpHasAttr(args[1], toATerm(attr))); return evalExpr(state, makeOpHasAttr(args[1], toATerm(attr)));
} }
static Expr primRemoveAttrs(EvalState & state, const ATermVector & args) static Expr prim_removeAttrs(EvalState & state, const ATermVector & args)
{ {
ATermMap attrs; ATermMap attrs;
queryAllAttrs(evalExpr(state, args[0]), attrs, true); queryAllAttrs(evalExpr(state, args[0]), attrs, true);
@ -756,7 +745,7 @@ static Expr primRemoveAttrs(EvalState & state, const ATermVector & args)
/* Determine whether the argument is a list. */ /* Determine whether the argument is a list. */
static Expr primIsList(EvalState & state, const ATermVector & args) static Expr prim_isList(EvalState & state, const ATermVector & args)
{ {
ATermList list; ATermList list;
return makeBool(matchList(evalExpr(state, args[0]), list)); return makeBool(matchList(evalExpr(state, args[0]), list));
@ -764,7 +753,7 @@ static Expr primIsList(EvalState & state, const ATermVector & args)
/* Return the first element of a list. */ /* Return the first element of a list. */
static Expr primHead(EvalState & state, const ATermVector & args) static Expr prim_head(EvalState & state, const ATermVector & args)
{ {
ATermList list = evalList(state, args[0]); ATermList list = evalList(state, args[0]);
if (ATisEmpty(list)) if (ATisEmpty(list))
@ -775,7 +764,7 @@ static Expr primHead(EvalState & state, const ATermVector & args)
/* Return a list consisting of everything but the the first element of /* Return a list consisting of everything but the the first element of
a list. */ a list. */
static Expr primTail(EvalState & state, const ATermVector & args) static Expr prim_tail(EvalState & state, const ATermVector & args)
{ {
ATermList list = evalList(state, args[0]); ATermList list = evalList(state, args[0]);
if (ATisEmpty(list)) if (ATisEmpty(list))
@ -785,7 +774,7 @@ static Expr primTail(EvalState & state, const ATermVector & args)
/* Apply a function to every element of a list. */ /* Apply a function to every element of a list. */
static Expr primMap(EvalState & state, const ATermVector & args) static Expr prim_map(EvalState & state, const ATermVector & args)
{ {
Expr fun = evalExpr(state, args[0]); Expr fun = evalExpr(state, args[0]);
ATermList list = evalList(state, args[1]); ATermList list = evalList(state, args[1]);
@ -803,7 +792,7 @@ static Expr primMap(EvalState & state, const ATermVector & args)
*************************************************************/ *************************************************************/
static Expr primAdd(EvalState & state, const ATermVector & args) static Expr prim_add(EvalState & state, const ATermVector & args)
{ {
int i1 = evalInt(state, args[0]); int i1 = evalInt(state, args[0]);
int i2 = evalInt(state, args[1]); int i2 = evalInt(state, args[1]);
@ -811,7 +800,7 @@ static Expr primAdd(EvalState & state, const ATermVector & args)
} }
static Expr primSub(EvalState & state, const ATermVector & args) static Expr prim_sub(EvalState & state, const ATermVector & args)
{ {
int i1 = evalInt(state, args[0]); int i1 = evalInt(state, args[0]);
int i2 = evalInt(state, args[1]); int i2 = evalInt(state, args[1]);
@ -819,7 +808,7 @@ static Expr primSub(EvalState & state, const ATermVector & args)
} }
static Expr primLessThan(EvalState & state, const ATermVector & args) static Expr prim_lessThan(EvalState & state, const ATermVector & args)
{ {
int i1 = evalInt(state, args[0]); int i1 = evalInt(state, args[0]);
int i2 = evalInt(state, args[1]); int i2 = evalInt(state, args[1]);
@ -832,6 +821,17 @@ static Expr primLessThan(EvalState & state, const ATermVector & args)
*************************************************************/ *************************************************************/
/* Convert the argument to a string. Paths are *not* copied to the
store, so `toString /foo/bar' yields `"/foo/bar"', not
`"/nix/store/whatever..."'. */
static Expr prim_toString(EvalState & state, const ATermVector & args)
{
PathSet context;
string s = coerceToString(state, args[0], context, true, false);
return makeStr(s, context);
}
/* `substr start len str' returns the substring of `str' starting at /* `substr start len str' returns the substring of `str' starting at
character position `min(start, stringLength str)' inclusive and character position `min(start, stringLength str)' inclusive and
ending at `min(start + len, stringLength str)'. `start' must be ending at `min(start + len, stringLength str)'. `start' must be
@ -857,60 +857,65 @@ static Expr prim_stringLength(EvalState & state, const ATermVector & args)
} }
/*************************************************************
* Primop registration
*************************************************************/
void EvalState::addPrimOps() void EvalState::addPrimOps()
{ {
addPrimOp("builtins", 0, primBuiltins); addPrimOp("builtins", 0, prim_builtins);
// Constants // Constants
addPrimOp("true", 0, primTrue); addPrimOp("true", 0, prim_true);
addPrimOp("false", 0, primFalse); addPrimOp("false", 0, prim_false);
addPrimOp("null", 0, primNull); addPrimOp("null", 0, prim_null);
addPrimOp("__currentSystem", 0, primCurrentSystem); addPrimOp("__currentSystem", 0, prim_currentSystem);
addPrimOp("__currentTime", 0, primCurrentTime); addPrimOp("__currentTime", 0, prim_currentTime);
// Miscellaneous // Miscellaneous
addPrimOp("import", 1, primImport); addPrimOp("import", 1, prim_import);
addPrimOp("toString", 1, primToString); addPrimOp("isNull", 1, prim_isNull);
addPrimOp("isNull", 1, primIsNull); addPrimOp("dependencyClosure", 1, prim_dependencyClosure);
addPrimOp("dependencyClosure", 1, primDependencyClosure); addPrimOp("abort", 1, prim_abort);
addPrimOp("abort", 1, primAbort); addPrimOp("__getEnv", 1, prim_getEnv);
addPrimOp("__getEnv", 1, primGetEnv);
addPrimOp("relativise", 2, primRelativise); addPrimOp("relativise", 2, prim_relativise);
// Derivations // Derivations
addPrimOp("derivation!", 1, primDerivationStrict); addPrimOp("derivation!", 1, prim_derivationStrict);
addPrimOp("derivation", 1, primDerivationLazy); addPrimOp("derivation", 1, prim_derivationLazy);
// Paths // Paths
addPrimOp("__toPath", 1, primToPath); addPrimOp("__toPath", 1, prim_toPath);
addPrimOp("__pathExists", 1, primPathExists); addPrimOp("__pathExists", 1, prim_pathExists);
addPrimOp("baseNameOf", 1, primBaseNameOf); addPrimOp("baseNameOf", 1, prim_baseNameOf);
addPrimOp("dirOf", 1, primDirOf); addPrimOp("dirOf", 1, prim_dirOf);
// Creating files // Creating files
addPrimOp("__toXML", 1, primToXML); addPrimOp("__toXML", 1, prim_toXML);
addPrimOp("__toFile", 2, primToFile); addPrimOp("__toFile", 2, prim_toFile);
addPrimOp("__filterSource", 2, primFilterSource); addPrimOp("__filterSource", 2, prim_filterSource);
// Attribute sets // Attribute sets
addPrimOp("__attrNames", 1, primAttrNames); addPrimOp("__attrNames", 1, prim_attrNames);
addPrimOp("__getAttr", 2, primGetAttr); addPrimOp("__getAttr", 2, prim_getAttr);
addPrimOp("__hasAttr", 2, primHasAttr); addPrimOp("__hasAttr", 2, prim_hasAttr);
addPrimOp("removeAttrs", 2, primRemoveAttrs); addPrimOp("removeAttrs", 2, prim_removeAttrs);
// Lists // Lists
addPrimOp("__isList", 1, primIsList); addPrimOp("__isList", 1, prim_isList);
addPrimOp("__head", 1, primHead); addPrimOp("__head", 1, prim_head);
addPrimOp("__tail", 1, primTail); addPrimOp("__tail", 1, prim_tail);
addPrimOp("map", 2, primMap); addPrimOp("map", 2, prim_map);
// Integer arithmetic // Integer arithmetic
addPrimOp("__add", 2, primAdd); addPrimOp("__add", 2, prim_add);
addPrimOp("__sub", 2, primSub); addPrimOp("__sub", 2, prim_sub);
addPrimOp("__lessThan", 2, primLessThan); addPrimOp("__lessThan", 2, prim_lessThan);
// String manipulation // String manipulation
addPrimOp("toString", 1, prim_toString);
addPrimOp("__substring", 3, prim_substring); addPrimOp("__substring", 3, prim_substring);
addPrimOp("__stringLength", 1, prim_stringLength); addPrimOp("__stringLength", 1, prim_stringLength);
} }