about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-02T13·18+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-02T13·18+0200
commite90569905ecdfe3225314f0e5f122263aa68efb2 (patch)
tree5c00fa4ae75870f8bab1b1815dfc7ee335ef6212
Initial version of nix-repl
This program interactively reads a Nix expression from the user,
evaluates it, and prints the result.
-rw-r--r--default.nix10
-rw-r--r--nix-repl.cc57
2 files changed, 67 insertions, 0 deletions
diff --git a/default.nix b/default.nix
new file mode 100644
index 000000000000..55c9ed9e3aff
--- /dev/null
+++ b/default.nix
@@ -0,0 +1,10 @@
+with import <nixpkgs> { };
+
+runCommand "nix-repl"
+  { buildInputs = [ readline nixUnstable boehmgc ]; }
+  ''
+    mkdir -p $out/bin
+    g++ -O3 -Wall -o $out/bin/nix-repl ${./nix-repl.cc} \
+      -I${nixUnstable}/include/nix -L${nixUnstable}/lib/nix \
+      -lexpr -lmain -lreadline
+  ''
diff --git a/nix-repl.cc b/nix-repl.cc
new file mode 100644
index 000000000000..ea248b5ca15f
--- /dev/null
+++ b/nix-repl.cc
@@ -0,0 +1,57 @@
+#include <iostream>
+#include <cstdlib>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include "shared.hh"
+#include "eval.hh"
+
+using namespace std;
+using namespace nix;
+
+
+string programId = "nix-repl";
+
+
+void printHelp()
+{
+    std::cout << "Usage: nix-repl\n";
+}
+
+
+bool getLine(string & line)
+{
+    char * s = readline ("nix-repl> ");
+    if (!s) return false;
+    line = chomp(string(s));
+    free(s);
+    if (line != "") add_history(line.c_str());
+    return true;
+}
+
+
+void run(nix::Strings args)
+{
+    EvalState state;
+    Path curDir = absPath(".");
+
+    while (true) {
+        string line;
+        if (!getLine(line)) break;
+
+        try {
+            Expr * e = state.parseExprFromString(line, curDir);
+            Value v;
+            state.eval(e, v);
+            state.strictForceValue(v);
+            std::cout << v << std::endl;
+        } catch (Error & e) {
+            printMsg(lvlError, e.msg());
+        }
+
+        std::cout << std::endl;
+    }
+
+    std::cout << std::endl;
+}