about summary refs log tree commit diff
path: root/nix-repl.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T14·02+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T14·02+0200
commitadde4f0c8d720b9c9c43f6d0a2e789d7c68798bd (patch)
tree0b95142e311136ffe5537a72b91a08fbf6e3cf2c /nix-repl.cc
parentddd22c37c58aa9d39a632c746a0617f6602d1815 (diff)
Add :reload command
Diffstat (limited to 'nix-repl.cc')
-rw-r--r--nix-repl.cc41
1 files changed, 33 insertions, 8 deletions
diff --git a/nix-repl.cc b/nix-repl.cc
index a4d74055d5ff..c0ba3a588313 100644
--- a/nix-repl.cc
+++ b/nix-repl.cc
@@ -27,6 +27,8 @@ struct NixRepl
     string curDir;
     EvalState state;
 
+    Strings loadedFiles;
+
     StaticEnv staticEnv;
     Env * env;
     int displ;
@@ -41,6 +43,7 @@ struct NixRepl
     bool getLine(string & line);
     bool processLine(string line);
     void loadFile(const Path & path);
+    void reloadFiles();
     void addAttrsToScope(Value & attrs);
     void addVarToScope(const Symbol & name, Value & v);
     Expr * parseString(string s);
@@ -84,9 +87,11 @@ void NixRepl::mainLoop(const Strings & args)
 {
     std::cout << "Welcome to Nix version " << NIX_VERSION << ". Type :? for help." << std::endl << std::endl;
 
-    foreach (Strings::const_iterator, i, args) {
-        std::cout << format("Loading ‘%1%’...") % *i << std::endl;
-        loadFile(*i);
+    foreach (Strings::const_iterator, i, args)
+        loadedFiles.push_back(*i);
+
+    if (!loadedFiles.empty()) {
+        reloadFiles();
         std::cout << std::endl;
     }
 
@@ -110,7 +115,6 @@ void NixRepl::mainLoop(const Strings & args)
 
         std::cout << std::endl;
     }
-
 }
 
 
@@ -271,7 +275,7 @@ bool NixRepl::processLine(string line)
         arg = line;
     }
 
-    if (command == ":?") {
+    if (command == ":?" || command == ":help") {
         cout << "The following commands are available:\n"
              << "\n"
              << "  <expr>        Evaluate and print expression\n"
@@ -281,21 +285,27 @@ bool NixRepl::processLine(string line)
              << "  :l <path>     Load Nix expression and add it to scope\n"
              << "  :p <expr>     Evaluate and print expression recursively\n"
              << "  :q            Exit nix-repl\n"
+             << "  :r            Reload all files\n"
              << "  :s <expr>     Build dependencies of derivation, then start nix-shell\n"
              << "  :t <expr>     Describe result of evaluation\n";
     }
 
-    else if (command == ":a") {
+    else if (command == ":a" || command == ":add") {
         Value v;
         evalString(arg, v);
         addAttrsToScope(v);
     }
 
-    else if (command == ":l") {
+    else if (command == ":l" || command == ":load") {
         state.resetFileCache();
         loadFile(arg);
     }
 
+    else if (command == ":r" || command == ":reload") {
+        state.resetFileCache();
+        reloadFiles();
+    }
+
     else if (command == ":t") {
         Value v;
         evalString(arg, v);
@@ -326,7 +336,7 @@ bool NixRepl::processLine(string line)
             runProgram("nix-shell", Strings{drvPath});
     }
 
-    else if (command == ":p") {
+    else if (command == ":p" || command == ":print") {
         Value v;
         evalString(arg, v);
         printValue(std::cout, v, 1000000000) << std::endl;
@@ -363,6 +373,8 @@ bool NixRepl::processLine(string line)
 
 void NixRepl::loadFile(const Path & path)
 {
+    loadedFiles.remove(path);
+    loadedFiles.push_back(path);
     Value v, v2;
     state.evalFile(lookupFileArg(state, path), v);
     Bindings bindings;
@@ -371,6 +383,19 @@ void NixRepl::loadFile(const Path & path)
 }
 
 
+void NixRepl::reloadFiles()
+{
+    Strings old = loadedFiles;
+    loadedFiles.clear();
+
+    foreach (Strings::iterator, i, old) {
+        if (i != old.begin()) std::cout << std::endl;
+        std::cout << format("Loading ‘%1%’...") % *i << std::endl;
+        loadFile(*i);
+    }
+}
+
+
 void NixRepl::addAttrsToScope(Value & attrs)
 {
     state.forceAttrs(attrs);