about summary refs log tree commit diff
path: root/src/libexpr/eval-test.cc
blob: c4e205682a3abef0407f40cca9abc3558633e0f2 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "nixexpr.hh"
#include "parser.hh"
#include "hash.hh"
#include "util.hh"
#include "nixexpr-ast.hh"

#include <cstdlib>

using namespace nix;


typedef struct Env_ * Env;
typedef struct Value_ * Value;

typedef std::map<string, Value> Bindings;


struct Env_
{
    Env up;
    Bindings bindings;
};


typedef enum {
    tInt = 1,
    tAttrs,
    tThunk
} ValueType;


struct Value_
{
    ValueType type;
    union 
    {
        int integer;
        Bindings * attrs;
        struct {
            Env env;
            Expr expr;
        } thunk;
    };
};


std::ostream & operator << (std::ostream & str, Value_ & v)
{
    switch (v.type) {
    case tInt:
        str << v.integer;
        break;
    case tAttrs:
        str << "{ ";
        foreach (Bindings::iterator, i, *v.attrs) {
            str << i->first << " = " << *i->second << "; ";
        }
        str << "}";
        break;
    case tThunk:
        str << "<CODE>";
        break;
    default:
        abort();
    }
    return str;
}


Value eval(Env env, Expr e);


void forceValue(Value v)
{
    if (v->type != tThunk) return;
    Value v2 = eval(v->thunk.env, v->thunk.expr);
    *v = *v2; // !!! slightly inefficient
}


Value lookupVar(Env env, const string & name)
{
    for ( ; env; env = env->up) {
        Value v = env->bindings[name];
        if (v) return v;
    }
    throw Error("undefined variable");
}


Value eval(Env env, Expr e)
{
    printMsg(lvlError, format("eval: %1%") % e);

    ATerm name;
    if (matchVar(e, name)) {
        Value v = lookupVar(env, aterm2String(name));
        forceValue(v);
        return v;
    }

    int n;
    if (matchInt(e, n)) {
        Value v = new Value_;
        v->type = tInt;
        v->integer = n;
        return v;
    }

    ATermList es;
    if (matchAttrs(e, es)) {
        Value v = new Value_;
        v->type = tAttrs;
        v->attrs = new Bindings;
        ATerm e2, pos;
        for (ATermIterator i(es); i; ++i) {
            if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
            Value v2 = new Value_;
            v2->type = tThunk;
            v2->thunk.env = env;
            v2->thunk.expr = e2;
            (*v->attrs)[aterm2String(name)] = v2;
        }
        return v;
    }

    ATermList rbnds, nrbnds;
    if (matchRec(e, rbnds, nrbnds)) {
        Env env2 = new Env_;
        env2->up = env;
        
        Value v = new Value_;
        v->type = tAttrs;
        v->attrs = &env2->bindings;
        ATerm name, e2, pos;
        for (ATermIterator i(rbnds); i; ++i) {
            if (!matchBind(*i, name, e2, pos)) abort(); /* can't happen */
            Value v2 = new Value_;
            v2->type = tThunk;
            v2->thunk.env = env2;
            v2->thunk.expr = e2;
            env2->bindings[aterm2String(name)] = v2;
        }
        return v;
    }

    ATerm e2;
    if (matchSelect(e, e2, name)) {
        Value v = eval(env, e2);
        if (v->type != tAttrs) throw TypeError("expected attribute set");
        Value v2 = (*v->attrs)[aterm2String(name)];
        if (!v2) throw TypeError("attribute not found");
        forceValue(v2);
        return v2;
    }

    abort();
}


void doTest(string s)
{
    EvalState state;
    Expr e = parseExprFromString(state, s, "/");
    printMsg(lvlError, format("%1%") % e);
    Value v = eval(0, e);
    printMsg(lvlError, format("result: %1%") % *v);
}


void run(Strings args)
{
    printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value_));
    
    doTest("123");
    doTest("{ x = 1; y = 2; }");
    doTest("{ x = 1; y = 2; }.y");
    doTest("rec { x = 1; y = x; }.y");
    //Expr e = parseExprFromString(state, "let x = \"a\"; in x + \"b\"", "/");
    //Expr e = parseExprFromString(state, "(x: x + \"b\") \"a\"", "/");
    //Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/");
    //Expr e = parseExprFromString(state, "\"a\" + \"b\"", "/");
}


void printHelp()
{
}


string programId = "eval-test";