From 8a6a14e1f5006543f63117fb1f0a81a0b1024ebd Mon Sep 17 00:00:00 2001 From: volth Date: Sat, 12 May 2018 16:50:39 +0000 Subject: add `mod' and bitwise builtins --- src/libexpr/parser.y | 7 +++++++ src/libexpr/primops.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) (limited to 'src') diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index eee48887dc22..6beb28868e8a 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -338,6 +338,13 @@ expr_op | expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__sub")), $1), $3); } | expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mul")), $1), $3); } | expr_op '/' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__div")), $1), $3); } + | expr_op '%' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mod")), $1), $3); } + | expr_op '&' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_and")), $1), $3); } + | expr_op '|' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_or")), $1), $3); } + | expr_op '^' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_xor")), $1), $3); } + | expr_op '<<' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_shl")), $1), $3); } + | expr_op '>>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_unsigned_shr")), $1), $3); } + | expr_op '>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_signed_shr")), $1), $3); } | expr_op CONCAT expr_op { $$ = new ExprOpConcatLists(CUR_POS, $1, $3); } | expr_app ; diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 57dc7bd1279d..fa17f06b4c73 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,6 +1676,52 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } +static void prim_mod(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + NixFloat f2 = state.forceFloat(*args[1], pos); + if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos); + + if (args[0]->type == tFloat || args[1]->type == tFloat) { + mkFloat(v, state.forceFloat(*args[0], pos) % state.forceFloat(*args[1], pos)); + } else { + NixInt i1 = state.forceInt(*args[0], pos); + NixInt i2 = state.forceInt(*args[1], pos); + /* Avoid division overflow as it might raise SIGFPE. */ + if (i1 == std::numeric_limits::min() && i2 == -1) + throw EvalError(format("overflow in integer division, at %1%") % pos); + mkInt(v, i1 % i2); + } +} + +static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); +} + +static void prim_bin_or(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) | state.forceInt(*args[1], pos)); +} + +static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); +} + +static void prim_bin_shl(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, state.forceInt(*args[0], pos) << (state.forceInt(*args[1], pos) & 63)); +} + +static void prim_bin_unsigned_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, (unsigned long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); +} + +static void prim_bin_signed_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) +{ + mkInt(v, (signed long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); +} static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v) { @@ -2221,6 +2267,13 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); + addPrimOp("__mod", 2, prim_mod); + addPrimOp("__bin_and", 2, prim_bin_and); + addPrimOp("__bin_or", 2, prim_bin_or); + addPrimOp("__bin_xor", 2, prim_bin_xor); + addPrimOp("__bin_shl", 2, prim_bin_shl); + addPrimOp("__bin_unsigned_shr", 2, prim_bin_unsigned_shr); + addPrimOp("__bin_signed_shr", 2, prim_bin_signed_shr); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation -- cgit 1.4.1 From 49b7cf1813d71b2c9d09f7c69408061774c01c51 Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 06:55:24 +0000 Subject: add `mod' and bitwise builtins: remove infix functions --- src/libexpr/parser.y | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src') diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index 6beb28868e8a..eee48887dc22 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -338,13 +338,6 @@ expr_op | expr_op '-' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__sub")), $1), $3); } | expr_op '*' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mul")), $1), $3); } | expr_op '/' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__div")), $1), $3); } - | expr_op '%' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__mod")), $1), $3); } - | expr_op '&' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_and")), $1), $3); } - | expr_op '|' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_or")), $1), $3); } - | expr_op '^' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_xor")), $1), $3); } - | expr_op '<<' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_shl")), $1), $3); } - | expr_op '>>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_unsigned_shr")), $1), $3); } - | expr_op '>>' expr_op { $$ = new ExprApp(CUR_POS, new ExprApp(new ExprVar(data->symbols.create("__bin_signed_shr")), $1), $3); } | expr_op CONCAT expr_op { $$ = new ExprOpConcatLists(CUR_POS, $1, $3); } | expr_app ; -- cgit 1.4.1 From f3c090f91ce5e188151d887e3b5806239f5e6c0a Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 06:57:11 +0000 Subject: add `mod' and bitwise builtins: remove `mod' and shifts --- src/libexpr/primops.cc | 36 ------------------------------------ 1 file changed, 36 deletions(-) (limited to 'src') diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index fa17f06b4c73..8514e804f11e 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,23 +1676,6 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } -static void prim_mod(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - NixFloat f2 = state.forceFloat(*args[1], pos); - if (f2 == 0) throw EvalError(format("division by zero, at %1%") % pos); - - if (args[0]->type == tFloat || args[1]->type == tFloat) { - mkFloat(v, state.forceFloat(*args[0], pos) % state.forceFloat(*args[1], pos)); - } else { - NixInt i1 = state.forceInt(*args[0], pos); - NixInt i2 = state.forceInt(*args[1], pos); - /* Avoid division overflow as it might raise SIGFPE. */ - if (i1 == std::numeric_limits::min() && i2 == -1) - throw EvalError(format("overflow in integer division, at %1%") % pos); - mkInt(v, i1 % i2); - } -} - static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); @@ -1708,21 +1691,6 @@ static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Val mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); } -static void prim_bin_shl(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, state.forceInt(*args[0], pos) << (state.forceInt(*args[1], pos) & 63)); -} - -static void prim_bin_unsigned_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, (unsigned long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); -} - -static void prim_bin_signed_shr(EvalState & state, const Pos & pos, Value * * args, Value & v) -{ - mkInt(v, (signed long)state.forceInt(*args[0], pos) >> (state.forceInt(*args[1], pos) & 63)); -} - static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v) { state.forceValue(*args[0]); @@ -2267,13 +2235,9 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); - addPrimOp("__mod", 2, prim_mod); addPrimOp("__bin_and", 2, prim_bin_and); addPrimOp("__bin_or", 2, prim_bin_or); addPrimOp("__bin_xor", 2, prim_bin_xor); - addPrimOp("__bin_shl", 2, prim_bin_shl); - addPrimOp("__bin_unsigned_shr", 2, prim_bin_unsigned_shr); - addPrimOp("__bin_signed_shr", 2, prim_bin_signed_shr); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation -- cgit 1.4.1 From 6cc28c0589a0072e94128976ddbf8b2fbfd9f496 Mon Sep 17 00:00:00 2001 From: volth Date: Wed, 16 May 2018 10:52:19 +0000 Subject: add `mod' and bitwise builtins: camel-case function names --- src/libexpr/primops.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 8514e804f11e..9dab8ecb0464 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1676,17 +1676,17 @@ static void prim_div(EvalState & state, const Pos & pos, Value * * args, Value & } } -static void prim_bin_and(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitAnd(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) & state.forceInt(*args[1], pos)); } -static void prim_bin_or(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitOr(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) | state.forceInt(*args[1], pos)); } -static void prim_bin_xor(EvalState & state, const Pos & pos, Value * * args, Value & v) +static void prim_bitXor(EvalState & state, const Pos & pos, Value * * args, Value & v) { mkInt(v, state.forceInt(*args[0], pos) ^ state.forceInt(*args[1], pos)); } @@ -2235,9 +2235,9 @@ void EvalState::createBaseEnv() addPrimOp("__sub", 2, prim_sub); addPrimOp("__mul", 2, prim_mul); addPrimOp("__div", 2, prim_div); - addPrimOp("__bin_and", 2, prim_bin_and); - addPrimOp("__bin_or", 2, prim_bin_or); - addPrimOp("__bin_xor", 2, prim_bin_xor); + addPrimOp("__bitAnd", 2, prim_bitAnd); + addPrimOp("__bitOr", 2, prim_bitOr); + addPrimOp("__bitXor", 2, prim_bitXor); addPrimOp("__lessThan", 2, prim_lessThan); // String manipulation -- cgit 1.4.1