about summary refs log tree commit diff
path: root/nix-repl.cc
blob: ea248b5ca15f74957270471edbc77e8833a82a4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}