2012-07-18 18:59:03 +00:00
|
|
|
#pragma once
|
2003-10-30 16:11:24 +00:00
|
|
|
|
2012-01-07 17:26:33 +00:00
|
|
|
#include "value.hh"
|
2010-04-13 12:25:42 +00:00
|
|
|
#include "symbol-table.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
2010-10-04 17:55:38 +00:00
|
|
|
#include <map>
|
|
|
|
|
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)
|
2016-02-24 10:32:18 +00:00
|
|
|
MakeError(IncompleteParseError, ParseError)
|
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)
|
2013-10-08 12:45:36 +00:00
|
|
|
MakeError(UndefinedVarError, Error)
|
2015-02-23 13:41:53 +00:00
|
|
|
MakeError(RestrictedPathError, Error)
|
2006-07-19 15:36:15 +00:00
|
|
|
|
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* Position objects. */
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct Pos
|
|
|
|
{
|
2013-10-08 13:19:59 +00:00
|
|
|
Symbol file;
|
2010-04-12 18:30:11 +00:00
|
|
|
unsigned int line, column;
|
2010-05-06 16:46:48 +00:00
|
|
|
Pos() : line(0), column(0) { };
|
2013-10-08 13:19:59 +00:00
|
|
|
Pos(const Symbol & file, unsigned int line, unsigned int column)
|
2010-05-06 16:46:48 +00:00
|
|
|
: file(file), line(line), column(column) { };
|
2014-04-04 19:14:11 +00:00
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return line != 0;
|
|
|
|
}
|
2012-08-13 03:29:28 +00:00
|
|
|
bool operator < (const Pos & p2) const
|
|
|
|
{
|
2013-10-08 13:19:59 +00:00
|
|
|
if (!line) return p2.line;
|
|
|
|
if (!p2.line) return false;
|
|
|
|
int d = ((string) file).compare((string) p2.file);
|
2012-08-13 03:29:28 +00:00
|
|
|
if (d < 0) return true;
|
|
|
|
if (d > 0) return false;
|
|
|
|
if (line < p2.line) return true;
|
|
|
|
if (line > p2.line) return false;
|
|
|
|
return column < p2.column;
|
|
|
|
}
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2010-05-06 16:46:48 +00:00
|
|
|
extern Pos noPos;
|
|
|
|
|
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;
|
2014-01-21 17:29:55 +00:00
|
|
|
class EvalState;
|
2010-04-14 14:42:32 +00:00
|
|
|
struct StaticEnv;
|
|
|
|
|
|
|
|
|
2011-07-06 10:58:17 +00:00
|
|
|
/* An attribute path is a sequence of attribute names. */
|
2013-12-31 23:56:26 +00:00
|
|
|
struct AttrName
|
|
|
|
{
|
|
|
|
Symbol symbol;
|
2014-10-04 23:04:58 +00:00
|
|
|
Expr * expr;
|
2013-12-31 23:56:26 +00:00
|
|
|
AttrName(const Symbol & s) : symbol(s) {};
|
2014-10-04 23:04:58 +00:00
|
|
|
AttrName(Expr * e) : expr(e) {};
|
2013-12-31 23:56:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<AttrName> AttrPath;
|
2011-07-06 10:58:17 +00:00
|
|
|
|
|
|
|
string showAttrPath(const AttrPath & attrPath);
|
|
|
|
|
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* Abstract syntax of Nix expressions. */
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
struct Expr
|
|
|
|
{
|
2014-01-21 17:29:55 +00:00
|
|
|
virtual ~Expr() { };
|
2018-03-14 18:20:32 +00:00
|
|
|
virtual void show(std::ostream & str) const;
|
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);
|
2012-01-04 21:24:11 +00:00
|
|
|
virtual Value * maybeThunk(EvalState & state, Env & env);
|
2013-05-16 17:08:02 +00:00
|
|
|
virtual void setName(Symbol & name);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2018-03-14 18:20:32 +00:00
|
|
|
std::ostream & operator << (std::ostream & str, const Expr & e);
|
2010-04-12 18:30:11 +00:00
|
|
|
|
|
|
|
#define COMMON_METHODS \
|
2018-03-14 18:20:32 +00:00
|
|
|
void show(std::ostream & str) const; \
|
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
|
|
|
|
{
|
2013-08-19 10:35:03 +00:00
|
|
|
NixInt n;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
2013-08-19 10:35:03 +00:00
|
|
|
ExprInt(NixInt n) : n(n) { mkInt(v, n); };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
2012-01-07 17:26:33 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2016-01-04 23:40:40 +00:00
|
|
|
struct ExprFloat : Expr
|
|
|
|
{
|
|
|
|
NixFloat nf;
|
|
|
|
Value v;
|
|
|
|
ExprFloat(NixFloat nf) : nf(nf) { mkFloat(v, nf); };
|
|
|
|
COMMON_METHODS
|
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprString : Expr
|
|
|
|
{
|
2010-10-23 21:11:59 +00:00
|
|
|
Symbol s;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
|
|
|
ExprString(const Symbol & s) : s(s) { mkString(v, s); };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
2012-01-07 17:26:33 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
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;
|
2012-01-07 17:26:33 +00:00
|
|
|
Value v;
|
|
|
|
ExprPath(const string & s) : s(s) { mkPathNoCopy(v, this->s.c_str()); };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
2012-01-07 17:26:33 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
2013-10-08 12:24:53 +00:00
|
|
|
struct ExprVar : Expr
|
2010-04-12 18:30:11 +00:00
|
|
|
{
|
2013-10-08 12:40:51 +00:00
|
|
|
Pos pos;
|
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;
|
2013-09-02 14:29:15 +00:00
|
|
|
|
2010-04-14 14:42:32 +00:00
|
|
|
/* 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
|
2013-10-24 14:41:04 +00:00
|
|
|
the set stored in the environment that is `level' levels up
|
|
|
|
from the current one.*/
|
2010-04-14 14:42:32 +00:00
|
|
|
unsigned int level;
|
|
|
|
unsigned int displ;
|
2010-04-14 15:14:23 +00:00
|
|
|
|
2014-05-26 15:02:22 +00:00
|
|
|
ExprVar(const Symbol & name) : name(name) { };
|
2013-10-08 12:40:51 +00:00
|
|
|
ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
2012-01-04 21:24:11 +00:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2010-04-12 18:30:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprSelect : Expr
|
|
|
|
{
|
2014-04-04 20:52:14 +00:00
|
|
|
Pos pos;
|
2011-07-13 12:19:57 +00:00
|
|
|
Expr * e, * def;
|
2011-07-06 12:28:57 +00:00
|
|
|
AttrPath attrPath;
|
2014-04-04 20:52:14 +00:00
|
|
|
ExprSelect(const Pos & pos, Expr * e, const AttrPath & attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(attrPath) { };
|
|
|
|
ExprSelect(const Pos & pos, Expr * e, const Symbol & name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); };
|
2010-04-12 18:30:11 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprOpHasAttr : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
2011-07-06 10:58:17 +00:00
|
|
|
AttrPath attrPath;
|
|
|
|
ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { };
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 18:30:11 +00:00
|
|
|
struct ExprAttrs : Expr
|
|
|
|
{
|
|
|
|
bool recursive;
|
2010-10-24 19:52:33 +00:00
|
|
|
struct AttrDef {
|
|
|
|
bool inherited;
|
2013-07-15 21:10:18 +00:00
|
|
|
Expr * e;
|
2010-10-24 19:52:33 +00:00
|
|
|
Pos pos;
|
|
|
|
unsigned int displ; // displacement
|
2014-10-04 23:04:58 +00:00
|
|
|
AttrDef(Expr * e, const Pos & pos, bool inherited=false)
|
|
|
|
: inherited(inherited), e(e), pos(pos) { };
|
2010-10-24 19:52:33 +00:00
|
|
|
AttrDef() { };
|
|
|
|
};
|
|
|
|
typedef std::map<Symbol, AttrDef> AttrDefs;
|
|
|
|
AttrDefs attrs;
|
Dynamic attrs
This adds new syntax for attribute names:
* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]
Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.
For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.
Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.
Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.
This is an alternative solution to and closes #167
Signed-off-by: Shea Levy <shea@shealevy.com>
2013-09-21 03:25:30 +00:00
|
|
|
struct DynamicAttrDef {
|
2014-10-04 23:04:58 +00:00
|
|
|
Expr * nameExpr, * valueExpr;
|
Dynamic attrs
This adds new syntax for attribute names:
* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]
Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.
For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.
Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.
Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.
This is an alternative solution to and closes #167
Signed-off-by: Shea Levy <shea@shealevy.com>
2013-09-21 03:25:30 +00:00
|
|
|
Pos pos;
|
2014-10-04 23:04:58 +00:00
|
|
|
DynamicAttrDef(Expr * nameExpr, Expr * valueExpr, const Pos & pos)
|
|
|
|
: nameExpr(nameExpr), valueExpr(valueExpr), pos(pos) { };
|
Dynamic attrs
This adds new syntax for attribute names:
* attrs."${name}" => getAttr name attrs
* attrs ? "${name}" => isAttrs attrs && hasAttr attrs name
* attrs."${name}" or def => if attrs ? "${name}" then attrs."${name}" else def
* { "${name}" = value; } => listToAttrs [{ inherit name value; }]
Of course, it's a bit more complicated than that. The attribute chains
can be arbitrarily long and contain combinations of static and dynamic
parts (e.g. attrs."${foo}".bar."${baz}" or qux), which is relatively
straightforward for the getAttrs/hasAttrs cases but is more complex for
the listToAttrs case due to rules about duplicate attribute definitions.
For attribute sets with dynamic attribute names, duplicate static
attributes are detected at parse time while duplicate dynamic attributes
are detected when the attribute set is forced. So, for example, { a =
null; a.b = null; "${"c"}" = true; } will be a parse-time error, while
{ a = {}; "${"a"}".b = null; c = true; } will be an eval-time error
(technically that case could theoretically be detected at parse time,
but the general case would require full evaluation). Moreover, duplicate
dynamic attributes are not allowed even in cases where they would be
with static attributes ({ a.b.d = true; a.b.c = false; } is legal, but {
a."${"b"}".d = true; a."${"b"}".c = false; } is not). This restriction
might be relaxed in the future in cases where the static variant would
not be an error, but it is not obvious that that is desirable.
Finally, recursive attribute sets with dynamic attributes have the
static attributes in scope but not the dynamic ones. So rec { a = true;
"${"b"}" = a; } is equivalent to { a = true; b = true; } but rec {
"${"a"}" = true; b = a; } would be an error or use a from the
surrounding scope if it exists.
Note that the getAttr, getAttr or default, and hasAttr are all
implemented purely in the parser as syntactic sugar, while attribute
sets with dynamic attribute names required changes to the AST to be
implemented cleanly.
This is an alternative solution to and closes #167
Signed-off-by: Shea Levy <shea@shealevy.com>
2013-09-21 03:25:30 +00:00
|
|
|
};
|
|
|
|
typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
|
|
|
|
DynamicAttrDefs dynamicAttrs;
|
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;
|
2013-05-16 17:08:02 +00:00
|
|
|
Symbol name;
|
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())
|
2017-07-30 11:27:57 +00:00
|
|
|
throw ParseError(format("duplicate formal function argument '%1%' at %2%")
|
2010-04-22 11:02:24 +00:00
|
|
|
% arg % pos);
|
|
|
|
};
|
2013-05-16 17:08:02 +00:00
|
|
|
void setName(Symbol & name);
|
2013-11-07 17:04:36 +00:00
|
|
|
string showNamePos() const;
|
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;
|
2018-05-02 11:56:34 +00:00
|
|
|
size_t 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) \
|
2018-03-09 03:16:33 +00:00
|
|
|
struct name : Expr \
|
2010-04-12 18:30:11 +00:00
|
|
|
{ \
|
2014-04-04 20:43:52 +00:00
|
|
|
Pos pos; \
|
2010-04-12 18:30:11 +00:00
|
|
|
Expr * e1, * e2; \
|
2018-03-09 03:16:33 +00:00
|
|
|
name(Expr * e1, Expr * e2) : e1(e1), e2(e2) { }; \
|
|
|
|
name(const Pos & pos, Expr * e1, Expr * e2) : pos(pos), e1(e1), e2(e2) { }; \
|
2018-03-14 18:20:32 +00:00
|
|
|
void show(std::ostream & str) const \
|
2010-04-12 18:30:11 +00:00
|
|
|
{ \
|
2014-10-20 06:44:32 +00:00
|
|
|
str << "(" << *e1 << " " s " " << *e2 << ")"; \
|
2010-04-12 18:30:11 +00:00
|
|
|
} \
|
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); \
|
|
|
|
};
|
|
|
|
|
2018-03-09 03:16:33 +00:00
|
|
|
MakeBinOp(ExprApp, "")
|
|
|
|
MakeBinOp(ExprOpEq, "==")
|
|
|
|
MakeBinOp(ExprOpNEq, "!=")
|
|
|
|
MakeBinOp(ExprOpAnd, "&&")
|
|
|
|
MakeBinOp(ExprOpOr, "||")
|
|
|
|
MakeBinOp(ExprOpImpl, "->")
|
|
|
|
MakeBinOp(ExprOpUpdate, "//")
|
|
|
|
MakeBinOp(ExprOpConcatLists, "++")
|
2010-04-12 18:30:11 +00:00
|
|
|
|
2010-04-12 21:21:24 +00:00
|
|
|
struct ExprConcatStrings : Expr
|
|
|
|
{
|
2014-04-04 20:19:33 +00:00
|
|
|
Pos pos;
|
2013-03-08 00:24:59 +00:00
|
|
|
bool forceString;
|
2010-04-12 21:21:24 +00:00
|
|
|
vector<Expr *> * es;
|
2014-04-04 20:19:33 +00:00
|
|
|
ExprConcatStrings(const Pos & pos, bool forceString, vector<Expr *> * es)
|
|
|
|
: pos(pos), forceString(forceString), es(es) { };
|
2010-04-12 21:21:24 +00:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
2004-04-05 22:27:41 +00:00
|
|
|
|
2013-11-18 19:14:54 +00:00
|
|
|
struct ExprPos : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
ExprPos(const Pos & pos) : pos(pos) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|