2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2012-01-07 19:26:33 +02:00
|
|
|
#include "value.hh"
|
2010-04-13 15:25:42 +03:00
|
|
|
#include "symbol-table.hh"
|
2020-05-12 00:52:15 +03:00
|
|
|
#include "error.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2003-10-30 18:11:24 +02:00
|
|
|
|
|
|
|
|
2019-11-10 18:14:26 +02:00
|
|
|
MakeError(EvalError, Error);
|
|
|
|
MakeError(ParseError, Error);
|
|
|
|
MakeError(AssertionError, EvalError);
|
|
|
|
MakeError(ThrownError, AssertionError);
|
|
|
|
MakeError(Abort, EvalError);
|
|
|
|
MakeError(TypeError, EvalError);
|
|
|
|
MakeError(UndefinedVarError, Error);
|
2021-02-22 17:13:09 +02:00
|
|
|
MakeError(MissingArgumentError, EvalError);
|
2019-11-10 18:14:26 +02:00
|
|
|
MakeError(RestrictedPathError, Error);
|
2006-07-19 18:36:15 +03:00
|
|
|
|
2022-01-08 20:03:48 +02:00
|
|
|
extern std::function<void(const Error * error, const Env & env, const Expr & expr)> debuggerHook;
|
2006-07-19 18:36:15 +03:00
|
|
|
|
2010-04-14 17:42:32 +03:00
|
|
|
/* Position objects. */
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct Pos
|
|
|
|
{
|
2013-10-08 16:19:59 +03:00
|
|
|
Symbol file;
|
2022-01-01 20:24:20 +02:00
|
|
|
uint32_t line;
|
|
|
|
FileOrigin origin:2;
|
|
|
|
uint32_t column:30;
|
|
|
|
Pos() : line(0), origin(foString), column(0) { };
|
|
|
|
Pos(FileOrigin origin, const Symbol & file, uint32_t line, uint32_t column)
|
|
|
|
: file(file), line(line), origin(origin), column(column) { };
|
2014-04-04 22:14:11 +03:00
|
|
|
operator bool() const
|
|
|
|
{
|
|
|
|
return line != 0;
|
|
|
|
}
|
2022-02-25 17:00:00 +02:00
|
|
|
|
2012-08-13 06:29:28 +03:00
|
|
|
bool operator < (const Pos & p2) const
|
|
|
|
{
|
2013-10-08 16:19:59 +03:00
|
|
|
if (!line) return p2.line;
|
|
|
|
if (!p2.line) return false;
|
2022-02-25 17:00:00 +02:00
|
|
|
int d = ((const std::string &) file).compare((const std::string &) p2.file);
|
2012-08-13 06:29:28 +03: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 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2010-05-06 19:46:48 +03:00
|
|
|
extern Pos noPos;
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
std::ostream & operator << (std::ostream & str, const Pos & pos);
|
|
|
|
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct Env;
|
|
|
|
struct Value;
|
2014-01-21 19:29:55 +02:00
|
|
|
class EvalState;
|
2010-04-14 17:42:32 +03:00
|
|
|
struct StaticEnv;
|
|
|
|
|
|
|
|
|
2011-07-06 13:58:17 +03:00
|
|
|
/* An attribute path is a sequence of attribute names. */
|
2014-01-01 01:56:26 +02:00
|
|
|
struct AttrName
|
|
|
|
{
|
|
|
|
Symbol symbol;
|
2014-10-05 02:04:58 +03:00
|
|
|
Expr * expr;
|
2014-01-01 01:56:26 +02:00
|
|
|
AttrName(const Symbol & s) : symbol(s) {};
|
2014-10-05 02:04:58 +03:00
|
|
|
AttrName(Expr * e) : expr(e) {};
|
2014-01-01 01:56:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<AttrName> AttrPath;
|
2011-07-06 13:58:17 +03:00
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string showAttrPath(const AttrPath & attrPath);
|
2011-07-06 13:58:17 +03:00
|
|
|
|
|
|
|
|
2010-04-14 17:42:32 +03:00
|
|
|
/* Abstract syntax of Nix expressions. */
|
2010-04-12 21:30:11 +03:00
|
|
|
|
|
|
|
struct Expr
|
|
|
|
{
|
2014-01-21 19:29:55 +02:00
|
|
|
virtual ~Expr() { };
|
2018-03-14 20:20:32 +02:00
|
|
|
virtual void show(std::ostream & str) const;
|
2021-09-14 19:49:22 +03:00
|
|
|
virtual void bindVars(const std::shared_ptr<const StaticEnv> & env);
|
2010-04-13 01:03:27 +03:00
|
|
|
virtual void eval(EvalState & state, Env & env, Value & v);
|
2012-01-04 23:24:11 +02:00
|
|
|
virtual Value * maybeThunk(EvalState & state, Env & env);
|
2013-05-16 20:08:02 +03:00
|
|
|
virtual void setName(Symbol & name);
|
2021-09-14 19:49:22 +03:00
|
|
|
|
2021-10-02 22:47:36 +03:00
|
|
|
std::shared_ptr<const StaticEnv> staticenv;
|
2022-03-26 02:15:31 +02:00
|
|
|
virtual const Pos* getPos() const = 0;
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2018-03-14 20:20:32 +02:00
|
|
|
std::ostream & operator << (std::ostream & str, const Expr & e);
|
2010-04-12 21:30:11 +03:00
|
|
|
|
|
|
|
#define COMMON_METHODS \
|
2018-03-14 20:20:32 +02:00
|
|
|
void show(std::ostream & str) const; \
|
2010-04-14 17:42:32 +03:00
|
|
|
void eval(EvalState & state, Env & env, Value & v); \
|
2021-09-14 19:49:22 +03:00
|
|
|
void bindVars(const std::shared_ptr<const StaticEnv> & env);
|
2010-04-12 21:30:11 +03:00
|
|
|
|
|
|
|
struct ExprInt : Expr
|
|
|
|
{
|
2013-08-19 13:35:03 +03:00
|
|
|
NixInt n;
|
2012-01-07 19:26:33 +02:00
|
|
|
Value v;
|
2022-01-04 19:40:39 +02:00
|
|
|
ExprInt(NixInt n) : n(n) { v.mkInt(n); };
|
2012-01-07 19:26:33 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2016-01-05 01:40:40 +02:00
|
|
|
struct ExprFloat : Expr
|
|
|
|
{
|
|
|
|
NixFloat nf;
|
|
|
|
Value v;
|
2022-01-04 19:40:39 +02:00
|
|
|
ExprFloat(NixFloat nf) : nf(nf) { v.mkFloat(nf); };
|
2016-01-05 01:40:40 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2016-01-05 01:40:40 +02:00
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprString : Expr
|
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2012-01-07 19:26:33 +02:00
|
|
|
Value v;
|
2022-01-19 15:31:30 +02:00
|
|
|
ExprString(std::string s) : s(std::move(s)) { v.mkString(this->s.data()); };
|
2012-01-07 19:26:33 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprPath : Expr
|
|
|
|
{
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2012-01-07 19:26:33 +02:00
|
|
|
Value v;
|
2022-02-25 17:00:00 +02:00
|
|
|
ExprPath(std::string s) : s(std::move(s)) { v.mkPath(this->s.c_str()); };
|
2012-01-07 19:26:33 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2020-02-24 15:33:01 +02:00
|
|
|
typedef uint32_t Level;
|
|
|
|
typedef uint32_t Displacement;
|
|
|
|
|
2013-10-08 15:24:53 +03:00
|
|
|
struct ExprVar : Expr
|
2010-04-12 21:30:11 +03:00
|
|
|
{
|
2013-10-08 15:40:51 +03:00
|
|
|
Pos pos;
|
2010-04-13 15:25:42 +03:00
|
|
|
Symbol name;
|
2010-04-14 17:42:32 +03: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 17:29:15 +03:00
|
|
|
|
2010-04-14 17:42:32 +03: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 17:41:04 +03:00
|
|
|
the set stored in the environment that is `level' levels up
|
|
|
|
from the current one.*/
|
2020-02-24 15:33:01 +02:00
|
|
|
Level level;
|
|
|
|
Displacement displ;
|
2010-04-14 18:14:23 +03:00
|
|
|
|
2014-05-26 18:02:22 +03:00
|
|
|
ExprVar(const Symbol & name) : name(name) { };
|
2013-10-08 15:40:51 +03:00
|
|
|
ExprVar(const Pos & pos, const Symbol & name) : pos(pos), name(name) { };
|
2012-01-04 23:24:11 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env);
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprSelect : Expr
|
|
|
|
{
|
2014-04-04 23:52:14 +03:00
|
|
|
Pos pos;
|
2011-07-13 15:19:57 +03:00
|
|
|
Expr * e, * def;
|
2011-07-06 15:28:57 +03:00
|
|
|
AttrPath attrPath;
|
2014-04-04 23:52:14 +03: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)); };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
struct ExprOpHasAttr : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
2011-07-06 13:58:17 +03:00
|
|
|
AttrPath attrPath;
|
|
|
|
ExprOpHasAttr(Expr * e, const AttrPath & attrPath) : e(e), attrPath(attrPath) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return e->getPos(); }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-13 00:21:24 +03:00
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprAttrs : Expr
|
|
|
|
{
|
|
|
|
bool recursive;
|
2021-01-08 23:27:00 +02:00
|
|
|
Pos pos;
|
2010-10-24 22:52:33 +03:00
|
|
|
struct AttrDef {
|
|
|
|
bool inherited;
|
2013-07-16 00:10:18 +03:00
|
|
|
Expr * e;
|
2010-10-24 22:52:33 +03:00
|
|
|
Pos pos;
|
2020-02-24 15:33:01 +02:00
|
|
|
Displacement displ; // displacement
|
2014-10-05 02:04:58 +03:00
|
|
|
AttrDef(Expr * e, const Pos & pos, bool inherited=false)
|
|
|
|
: inherited(inherited), e(e), pos(pos) { };
|
2010-10-24 22:52:33 +03: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 06:25:30 +03:00
|
|
|
struct DynamicAttrDef {
|
2014-10-05 02:04:58 +03: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 06:25:30 +03:00
|
|
|
Pos pos;
|
2014-10-05 02:04:58 +03: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 06:25:30 +03:00
|
|
|
};
|
|
|
|
typedef std::vector<DynamicAttrDef> DynamicAttrDefs;
|
|
|
|
DynamicAttrDefs dynamicAttrs;
|
2021-01-08 23:27:00 +02:00
|
|
|
ExprAttrs(const Pos &pos) : recursive(false), pos(pos) { };
|
|
|
|
ExprAttrs() : recursive(false), pos(noPos) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
2004-10-27 01:54:26 +03:00
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprList : Expr
|
|
|
|
{
|
|
|
|
std::vector<Expr *> elems;
|
|
|
|
ExprList() { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct Formal
|
|
|
|
{
|
2020-04-02 06:03:58 +03:00
|
|
|
Pos pos;
|
2010-04-13 15:25:42 +03:00
|
|
|
Symbol name;
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * def;
|
2020-04-02 06:03:58 +03:00
|
|
|
Formal(const Pos & pos, const Symbol & name, Expr * def) : pos(pos), name(name), def(def) { };
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
2004-08-04 13:59:20 +03:00
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct Formals
|
|
|
|
{
|
2022-01-19 17:49:02 +02:00
|
|
|
typedef std::vector<Formal> Formals_;
|
2010-04-12 21:30:11 +03:00
|
|
|
Formals_ formals;
|
|
|
|
bool ellipsis;
|
2022-01-19 17:49:02 +02:00
|
|
|
|
|
|
|
bool has(Symbol arg) const {
|
|
|
|
auto it = std::lower_bound(formals.begin(), formals.end(), arg,
|
|
|
|
[] (const Formal & f, const Symbol & sym) { return f.name < sym; });
|
|
|
|
return it != formals.end() && it->name == arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Formal> lexicographicOrder() const
|
|
|
|
{
|
|
|
|
std::vector<Formal> result(formals.begin(), formals.end());
|
|
|
|
std::sort(result.begin(), result.end(),
|
|
|
|
[] (const Formal & a, const Formal & b) {
|
|
|
|
return std::string_view(a.name) < std::string_view(b.name);
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
2004-08-04 13:59:20 +03:00
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprLambda : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
2013-05-16 20:08:02 +03:00
|
|
|
Symbol name;
|
2010-04-13 15:25:42 +03:00
|
|
|
Symbol arg;
|
2010-04-12 21:30:11 +03:00
|
|
|
Formals * formals;
|
|
|
|
Expr * body;
|
2021-10-06 18:08:08 +03:00
|
|
|
ExprLambda(const Pos & pos, const Symbol & arg, Formals * formals, Expr * body)
|
|
|
|
: pos(pos), arg(arg), formals(formals), body(body)
|
2010-04-22 14:02:24 +03:00
|
|
|
{
|
2020-04-29 19:14:32 +03:00
|
|
|
};
|
2013-05-16 20:08:02 +03:00
|
|
|
void setName(Symbol & name);
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string showNamePos() const;
|
2021-10-06 18:08:08 +03:00
|
|
|
inline bool hasFormals() const { return formals != nullptr; }
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2020-02-24 02:32:01 +02:00
|
|
|
struct ExprCall : Expr
|
|
|
|
{
|
|
|
|
Expr * fun;
|
|
|
|
std::vector<Expr *> args;
|
|
|
|
Pos pos;
|
|
|
|
ExprCall(const Pos & pos, Expr * fun, std::vector<Expr *> && args)
|
|
|
|
: fun(fun), args(args), pos(pos)
|
|
|
|
{ }
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2010-04-13 16:42:25 +03:00
|
|
|
struct ExprLet : Expr
|
|
|
|
{
|
|
|
|
ExprAttrs * attrs;
|
|
|
|
Expr * body;
|
|
|
|
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-13 16:42:25 +03:00
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprWith : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
Expr * attrs, * body;
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t prevWith;
|
2010-04-12 21:30:11 +03:00
|
|
|
ExprWith(const Pos & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprIf : Expr
|
|
|
|
{
|
2020-04-09 10:45:15 +03:00
|
|
|
Pos pos;
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * cond, * then, * else_;
|
2020-04-09 10:45:15 +03:00
|
|
|
ExprIf(const Pos & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
struct ExprAssert : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
Expr * cond, * body;
|
|
|
|
ExprAssert(const Pos & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-13 00:21:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprOpNot : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
|
|
|
ExprOpNot(Expr * e) : e(e) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return 0; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-13 00:21:24 +03:00
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
#define MakeBinOp(name, s) \
|
2018-03-09 05:16:33 +02:00
|
|
|
struct name : Expr \
|
2010-04-12 21:30:11 +03:00
|
|
|
{ \
|
2014-04-04 23:43:52 +03:00
|
|
|
Pos pos; \
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * e1, * e2; \
|
2018-03-09 05:16:33 +02: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 20:20:32 +02:00
|
|
|
void show(std::ostream & str) const \
|
2010-04-12 21:30:11 +03:00
|
|
|
{ \
|
2014-10-20 09:44:32 +03:00
|
|
|
str << "(" << *e1 << " " s " " << *e2 << ")"; \
|
2010-04-12 21:30:11 +03:00
|
|
|
} \
|
2021-09-14 19:49:22 +03:00
|
|
|
void bindVars(const std::shared_ptr<const StaticEnv> & env) \
|
2010-04-14 17:42:32 +03:00
|
|
|
{ \
|
|
|
|
e1->bindVars(env); e2->bindVars(env); \
|
|
|
|
} \
|
2010-04-12 21:30:11 +03:00
|
|
|
void eval(EvalState & state, Env & env, Value & v); \
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; } \
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
2018-03-09 05:16:33 +02:00
|
|
|
MakeBinOp(ExprOpEq, "==")
|
|
|
|
MakeBinOp(ExprOpNEq, "!=")
|
|
|
|
MakeBinOp(ExprOpAnd, "&&")
|
|
|
|
MakeBinOp(ExprOpOr, "||")
|
|
|
|
MakeBinOp(ExprOpImpl, "->")
|
|
|
|
MakeBinOp(ExprOpUpdate, "//")
|
|
|
|
MakeBinOp(ExprOpConcatLists, "++")
|
2010-04-12 21:30:11 +03:00
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
struct ExprConcatStrings : Expr
|
|
|
|
{
|
2014-04-04 23:19:33 +03:00
|
|
|
Pos pos;
|
2013-03-08 02:24:59 +02:00
|
|
|
bool forceString;
|
2022-02-21 17:32:34 +02:00
|
|
|
std::vector<std::pair<Pos, Expr *> > * es;
|
|
|
|
ExprConcatStrings(const Pos & pos, bool forceString, std::vector<std::pair<Pos, Expr *> > * es)
|
2014-04-04 23:19:33 +03:00
|
|
|
: pos(pos), forceString(forceString), es(es) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2010-04-13 00:21:24 +03:00
|
|
|
};
|
2004-04-06 01:27:41 +03:00
|
|
|
|
2013-11-18 21:14:54 +02:00
|
|
|
struct ExprPos : Expr
|
|
|
|
{
|
|
|
|
Pos pos;
|
|
|
|
ExprPos(const Pos & pos) : pos(pos) { };
|
2022-03-26 02:15:31 +02:00
|
|
|
const Pos* getPos() const { return &pos; }
|
2021-12-28 03:29:55 +02:00
|
|
|
COMMON_METHODS
|
2013-11-18 21:14:54 +02:00
|
|
|
};
|
|
|
|
|
2010-04-08 14:41:19 +03:00
|
|
|
|
2010-04-14 17:42:32 +03: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;
|
2020-02-21 19:31:16 +02:00
|
|
|
|
|
|
|
// Note: these must be in sorted order.
|
2020-02-24 15:33:01 +02:00
|
|
|
typedef std::vector<std::pair<Symbol, Displacement>> Vars;
|
2010-04-14 17:42:32 +03:00
|
|
|
Vars vars;
|
2020-02-21 19:31:16 +02:00
|
|
|
|
|
|
|
StaticEnv(bool isWith, const StaticEnv * up, size_t expectedSize = 0) : isWith(isWith), up(up) {
|
|
|
|
vars.reserve(expectedSize);
|
|
|
|
};
|
|
|
|
|
|
|
|
void sort()
|
|
|
|
{
|
2022-02-04 08:36:56 +02:00
|
|
|
std::stable_sort(vars.begin(), vars.end(),
|
2020-02-21 19:31:16 +02:00
|
|
|
[](const Vars::value_type & a, const Vars::value_type & b) { return a.first < b.first; });
|
|
|
|
}
|
|
|
|
|
2021-12-27 14:18:55 +02:00
|
|
|
void deduplicate()
|
|
|
|
{
|
2022-02-04 08:36:56 +02:00
|
|
|
auto it = vars.begin(), jt = it, end = vars.end();
|
|
|
|
while (jt != end) {
|
|
|
|
*it = *jt++;
|
|
|
|
while (jt != end && it->first == jt->first) *it = *jt++;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
vars.erase(it, end);
|
2021-12-27 14:18:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 19:31:16 +02:00
|
|
|
Vars::const_iterator find(const Symbol & name) const
|
|
|
|
{
|
|
|
|
Vars::value_type key(name, 0);
|
|
|
|
auto i = std::lower_bound(vars.begin(), vars.end(), key);
|
|
|
|
if (i != vars.end() && i->first == name) return i;
|
|
|
|
return vars.end();
|
|
|
|
}
|
2010-04-14 17:42:32 +03:00
|
|
|
};
|
|
|
|
|
2006-10-16 18:55:34 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|