-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin command support for
nix repl
- Refactor the repl core into libexpr - Use dependency injection via std::function to provide the completion functions from editline so we don't introduce extra dependencies for libexpr - Add a RegisterReplCmd analogous to RegisterPrimOp for repl commands - Refactor: get rid of the "ugly" global curRepl and replace it with a trick with closures on the nix side (that are effectively globals, but unique per repl user and thus not as ugly ;p) - Rip out readline support since there appears to be no build system support for it and it is thus dead code - Integration test this new plugin functionality
- Loading branch information
Showing
12 changed files
with
844 additions
and
691 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "eval.hh" | ||
#include <vector> | ||
#include <functional> | ||
|
||
#if HAVE_BOEHMGC | ||
#define GC_INCLUDE_NEW | ||
#include <gc/gc_cpp.h> | ||
#endif | ||
|
||
namespace nix { | ||
|
||
struct NixRepl | ||
#if HAVE_BOEHMGC | ||
: gc | ||
#endif | ||
{ | ||
struct CompletionFunctions { | ||
std::function<int (const char * filename)> writeHistory; | ||
std::function<char * (const char * prompt)> readline; | ||
}; | ||
|
||
string curDir; | ||
std::unique_ptr<EvalState> state; | ||
Bindings * autoArgs; | ||
|
||
Strings loadedFiles; | ||
|
||
const static int envSize = 32768; | ||
StaticEnv staticEnv; | ||
Env * env; | ||
int displ; | ||
StringSet varNames; | ||
|
||
const Path historyFile; | ||
CompletionFunctions completionFunctions; | ||
|
||
NixRepl(const Strings & searchPath, nix::ref<Store> store, | ||
CompletionFunctions completionFunctions); | ||
~NixRepl(); | ||
void mainLoop(const std::vector<std::string> & files); | ||
StringSet completePrefix(string prefix); | ||
bool getLine(string & input, const std::string &prompt); | ||
Path getDerivationPath(Value & v); | ||
bool processLine(string line); | ||
void loadFile(const Path & path); | ||
void initEnv(); | ||
void reloadFiles(); | ||
void addAttrsToScope(Value & attrs); | ||
void addVarToScope(const Symbol & name, Value & v); | ||
Expr * parseString(string s); | ||
void evalString(string s, Value & v); | ||
|
||
static string removeWhitespace(string s); | ||
|
||
typedef set<Value *> ValuesSeen; | ||
std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth); | ||
std::ostream & printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen); | ||
}; | ||
|
||
using ReplCmdFun = std::function<void (string name, string arg)>; | ||
|
||
/** | ||
* A registry for extending the REPL commands list. | ||
*/ | ||
struct RegisterReplCmd | ||
{ | ||
struct ReplCmd | ||
{ | ||
/** | ||
* Names of the commands this matches, not prefixed by :. The first one | ||
* is displayed in help. | ||
*/ | ||
vector<string> names; | ||
/** | ||
* Argument placeholder, for example, "<expr>". | ||
*/ | ||
string argPlaceholder; | ||
/** | ||
* Help message displayed in :?. | ||
*/ | ||
string help; | ||
/** | ||
* Callback. | ||
*/ | ||
ReplCmdFun cmd; | ||
}; | ||
|
||
using ReplCmds = vector<ReplCmd>; | ||
|
||
static ReplCmds * replCmds; | ||
|
||
RegisterReplCmd( | ||
vector<string> names, | ||
string help, | ||
ReplCmdFun cmd, | ||
string argPlaceholder = "" | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
// from https://stackoverflow.com/a/48368508 | ||
|
||
#include <type_traits> | ||
#include <functional> | ||
|
||
template <class F> | ||
struct lambda_traits : lambda_traits<decltype(&F::operator())> | ||
{ }; | ||
|
||
template <typename F, typename R, typename... Args> | ||
struct lambda_traits<R(F::*)(Args...)> : lambda_traits<R(F::*)(Args...) const> | ||
{ }; | ||
|
||
template <class F, class R, class... Args> | ||
struct lambda_traits<R(F::*)(Args...) const> { | ||
using pointer = typename std::add_pointer<R(Args...)>::type; | ||
|
||
static pointer cify(F&& f) { | ||
static F fn = std::forward<F>(f); | ||
return [](Args... args) { | ||
return fn(std::forward<Args>(args)...); | ||
}; | ||
} | ||
}; | ||
|
||
template <class F> | ||
inline typename lambda_traits<F>::pointer cify(F&& f) { | ||
return lambda_traits<F>::cify(std::forward<F>(f)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.