about summary refs log tree commit diff
path: root/src/libexpr/nixexpr.hh
blob: ebdfd0a152cad2324ac4152443a86c79efea2a53 (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
192
193
194
#ifndef __NIXEXPR_H
#define __NIXEXPR_H

#include <map>

#include "types.hh"


namespace nix {


MakeError(EvalError, Error)
MakeError(ParseError, Error)
MakeError(AssertionError, EvalError)
MakeError(ThrownError, AssertionError)
MakeError(Abort, EvalError)
MakeError(TypeError, EvalError)


struct Pos
{
    string file;
    unsigned int line, column;
};


/* Abstract syntax of Nix expressions. */

struct Env;
struct Value;
struct EvalState;

struct Expr
{
    virtual void show(std::ostream & str) = 0;
    virtual void eval(EvalState & state, Env & env, Value & v)
    {
        throw Error("not implemented");
    }
};

std::ostream & operator << (std::ostream & str, Expr & e);

#define COMMON_METHODS \
    void show(std::ostream & str); \
    void eval(EvalState & state, Env & env, Value & v);

struct ExprInt : Expr
{
    int n;
    ExprInt(int n) : n(n) { };
    COMMON_METHODS
};

struct ExprString : Expr
{
    string s;
    ExprString(const string & s) : s(s) { };
    COMMON_METHODS
};

struct ExprPath : Expr
{
    string s;
    ExprPath(const string & s) : s(s) { };
    COMMON_METHODS
};

struct ExprVar : Expr
{
    string name;
    ExprVar(const string & name) : name(name) { };
    COMMON_METHODS
};

struct ExprSelect : Expr
{
    Expr * e;
    string name;
    ExprSelect(Expr * e, const string & name) : e(e), name(name) { };
    COMMON_METHODS
};

struct ExprAttrs : Expr
{
    bool recursive;
    typedef std::map<string, Expr *> Attrs;
    Attrs attrs;
    list<string> inherited;
    ExprAttrs() : recursive(false) { };
    COMMON_METHODS
};

struct ExprList : Expr
{
    std::vector<Expr *> elems;
    ExprList() { };
    COMMON_METHODS
};

struct Formal
{
    string name;
    Expr * def;
    Formal(const string & name, Expr * def) : name(name), def(def) { };
};

struct Formals
{
    typedef std::list<Formal> Formals_;
    Formals_ formals;
    bool ellipsis;
};

struct ExprLambda : Expr
{
    Pos pos;
    string arg;
    bool matchAttrs;
    Formals * formals;
    Expr * body;
    ExprLambda(const Pos & pos, const string & arg, bool matchAttrs, Formals * formals, Expr * body)
        : pos(pos), arg(arg), matchAttrs(matchAttrs), formals(formals), body(body) { };
    COMMON_METHODS
};

struct ExprWith : Expr
{
    Pos pos;
    Expr * attrs, * body;
    ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
    COMMON_METHODS
};

struct ExprIf : Expr
{
    Expr * cond, * then, * else_;
    ExprIf(Expr * cond, Expr * then, Expr * else_) : cond(cond), then(then), else_(else_) { };
    COMMON_METHODS
};

#define MakeBinOp(name, s) \
    struct Expr##name : Expr \
    { \
        Expr * e1, * e2; \
        Expr##name(Expr * e1, Expr * e2) : e1(e1), e2(e2) { }; \
        void show(std::ostream & str) \
        { \
            str << *e1 << " " s " " << *e2; \
        } \
        void eval(EvalState & state, Env & env, Value & v); \
    };

MakeBinOp(App, "")
MakeBinOp(OpEq, "==")
MakeBinOp(OpNEq, "!=")
MakeBinOp(OpAnd, "&&")
MakeBinOp(OpOr, "||")
MakeBinOp(OpImpl, "->")
MakeBinOp(OpUpdate, "//")
MakeBinOp(OpConcatStrings, "+")
MakeBinOp(OpConcatLists, "++")


#if 0
/* Show a position. */
string showPos(ATerm pos);


/* Generic bottomup traversal over ATerms.  The traversal first
   recursively descends into subterms, and then applies the given term
   function to the resulting term. */
struct TermFun
{
    virtual ~TermFun() { }
    virtual ATerm operator () (ATerm e) = 0;
};
ATerm bottomupRewrite(TermFun & f, ATerm e);


/* Create an attribute set expression from an Attrs value. */
Expr makeAttrs(const ATermMap & attrs);


/* Check whether all variables are defined in the given expression.
   Throw an exception if this isn't the case. */
void checkVarDefs(const ATermMap & def, Expr e);
#endif


}


#endif /* !__NIXEXPR_H */