mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-11 00:36:20 +02:00
Initial version of nix-repl
This program interactively reads a Nix expression from the user, evaluates it, and prints the result.
This commit is contained in:
commit
e90569905e
2 changed files with 67 additions and 0 deletions
10
default.nix
Normal file
10
default.nix
Normal file
|
@ -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
|
||||||
|
''
|
57
nix-repl.cc
Normal file
57
nix-repl.cc
Normal file
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in a new issue