From 0f6279d87421f19cd2c1e286163d7567f13dc77f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 2 Sep 2013 18:18:27 +0200 Subject: [PATCH] Add a command :l for loading a file into scope Example: nix-repl> :l nix-repl> lib.range 0 10 [ 0 1 2 3 4 5 6 7 8 9 10 ] nix-repl> :l nix-repl> config.boot.kernelModules [ "vboxdrv" "vboxnetadp" ... ] --- nix-repl.cc | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/nix-repl.cc b/nix-repl.cc index 9858a034d..0b8483449 100644 --- a/nix-repl.cc +++ b/nix-repl.cc @@ -8,6 +8,7 @@ #include "eval.hh" #include "eval-inline.hh" #include "store-api.hh" +#include "common-opts.hh" using namespace std; using namespace nix; @@ -28,7 +29,8 @@ struct NixRepl NixRepl(); void mainLoop(); void processLine(string line); - void addVar(const Symbol & name, Value * v); + void addAttrsToScope(Value & attrs); + void addVarToScope(const Symbol & name, Value * v); Expr * parseString(string s); void evalString(string s, Value & v); }; @@ -51,6 +53,15 @@ bool getLine(string & line) } +string removeWhitespace(string s) +{ + s = chomp(s); + size_t n = s.find_first_not_of(" \n\r\t"); + if (n != string::npos) s = string(s, n); + return s; +} + + NixRepl::NixRepl() : staticEnv(false, &state.staticBaseEnv) { @@ -72,12 +83,8 @@ void NixRepl::mainLoop() string line; if (!getLine(line)) break; - /* Remove preceeding whitespace. */ - size_t n = line.find_first_not_of(" \n\r\t"); - if (n != string::npos) line = string(line, n); - try { - processLine(line); + processLine(removeWhitespace(line)); } catch (Error & e) { printMsg(lvlError, e.msg()); } @@ -94,9 +101,17 @@ void NixRepl::processLine(string line) if (string(line, 0, 2) == ":a") { Value v; evalString(string(line, 2), v); - state.forceAttrs(v); - foreach (Bindings::iterator, i, *v.attrs) - addVar(i->name, i->value); + addAttrsToScope(v); + } + + else if (string(line, 0, 2) == ":l") { + state.resetFileCache(); + Path path = lookupFileArg(state, removeWhitespace(string(line, 2))); + Value v, v2; + state.evalFile(path, v); + Bindings bindings; + state.autoCallFunction(bindings, v, v2); + addAttrsToScope(v2); } else if (string(line, 0, 2) == ":t") { @@ -118,7 +133,15 @@ void NixRepl::processLine(string line) } -void NixRepl::addVar(const Symbol & name, Value * v) +void NixRepl::addAttrsToScope(Value & attrs) +{ + state.forceAttrs(attrs); + foreach (Bindings::iterator, i, *attrs.attrs) + addVarToScope(i->name, i->value); +} + + +void NixRepl::addVarToScope(const Symbol & name, Value * v) { staticEnv.vars[name] = displ; env->values[displ++] = v;