forked from lix-project/lix
Add the ExprBuiltin Expr type to the AST
Certain desugaring schemes may require the parser to use some builtin function to do some of the work (e.g. currently `throw` is used to lazily cause an error if a `<>`-style path is not in the search path) Unfortunately, these names are not reserved keywords, so an expression that uses such a syntactic sugar will not see the expected behavior (see tests/lang/eval-okay-redefine-builtin.nix for an example). This adds the ExprBuiltin AST type, which when evaluated uses the value from the rootmost variable scope (which of course is initialized internally and can't shadow any of the builtins). Signed-off-by: Shea Levy <shea@shealevy.com>
This commit is contained in:
parent
5ba5993470
commit
136f2f7046
|
@ -908,6 +908,17 @@ void ExprOpNot::eval(EvalState & state, Env & env, Value & v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ExprBuiltin::eval(EvalState & state, Env & env, Value & v)
|
||||||
|
{
|
||||||
|
// Not a hot path at all, but would be nice to access state.baseEnv directly
|
||||||
|
Env *baseEnv = &env;
|
||||||
|
while (baseEnv->up) baseEnv = baseEnv->up;
|
||||||
|
Bindings::iterator binding = baseEnv->values[0]->attrs->find(name);
|
||||||
|
assert(binding != baseEnv->values[0]->attrs->end());
|
||||||
|
v = *binding->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ExprOpEq::eval(EvalState & state, Env & env, Value & v)
|
void ExprOpEq::eval(EvalState & state, Env & env, Value & v)
|
||||||
{
|
{
|
||||||
Value v1; e1->eval(state, env, v1);
|
Value v1; e1->eval(state, env, v1);
|
||||||
|
|
|
@ -121,6 +121,11 @@ void ExprOpNot::show(std::ostream & str)
|
||||||
str << "! " << *e;
|
str << "! " << *e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExprBuiltin::show(std::ostream & str)
|
||||||
|
{
|
||||||
|
str << "builtins." << name;
|
||||||
|
}
|
||||||
|
|
||||||
void ExprConcatStrings::show(std::ostream & str)
|
void ExprConcatStrings::show(std::ostream & str)
|
||||||
{
|
{
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
@ -314,6 +319,10 @@ void ExprOpNot::bindVars(const StaticEnv & env)
|
||||||
e->bindVars(env);
|
e->bindVars(env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExprBuiltin::bindVars(const StaticEnv & env)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void ExprConcatStrings::bindVars(const StaticEnv & env)
|
void ExprConcatStrings::bindVars(const StaticEnv & env)
|
||||||
{
|
{
|
||||||
foreach (vector<Expr *>::iterator, i, *es)
|
foreach (vector<Expr *>::iterator, i, *es)
|
||||||
|
|
|
@ -248,6 +248,13 @@ struct ExprOpNot : Expr
|
||||||
COMMON_METHODS
|
COMMON_METHODS
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ExprBuiltin : Expr
|
||||||
|
{
|
||||||
|
Symbol name;
|
||||||
|
ExprBuiltin(Symbol name) : name(name) { };
|
||||||
|
COMMON_METHODS
|
||||||
|
};
|
||||||
|
|
||||||
#define MakeBinOp(name, s) \
|
#define MakeBinOp(name, s) \
|
||||||
struct Expr##name : Expr \
|
struct Expr##name : Expr \
|
||||||
{ \
|
{ \
|
||||||
|
|
|
@ -311,13 +311,13 @@ expr_if
|
||||||
|
|
||||||
expr_op
|
expr_op
|
||||||
: '!' expr_op %prec NOT { $$ = new ExprOpNot($2); }
|
: '!' expr_op %prec NOT { $$ = new ExprOpNot($2); }
|
||||||
| '-' expr_op %prec NEGATE { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__sub")), new ExprInt(0)), $2); }
|
| '-' expr_op %prec NEGATE { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("sub")), new ExprInt(0)), $2); }
|
||||||
| expr_op EQ expr_op { $$ = new ExprOpEq($1, $3); }
|
| expr_op EQ expr_op { $$ = new ExprOpEq($1, $3); }
|
||||||
| expr_op NEQ expr_op { $$ = new ExprOpNEq($1, $3); }
|
| expr_op NEQ expr_op { $$ = new ExprOpNEq($1, $3); }
|
||||||
| expr_op '<' expr_op { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__lessThan")), $1), $3); }
|
| expr_op '<' expr_op { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("lessThan")), $1), $3); }
|
||||||
| expr_op LEQ expr_op { $$ = new ExprOpNot(new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__lessThan")), $3), $1)); }
|
| expr_op LEQ expr_op { $$ = new ExprOpNot(new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("lessThan")), $3), $1)); }
|
||||||
| expr_op '>' expr_op { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__lessThan")), $3), $1); }
|
| expr_op '>' expr_op { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("lessThan")), $3), $1); }
|
||||||
| expr_op GEQ expr_op { $$ = new ExprOpNot(new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__lessThan")), $1), $3)); }
|
| expr_op GEQ expr_op { $$ = new ExprOpNot(new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("lessThan")), $1), $3)); }
|
||||||
| expr_op AND expr_op { $$ = new ExprOpAnd($1, $3); }
|
| expr_op AND expr_op { $$ = new ExprOpAnd($1, $3); }
|
||||||
| expr_op OR expr_op { $$ = new ExprOpOr($1, $3); }
|
| expr_op OR expr_op { $$ = new ExprOpOr($1, $3); }
|
||||||
| expr_op IMPL expr_op { $$ = new ExprOpImpl($1, $3); }
|
| expr_op IMPL expr_op { $$ = new ExprOpImpl($1, $3); }
|
||||||
|
@ -329,9 +329,9 @@ expr_op
|
||||||
l->push_back($3);
|
l->push_back($3);
|
||||||
$$ = new ExprConcatStrings(false, l);
|
$$ = new ExprConcatStrings(false, l);
|
||||||
}
|
}
|
||||||
| expr_op '-' expr_op { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__sub")), $1), $3); }
|
| expr_op '-' expr_op { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("sub")), $1), $3); }
|
||||||
| expr_op '*' expr_op { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__mul")), $1), $3); }
|
| expr_op '*' expr_op { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("mul")), $1), $3); }
|
||||||
| expr_op '/' expr_op { $$ = new ExprApp(new ExprApp(new ExprVar(noPos, data->symbols.create("__div")), $1), $3); }
|
| expr_op '/' expr_op { $$ = new ExprApp(new ExprApp(new ExprBuiltin(data->symbols.create("div")), $1), $3); }
|
||||||
| expr_op CONCAT expr_op { $$ = new ExprOpConcatLists($1, $3); }
|
| expr_op CONCAT expr_op { $$ = new ExprOpConcatLists($1, $3); }
|
||||||
| expr_app
|
| expr_app
|
||||||
;
|
;
|
||||||
|
@ -381,7 +381,7 @@ expr_simple
|
||||||
‘throw’. */
|
‘throw’. */
|
||||||
$$ = path2 == ""
|
$$ = path2 == ""
|
||||||
? (Expr * ) new ExprApp(
|
? (Expr * ) new ExprApp(
|
||||||
new ExprVar(noPos, data->symbols.create("throw")),
|
new ExprBuiltin(data->symbols.create("throw")),
|
||||||
new ExprString(data->symbols.create(
|
new ExprString(data->symbols.create(
|
||||||
(format("file `%1%' was not found in the Nix search path (add it using $NIX_PATH or -I)") % path).str())))
|
(format("file `%1%' was not found in the Nix search path (add it using $NIX_PATH or -I)") % path).str())))
|
||||||
: (Expr * ) new ExprPath(path2);
|
: (Expr * ) new ExprPath(path2);
|
||||||
|
|
1
tests/lang/eval-okay-redefine-builtin.exp
Normal file
1
tests/lang/eval-okay-redefine-builtin.exp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
false
|
3
tests/lang/eval-okay-redefine-builtin.nix
Normal file
3
tests/lang/eval-okay-redefine-builtin.nix
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
let
|
||||||
|
throw = abort "Error!";
|
||||||
|
in (builtins.tryEval <foobaz>).success
|
Loading…
Reference in a new issue