about summary refs log tree commit diff
path: root/src/libexpr/eval.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2010-03-30T14·39+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2010-03-30T14·39+0000
commitc3aa615a5f4a8bdde5d836ce91d5daa743ded065 (patch)
tree139a8782e58be7af83e06ae9dd6b9e9399b79fcb /src/libexpr/eval.cc
parent5b72d8a749e6718986f6e2bfef2ae725981a26ff (diff)
* More primops.
Diffstat (limited to 'src/libexpr/eval.cc')
-rw-r--r--src/libexpr/eval.cc32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index f1437e465709..06ca01ff44cf 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -95,14 +95,26 @@ EvalState::EvalState() : baseEnv(allocEnv())
 }
 
 
+void EvalState::addConstant(const string & name, Value & v)
+{
+    baseEnv.bindings[toATerm(name)] = v;
+    string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name;
+    (*baseEnv.bindings[toATerm("builtins")].attrs)[toATerm(name2)] = v;
+    nrValues += 2;
+}
+
+
 void EvalState::addPrimOp(const string & name,
     unsigned int arity, PrimOp primOp)
 {
-    Value & v = baseEnv.bindings[toATerm(name)];
-    nrValues++;
+    Value v;
     v.type = tPrimOp;
     v.primOp.arity = arity;
     v.primOp.fun = primOp;
+    baseEnv.bindings[toATerm(name)] = v;
+    string name2 = string(name, 0, 2) == "__" ? string(name, 2) : name;
+    (*baseEnv.bindings[toATerm("builtins")].attrs)[toATerm(name2)] = v;
+    nrValues += 2;
 }
 
 
@@ -212,6 +224,14 @@ Env & EvalState::allocEnv()
 }
 
 
+void EvalState::mkList(Value & v, unsigned int length)
+{
+    v.type = tList;
+    v.list.length = length;
+    v.list.elems = allocValues(length);
+}
+
+
 void EvalState::evalFile(const Path & path, Value & v)
 {
     startNest(nest, lvlTalkative, format("evaluating file `%1%'") % path);
@@ -349,9 +369,7 @@ void EvalState::eval(Env & env, Expr e, Value & v)
     }
 
     if (matchList(e, es)) {
-        v.type = tList;
-        v.list.length = ATgetLength(es);
-        v.list.elems = allocValues(v.list.length);
+        mkList(v, ATgetLength(es));
         for (unsigned int n = 0; n < v.list.length; ++n, es = ATgetNext(es))
             mkThunk(v.list.elems[n], env, ATgetFirst(es));
         return;
@@ -376,9 +394,7 @@ void EvalState::eval(Env & env, Expr e, Value & v)
         forceList(v1);
         Value v2; eval(env, e2, v2);
         forceList(v2);
-        v.type = tList;
-        v.list.length = v1.list.length + v2.list.length;
-        v.list.elems = allocValues(v.list.length);
+        mkList(v, v1.list.length + v2.list.length);
         /* !!! This loses sharing with the original lists.  We could
            use a tCopy node, but that would use more memory. */
         for (unsigned int n = 0; n < v1.list.length; ++n)