2003-11-18 12:06:07 +00:00
|
|
|
#ifndef __NIXEXPR_H
|
|
|
|
#define __NIXEXPR_H
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2003-10-31 17:09:31 +00:00
|
|
|
#include <map>
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "types.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2003-10-30 16:11:24 +00:00
|
|
|
|
|
|
|
|
2006-07-19 15:36:15 +00:00
|
|
|
MakeError(EvalError, Error)
|
2009-05-15 12:35:23 +00:00
|
|
|
MakeError(ParseError, Error)
|
2006-07-19 15:36:15 +00:00
|
|
|
MakeError(AssertionError, EvalError)
|
2007-04-16 15:03:19 +00:00
|
|
|
MakeError(ThrownError, AssertionError)
|
2006-08-23 15:46:00 +00:00
|
|
|
MakeError(Abort, EvalError)
|
2006-07-19 15:36:15 +00:00
|
|
|
MakeError(TypeError, EvalError)
|
|
|
|
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
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
|
|
|
|
};
|
2004-10-26 22:54:26 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprList : Expr
|
|
|
|
{
|
|
|
|
std::vector<Expr *> elems;
|
|
|
|
ExprList() { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Formal
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
Expr * def;
|
|
|
|
Formal(const string & name, Expr * def) : name(name), def(def) { };
|
|
|
|
};
|
2004-08-04 10:59:20 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Formals
|
|
|
|
{
|
|
|
|
typedef std::list<Formal> Formals_;
|
|
|
|
Formals_ formals;
|
|
|
|
bool ellipsis;
|
|
|
|
};
|
2004-08-04 10:59:20 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
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
|
2004-04-05 22:27:41 +00:00
|
|
|
/* Show a position. */
|
|
|
|
string showPos(ATerm pos);
|
|
|
|
|
2010-04-08 11:41:19 +00:00
|
|
|
|
2003-10-30 16:11:24 +00:00
|
|
|
/* 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
|
|
|
|
{
|
2006-09-20 16:36:29 +00:00
|
|
|
virtual ~TermFun() { }
|
2003-10-30 16:11:24 +00:00
|
|
|
virtual ATerm operator () (ATerm e) = 0;
|
|
|
|
};
|
|
|
|
ATerm bottomupRewrite(TermFun & f, ATerm e);
|
|
|
|
|
2006-10-16 15:55:34 +00:00
|
|
|
|
2003-10-31 17:09:31 +00:00
|
|
|
/* Create an attribute set expression from an Attrs value. */
|
2003-11-03 20:30:40 +00:00
|
|
|
Expr makeAttrs(const ATermMap & attrs);
|
2003-10-31 17:09:31 +00:00
|
|
|
|
2006-10-16 15:55:34 +00:00
|
|
|
|
2004-02-03 14:45:34 +00:00
|
|
|
/* 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);
|
2010-04-12 18:30:11 +00:00
|
|
|
#endif
|
2006-10-16 15:55:34 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|
|
|
|
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2003-11-18 12:06:07 +00:00
|
|
|
#endif /* !__NIXEXPR_H */
|