2012-07-18 21:59:03 +03:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2003-10-30 18:11:24 +02:00
|
|
|
|
2022-03-04 20:31:59 +02:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
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"
|
2022-04-22 11:01:02 +03:00
|
|
|
#include "chunked-vector.hh"
|
2023-12-18 23:14:42 +02:00
|
|
|
#include "position.hh"
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
#include "eval-error.hh"
|
2024-02-01 23:07:45 +02:00
|
|
|
#include "pos-idx.hh"
|
|
|
|
#include "pos-table.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
namespace nix {
|
2003-10-30 18:11:24 +02:00
|
|
|
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct Env;
|
|
|
|
struct Value;
|
2014-01-21 19:29:55 +02:00
|
|
|
class EvalState;
|
2023-12-22 19:19:53 +02:00
|
|
|
struct ExprWith;
|
2010-04-14 17:42:32 +03:00
|
|
|
struct StaticEnv;
|
|
|
|
|
|
|
|
|
2023-04-07 16:55:28 +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;
|
2022-04-26 14:23:32 +03:00
|
|
|
AttrName(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-03-05 15:40:24 +02:00
|
|
|
std::string showAttrPath(const SymbolTable & symbols, 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
|
|
|
|
{
|
2024-01-15 17:52:18 +02:00
|
|
|
struct AstSymbols {
|
|
|
|
Symbol sub, lessThan, mul, div, or_, findFile, nixPath, body;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-09-12 14:21:55 +03:00
|
|
|
static unsigned long nrExprs;
|
2023-09-25 19:51:17 +03:00
|
|
|
Expr() {
|
2023-09-12 14:21:55 +03:00
|
|
|
nrExprs++;
|
|
|
|
}
|
2014-01-21 19:29:55 +02:00
|
|
|
virtual ~Expr() { };
|
2022-03-05 15:40:24 +02:00
|
|
|
virtual void show(const SymbolTable & symbols, std::ostream & str) const;
|
2022-05-19 19:48:10 +03:00
|
|
|
virtual void bindVars(EvalState & es, 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);
|
2022-04-22 22:45:39 +03:00
|
|
|
virtual void setName(Symbol name);
|
2022-05-05 13:29:14 +03:00
|
|
|
virtual PosIdx getPos() const { return noPos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define COMMON_METHODS \
|
2022-06-02 17:55:28 +03:00
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override; \
|
|
|
|
void eval(EvalState & state, Env & env, Value & v) override; \
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override;
|
2010-04-12 21:30:11 +03:00
|
|
|
|
|
|
|
struct ExprInt : Expr
|
|
|
|
{
|
2012-01-07 19:26:33 +02:00
|
|
|
Value v;
|
2023-09-12 14:45:45 +03:00
|
|
|
ExprInt(NixInt n) { v.mkInt(n); };
|
2022-06-02 17:55:28 +03:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2016-01-05 01:40:40 +02:00
|
|
|
struct ExprFloat : Expr
|
|
|
|
{
|
|
|
|
Value v;
|
2023-09-12 14:45:45 +03:00
|
|
|
ExprFloat(NixFloat nf) { v.mkFloat(nf); };
|
2022-06-02 17:55:28 +03:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2016-01-05 01:40:40 +02:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
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;
|
2023-02-11 01:34:31 +02:00
|
|
|
ExprString(std::string &&s) : s(std::move(s)) { v.mkString(this->s.data()); };
|
2022-06-02 17:55:28 +03:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprPath : Expr
|
|
|
|
{
|
2023-10-18 16:32:31 +03:00
|
|
|
ref<InputAccessor> accessor;
|
2022-02-25 17:00:00 +02:00
|
|
|
std::string s;
|
2012-01-07 19:26:33 +02:00
|
|
|
Value v;
|
2023-10-18 16:32:31 +03:00
|
|
|
ExprPath(ref<InputAccessor> accessor, std::string s) : accessor(accessor), s(std::move(s))
|
|
|
|
{
|
|
|
|
v.mkPath(&*accessor, this->s.c_str());
|
|
|
|
}
|
2022-06-02 17:55:28 +03:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx 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
|
2023-12-22 19:19:53 +02:00
|
|
|
or function argument) or from a "with".
|
|
|
|
|
|
|
|
`nullptr`: Not from a `with`.
|
|
|
|
Valid pointer: the nearest, innermost `with` expression to query first. */
|
|
|
|
ExprWith * fromWith;
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/* In the former case, the value is obtained by going `level`
|
2010-04-14 17:42:32 +03:00
|
|
|
levels up from the current environment and getting the
|
2023-04-07 16:55:28 +03:00
|
|
|
`displ`th value in that environment. In the latter case, the
|
|
|
|
value is obtained by getting the attribute named `name` from
|
|
|
|
the set stored in the environment that is `level` levels up
|
2013-10-24 17:41:04 +03:00
|
|
|
from the current one.*/
|
2020-02-24 15:33:01 +02:00
|
|
|
Level level;
|
|
|
|
Displacement displ;
|
2010-04-14 18:14:23 +03:00
|
|
|
|
2022-04-26 14:23:32 +03:00
|
|
|
ExprVar(Symbol name) : name(name) { };
|
|
|
|
ExprVar(const PosIdx & pos, Symbol name) : pos(pos), name(name) { };
|
2022-06-02 17:55:28 +03:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
struct ExprInheritFrom : ExprVar
|
|
|
|
{
|
|
|
|
ExprInheritFrom(PosIdx pos, Displacement displ): ExprVar(pos, {})
|
|
|
|
{
|
|
|
|
this->level = 0;
|
|
|
|
this->displ = displ;
|
|
|
|
this->fromWith = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprSelect : Expr
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2011-07-13 15:19:57 +03:00
|
|
|
Expr * e, * def;
|
2011-07-06 15:28:57 +03:00
|
|
|
AttrPath attrPath;
|
2023-09-24 09:54:41 +03:00
|
|
|
ExprSelect(const PosIdx & pos, Expr * e, AttrPath attrPath, Expr * def) : pos(pos), e(e), def(def), attrPath(std::move(attrPath)) { };
|
2022-04-26 14:23:32 +03:00
|
|
|
ExprSelect(const PosIdx & pos, Expr * e, Symbol name) : pos(pos), e(e), def(0) { attrPath.push_back(AttrName(name)); };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
struct ExprOpHasAttr : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
2011-07-06 13:58:17 +03:00
|
|
|
AttrPath attrPath;
|
2023-09-24 09:54:41 +03:00
|
|
|
ExprOpHasAttr(Expr * e, AttrPath attrPath) : e(e), attrPath(std::move(attrPath)) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return e->getPos(); }
|
2010-04-13 00:21:24 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprAttrs : Expr
|
|
|
|
{
|
|
|
|
bool recursive;
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2010-10-24 22:52:33 +03:00
|
|
|
struct AttrDef {
|
2024-01-27 17:33:34 +02:00
|
|
|
enum class Kind {
|
|
|
|
/** `attr = expr;` */
|
|
|
|
Plain,
|
|
|
|
/** `inherit attr1 attrn;` */
|
|
|
|
Inherited,
|
|
|
|
/** `inherit (expr) attr1 attrn;` */
|
|
|
|
InheritedFrom,
|
|
|
|
};
|
|
|
|
|
|
|
|
Kind kind;
|
2013-07-16 00:10:18 +03:00
|
|
|
Expr * e;
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2020-02-24 15:33:01 +02:00
|
|
|
Displacement displ; // displacement
|
2024-01-27 17:33:34 +02:00
|
|
|
AttrDef(Expr * e, const PosIdx & pos, Kind kind = Kind::Plain)
|
|
|
|
: kind(kind), e(e), pos(pos) { };
|
2010-10-24 22:52:33 +03:00
|
|
|
AttrDef() { };
|
2024-01-27 17:33:34 +02:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
template<typename T>
|
|
|
|
const T & chooseByKind(const T & plain, const T & inherited, const T & inheritedFrom) const
|
|
|
|
{
|
|
|
|
switch (kind) {
|
|
|
|
case Kind::Plain:
|
|
|
|
return plain;
|
|
|
|
case Kind::Inherited:
|
|
|
|
return inherited;
|
|
|
|
default:
|
|
|
|
case Kind::InheritedFrom:
|
|
|
|
return inheritedFrom;
|
|
|
|
}
|
|
|
|
}
|
2010-10-24 22:52:33 +03:00
|
|
|
};
|
|
|
|
typedef std::map<Symbol, AttrDef> AttrDefs;
|
|
|
|
AttrDefs attrs;
|
2024-01-27 17:33:34 +02:00
|
|
|
std::unique_ptr<std::vector<Expr *>> inheritFromExprs;
|
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;
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
|
|
|
DynamicAttrDef(Expr * nameExpr, Expr * valueExpr, const PosIdx & pos)
|
2014-10-05 02:04:58 +03:00
|
|
|
: 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;
|
2022-03-04 20:31:59 +02:00
|
|
|
ExprAttrs(const PosIdx &pos) : recursive(false), pos(pos) { };
|
|
|
|
ExprAttrs() : recursive(false) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
2024-01-27 17:33:34 +02:00
|
|
|
|
2024-01-27 17:33:34 +02:00
|
|
|
std::shared_ptr<const StaticEnv> bindInheritSources(
|
|
|
|
EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
|
|
|
Env * buildInheritFromEnv(EvalState & state, Env & up);
|
2024-01-27 17:33:34 +02:00
|
|
|
void showBindings(const SymbolTable & symbols, std::ostream & str) const;
|
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() { };
|
|
|
|
COMMON_METHODS
|
Optimize empty list constants
This avoids a Value allocation for empty list constants. During a `nix
search nixpkgs`, about 82% of all thunked lists are empty, so this
removes about 3 million Value allocations.
Performance comparison on `nix search github:NixOS/nixpkgs/e1fa12d4f6c6fe19ccb59cac54b5b3f25e160870 --no-eval-cache`:
maximum RSS: median = 3845432.0000 mean = 3845432.0000 stddev = 0.0000 min = 3845432.0000 max = 3845432.0000 [rejected?, p=0.00000, Δ=-70084.00000±0.00000]
soft page faults: median = 965395.0000 mean = 965394.6667 stddev = 1.1181 min = 965392.0000 max = 965396.0000 [rejected?, p=0.00000, Δ=-17929.77778±38.59610]
system CPU time: median = 1.8029 mean = 1.7702 stddev = 0.0621 min = 1.6749 max = 1.8417 [rejected, p=0.00064, Δ=-0.12873±0.09905]
user CPU time: median = 14.1022 mean = 14.0633 stddev = 0.1869 min = 13.8118 max = 14.3190 [not rejected, p=0.03006, Δ=-0.18248±0.24928]
elapsed time: median = 15.8205 mean = 15.8618 stddev = 0.2312 min = 15.5033 max = 16.1670 [not rejected, p=0.00558, Δ=-0.28963±0.29434]
2024-01-02 13:39:16 +02:00
|
|
|
Value * maybeThunk(EvalState & state, Env & env) override;
|
2022-05-05 13:29:14 +03:00
|
|
|
|
|
|
|
PosIdx getPos() const override
|
|
|
|
{
|
|
|
|
return elems.empty() ? noPos : elems.front()->getPos();
|
|
|
|
}
|
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
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2010-04-13 15:25:42 +03:00
|
|
|
Symbol name;
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * def;
|
|
|
|
};
|
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
|
|
|
|
2022-04-26 14:23:32 +03:00
|
|
|
bool has(Symbol arg) const
|
|
|
|
{
|
2022-01-19 17:49:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-05 15:40:24 +02:00
|
|
|
std::vector<Formal> lexicographicOrder(const SymbolTable & symbols) const
|
2022-01-19 17:49:02 +02:00
|
|
|
{
|
|
|
|
std::vector<Formal> result(formals.begin(), formals.end());
|
|
|
|
std::sort(result.begin(), result.end(),
|
2022-03-05 15:40:24 +02:00
|
|
|
[&] (const Formal & a, const Formal & b) {
|
|
|
|
std::string_view sa = symbols[a.name], sb = symbols[b.name];
|
|
|
|
return sa < sb;
|
2022-01-19 17:49:02 +02:00
|
|
|
});
|
|
|
|
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
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx 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;
|
2022-04-22 22:45:39 +03:00
|
|
|
ExprLambda(PosIdx pos, Symbol arg, Formals * formals, Expr * body)
|
2021-10-06 18:08:08 +03:00
|
|
|
: pos(pos), arg(arg), formals(formals), body(body)
|
2010-04-22 14:02:24 +03:00
|
|
|
{
|
2020-04-29 19:14:32 +03:00
|
|
|
};
|
2022-03-05 15:40:24 +02:00
|
|
|
ExprLambda(PosIdx pos, Formals * formals, Expr * body)
|
|
|
|
: pos(pos), formals(formals), body(body)
|
|
|
|
{
|
|
|
|
}
|
2022-06-02 17:55:28 +03:00
|
|
|
void setName(Symbol name) override;
|
2022-03-05 15:40:24 +02:00
|
|
|
std::string showNamePos(const EvalState & state) const;
|
2021-10-06 18:08:08 +03:00
|
|
|
inline bool hasFormals() const { return formals != nullptr; }
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2020-02-24 02:32:01 +02:00
|
|
|
struct ExprCall : Expr
|
|
|
|
{
|
|
|
|
Expr * fun;
|
|
|
|
std::vector<Expr *> args;
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
|
|
|
ExprCall(const PosIdx & pos, Expr * fun, std::vector<Expr *> && args)
|
2020-02-24 02:32:01 +02:00
|
|
|
: fun(fun), args(args), pos(pos)
|
|
|
|
{ }
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2020-02-24 02:32:01 +02:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-13 16:42:25 +03:00
|
|
|
struct ExprLet : Expr
|
|
|
|
{
|
|
|
|
ExprAttrs * attrs;
|
|
|
|
Expr * body;
|
|
|
|
ExprLet(ExprAttrs * attrs, Expr * body) : attrs(attrs), body(body) { };
|
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-12 21:30:11 +03:00
|
|
|
struct ExprWith : Expr
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * attrs, * body;
|
2018-05-02 14:56:34 +03:00
|
|
|
size_t prevWith;
|
2023-12-22 19:19:53 +02:00
|
|
|
ExprWith * parentWith;
|
2022-03-04 20:31:59 +02:00
|
|
|
ExprWith(const PosIdx & pos, Expr * attrs, Expr * body) : pos(pos), attrs(attrs), body(body) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprIf : Expr
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2010-04-12 21:30:11 +03:00
|
|
|
Expr * cond, * then, * else_;
|
2022-03-04 20:31:59 +02:00
|
|
|
ExprIf(const PosIdx & pos, Expr * cond, Expr * then, Expr * else_) : pos(pos), cond(cond), then(then), else_(else_) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-12 21:30:11 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2010-04-13 00:21:24 +03:00
|
|
|
struct ExprAssert : Expr
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2010-04-13 00:21:24 +03:00
|
|
|
Expr * cond, * body;
|
2022-03-04 20:31:59 +02:00
|
|
|
ExprAssert(const PosIdx & pos, Expr * cond, Expr * body) : pos(pos), cond(cond), body(body) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-13 00:21:24 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExprOpNot : Expr
|
|
|
|
{
|
|
|
|
Expr * e;
|
|
|
|
ExprOpNot(Expr * e) : e(e) { };
|
2023-12-07 20:01:42 +02:00
|
|
|
PosIdx getPos() const override { return e->getPos(); }
|
2010-04-13 00:21:24 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{ \
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx 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) { }; \
|
2022-03-04 20:31:59 +02:00
|
|
|
name(const PosIdx & pos, Expr * e1, Expr * e2) : pos(pos), e1(e1), e2(e2) { }; \
|
2022-06-02 17:55:28 +03:00
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override \
|
2010-04-12 21:30:11 +03:00
|
|
|
{ \
|
2022-03-05 15:40:24 +02:00
|
|
|
str << "("; e1->show(symbols, str); str << " " s " "; e2->show(symbols, str); str << ")"; \
|
2010-04-12 21:30:11 +03:00
|
|
|
} \
|
2022-06-02 17:55:28 +03:00
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override \
|
2010-04-14 17:42:32 +03:00
|
|
|
{ \
|
2022-03-05 15:40:24 +02:00
|
|
|
e1->bindVars(es, env); e2->bindVars(es, env); \
|
2010-04-14 17:42:32 +03:00
|
|
|
} \
|
2022-06-02 17:55:28 +03:00
|
|
|
void eval(EvalState & state, Env & env, Value & v) override; \
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { 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
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
2013-03-08 02:24:59 +02:00
|
|
|
bool forceString;
|
2022-05-25 16:49:41 +03:00
|
|
|
std::vector<std::pair<PosIdx, Expr *>> * es;
|
|
|
|
ExprConcatStrings(const PosIdx & pos, bool forceString, std::vector<std::pair<PosIdx, Expr *>> * es)
|
2014-04-04 23:19:33 +03:00
|
|
|
: pos(pos), forceString(forceString), es(es) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2010-04-13 00:21:24 +03:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
2004-04-06 01:27:41 +03:00
|
|
|
|
2013-11-18 21:14:54 +02:00
|
|
|
struct ExprPos : Expr
|
|
|
|
{
|
2022-03-04 20:31:59 +02:00
|
|
|
PosIdx pos;
|
|
|
|
ExprPos(const PosIdx & pos) : pos(pos) { };
|
2022-05-05 13:29:14 +03:00
|
|
|
PosIdx getPos() const override { return pos; }
|
2013-11-18 21:14:54 +02:00
|
|
|
COMMON_METHODS
|
|
|
|
};
|
|
|
|
|
2023-12-11 17:23:08 +02:00
|
|
|
/* only used to mark thunks as black holes. */
|
|
|
|
struct ExprBlackHole : Expr
|
|
|
|
{
|
|
|
|
void show(const SymbolTable & symbols, std::ostream & str) const override {}
|
|
|
|
void eval(EvalState & state, Env & env, Value & v) override;
|
|
|
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern ExprBlackHole eBlackHole;
|
|
|
|
|
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
|
|
|
|
{
|
2023-12-22 19:19:53 +02:00
|
|
|
ExprWith * isWith;
|
2010-04-14 17:42:32 +03:00
|
|
|
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
|
|
|
|
2023-12-22 19:19:53 +02:00
|
|
|
StaticEnv(ExprWith * isWith, const StaticEnv * up, size_t expectedSize = 0) : isWith(isWith), up(up) {
|
2020-02-21 19:31:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-26 14:23:32 +03:00
|
|
|
Vars::const_iterator find(Symbol name) const
|
2020-02-21 19:31:16 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|