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>
|
|
|
|
|
2010-04-13 12:25:42 +00:00
|
|
|
#include "symbol-table.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-14 14:42:32 +00:00
|
|
|
/* Position objects. */
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Pos
|
|
|
|
{
|
|
|
|
string file;
|
|
|
|
unsigned int line, column;
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
std::ostream & operator << (std::ostream & str, const Pos & pos);
|
|
|
|
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Env;
|
|
|
|
struct Value;
|
|
|
|
struct EvalState;
|
2010-04-14 14:42:32 +00:00
|
|
|
struct StaticEnv;
|
|
|
|
|
|
|
|
|
|
|
|
/* Abstract syntax of Nix expressions. */
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
struct Expr
|
|
|
|
{
|
2010-04-12 22:03:27 +00:00
|
|
|
virtual void show(std::ostream & str);
|
2010-04-14 14:42:32 +00:00
|
|
|
virtual void bindVars(const StaticEnv & env);
|
2010-04-12 22:03:27 +00:00
|
|
|
virtual void eval(EvalState & state, Env & env, Value & v);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream & operator << (std::ostream & str, Expr & e);
|
|
|
|
|
|
|
|
#define COMMON_METHODS \
|
|
|
|
void show(std::ostream & str); \
|
2010-04-14 14:42:32 +00:00
|
|
|
void eval(EvalState & state, Env & env, Value & v); \
|
|
|
|
void bindVars(const StaticEnv & env);
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2010-04-12 22:03:27 +00:00
|
|
|
/* Temporary class used during parsing of indented strings. */
|
|
|
|
struct ExprIndStr : Expr
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
ExprIndStr(const string & s) : s(s) { };
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprPath : Expr
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
ExprPath(const string & s) : s(s) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-14 15:14:23 +00:00
|
|
|
struct VarRef
|
2010-04-12 18:30:11 +00:00
|
|
|
{
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
2010-04-14 14:42:32 +00:00
|
|
|
|
|
|
|
/* Whether the variable comes from an environment (e.g. a rec, let
|
|
|
|
or function argument) or from a "with". */
|
|
|
|
bool fromWith;
|
|
|
|
|
|
|
|
/* In the former case, the value is obtained by going `level'
|
|
|
|
levels up from the current environment and getting the
|
|
|
|
`displ'th value in that environment. In the latter case, the
|
|
|
|
value is obtained by getting the attribute named `name' from
|
|
|
|
the attribute set stored in the environment that is `level'
|
|
|
|
levels up from the current one.*/
|
|
|
|
unsigned int level;
|
|
|
|
unsigned int displ;
|
2010-04-14 15:14:23 +00:00
|
|
|
|
|
|
|
VarRef(const Symbol & name) : name(name) { };
|
|
|
|
void bind(const StaticEnv & env);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprVar : Expr
|
|
|
|
{
|
|
|
|
VarRef info;
|
|
|
|
ExprVar(const Symbol & name) : info(name) { };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprSelect : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
|
|
|
ExprSelect(Expr * e, const Symbol & name) : e(e), name(name) { };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprOpHasAttr : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
|
|
|
ExprOpHasAttr(Expr * e, const Symbol & name) : e(e), name(name) { };
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprAttrs : Expr
|
|
|
|
{
|
|
|
|
bool recursive;
|
2010-04-13 12:25:42 +00:00
|
|
|
typedef std::map<Symbol, Expr *> Attrs;
|
2010-04-12 18:30:11 +00:00
|
|
|
Attrs attrs;
|
2010-04-14 23:25:05 +00:00
|
|
|
list<VarRef> inherited;
|
2010-04-22 11:02:24 +00:00
|
|
|
set<Symbol> attrNames; // used during parsing
|
2010-04-12 18:30:11 +00:00
|
|
|
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
|
|
|
|
{
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol name;
|
2010-04-12 18:30:11 +00:00
|
|
|
Expr * def;
|
2010-04-13 12:25:42 +00:00
|
|
|
Formal(const Symbol & name, Expr * def) : name(name), def(def) { };
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
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;
|
2010-04-22 11:02:24 +00:00
|
|
|
std::set<Symbol> argNames; // used during parsing
|
2010-04-12 18:30:11 +00:00
|
|
|
bool ellipsis;
|
|
|
|
};
|
2004-08-04 10:59:20 +00:00
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprLambda : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
2010-04-13 12:25:42 +00:00
|
|
|
Symbol arg;
|
2010-04-12 18:30:11 +00:00
|
|
|
bool matchAttrs;
|
|
|
|
Formals * formals;
|
|
|
|
Expr * body;
|
2010-04-13 12:25:42 +00:00
|
|
|
ExprLambda(const Pos & pos, const Symbol & arg, bool matchAttrs, Formals * formals, Expr * body)
|
2010-04-22 11:02:24 +00:00
|
|
|
: pos(pos), arg(arg), matchAttrs(matchAttrs), formals(formals), body(body)
|
|
|
|
{
|
|
|
|
if (!arg.empty() && formals && formals->argNames.find(arg) != formals->argNames.end())
|
|
|
|
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
|
|
|
|
% arg % pos);
|
|
|
|
};
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-13 13:42:25 +00:00
|
|
|
struct ExprLet : Expr
|
|
|
|
{
|
|
|
|
ExprAttrs * attrs;
|
|
|
|
Expr * body;
|
|
|
|
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprWith : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
Expr * attrs, * body;
|
2010-04-14 15:01:04 +00:00
|
|
|
int prevWith;
|
2010-04-12 18:30:11 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprAssert : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
Expr * cond, * body;
|
|
|
|
ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprOpNot : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
|
|
|
ExprOpNot(Expr * e) : e(e) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
#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; \
|
|
|
|
} \
|
2010-04-14 14:42:32 +00:00
|
|
|
void bindVars(const StaticEnv & env) \
|
|
|
|
{ \
|
|
|
|
e1->bindVars(env); e2->bindVars(env); \
|
|
|
|
} \
|
2010-04-12 18:30:11 +00:00
|
|
|
void eval(EvalState & state, Env & env, Value & v); \
|
|
|
|
};
|
|
|
|
|
|
|
|
MakeBinOp(App, "")
|
|
|
|
MakeBinOp(OpEq, "==")
|
|
|
|
MakeBinOp(OpNEq, "!=")
|
|
|
|
MakeBinOp(OpAnd, "&&")
|
|
|
|
MakeBinOp(OpOr, "||")
|
|
|
|
MakeBinOp(OpImpl, "->")
|
|
|
|
MakeBinOp(OpUpdate, "//")
|
|
|
|
MakeBinOp(OpConcatLists, "++")
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprConcatStrings : Expr
|
|
|
|
{
|
|
|
|
vector<Expr *> * es;
|
|
|
|
ExprConcatStrings(vector<Expr *> * es) : es(es) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
2004-04-05 22:27:41 +00:00
|
|
|
|
2010-04-08 11:41:19 +00:00
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* Static environments are used to map variable names onto (level,
|
|
|
|
displacement) pairs used to obtain the value of the variable at
|
|
|
|
runtime. */
|
|
|
|
struct StaticEnv
|
|
|
|
{
|
|
|
|
bool isWith;
|
|
|
|
const StaticEnv * up;
|
|
|
|
typedef std::map<Symbol, unsigned int> Vars;
|
|
|
|
Vars vars;
|
|
|
|
StaticEnv(bool isWith, const StaticEnv * up) : isWith(isWith), up(up) { };
|
|
|
|
};
|
|
|
|
|
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 */
|