Author: aschnell Date: Fri Jun 20 15:32:06 2008 New Revision: 48453 URL: http://svn.opensuse.org/viewcvs/yast?rev=48453&view=rev Log: - added list::reduce builtin (documentation unfinished) Modified: trunk/core/VERSION trunk/core/libycp/src/YCPBuiltinInteger.cc trunk/core/libycp/src/YCPBuiltinList.cc trunk/core/libycp/testsuite/tests/builtin/Builtin-List.err trunk/core/libycp/testsuite/tests/builtin/Builtin-List.out trunk/core/libycp/testsuite/tests/builtin/Builtin-List.ycp trunk/core/package/yast2-core.changes Modified: trunk/core/VERSION URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/VERSION?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/VERSION (original) +++ trunk/core/VERSION Fri Jun 20 15:32:06 2008 @@ -1 +1 @@ -2.17.0 +2.17.1 Modified: trunk/core/libycp/src/YCPBuiltinInteger.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/libycp/src/YCPBuiltinInteger.cc?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/libycp/src/YCPBuiltinInteger.cc (original) +++ trunk/core/libycp/src/YCPBuiltinInteger.cc Fri Jun 20 15:32:06 2008 @@ -302,7 +302,6 @@ break; } return YCPNull(); - } Modified: trunk/core/libycp/src/YCPBuiltinList.cc URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/libycp/src/YCPBuiltinList.cc?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/libycp/src/YCPBuiltinList.cc (original) +++ trunk/core/libycp/src/YCPBuiltinList.cc Fri Jun 20 15:32:06 2008 @@ -1083,6 +1083,135 @@ static YCPValue +l_reduce1 (const YCPSymbol &x, const YCPSymbol &y, const YCPList &list, const YCPCode &expr) +{ + /** + * @builtin list::reduce + * @id reduce_1 + * @short Reduces a list to a single value. + * @param flex1 x + * @param flex1 y + * @param list<flex1> list + * @param block<flex1> expr + * @return flex1 + * + * TODO + * + * list must not be empty + * + * @usage list::reduce (integer x, integer y, [2, 4, 6], { return x < y ? x : y; }) -> 2 + * @usage list::reduce (integer x, integer y, [2, 4, 6], { return x < y ? x : y; }) -> 6 + */ + + if (list.isNull()) + { + return YCPNull(); + } + + if (list->size() < 1) + { + ycp2error("Empty list %s for 'reduce'", list->toString().c_str()); + return YCPNull(); + } + + SymbolEntryPtr xs = x->asEntry()->entry(); + SymbolEntryPtr ys = y->asEntry()->entry(); + + YCPValue ret = list->value(0); + + for (int i = 1; i < list->size(); i++) + { + xs->setValue(ret); + ys->setValue(list->value(i)); + + YCPValue tmp = expr->evaluate(); + if (tmp.isNull()) + { + ycp2error("Bad 'reduce' expression %s", expr->toString().c_str()); + continue; + } + if (tmp->isVoid()) + { + ycp2error("The expression for 'reduce' returned 'nil'"); + continue; + } + if (tmp->isBreak()) + { + break; + } + + ret = tmp; + } + + return ret; +} + + +static YCPValue +l_reduce2 (const YCPSymbol &x, const YCPSymbol &y, const YCPValue &initial, const YCPList &list, const YCPCode &expr) +{ + /** + * @builtin list::reduce + * @id reduce_2 + * @short Reduces a list to a single value. + * @param flex1 x + * @param flex2 y + * @param flex1 value + * @param list<flex2> list + * @param block<flex1> expr + * @return flex1 + * + * TODO + * + * list can be empty + * + * @usage list::reduce (integer x, integer y, 0, [2, 4, 6], { return x + y; }) -> 12 + * @usage list::reduce (integer x, integer y, 1, [2, 4, 6], { return x * y; }) -> 48 + * + * different types + * + * @usage list::reduce (term t, float f, `item(`id(`dummy)), [3.14, 2.71], { return add(t, tostring(f)); }) -> `item (`id (`dummy), "3.14", "2.71") + */ + + if (list.isNull()) + { + return YCPNull(); + } + + SymbolEntryPtr xs = x->asEntry()->entry(); + SymbolEntryPtr ys = y->asEntry()->entry(); + + YCPValue ret = initial; + + for (int i = 0; i < list->size(); i++) + { + xs->setValue(ret); + ys->setValue(list->value(i)); + + YCPValue tmp = expr->evaluate(); + if (tmp.isNull()) + { + ycp2error("Bad 'reduce' expression %s", expr->toString().c_str()); + continue; + } + if (tmp->isVoid()) + { + ycp2error("The expression for 'reduce' returned 'nil'"); + continue; + } + if (tmp->isBreak()) + { + break; + } + + ret = tmp; + } + + return ret; +} + + +static YCPValue l_tolist (const YCPValue &v) { /** @@ -1143,5 +1272,14 @@ { 0 } }; + // must be static, registerDeclarations saves a pointer to it! + static declaration_t declarations_ns[] = { + { "list", "", NULL, DECL_NAMESPACE }, + { "reduce", "flex1 (variable <flex1>, variable <flex1>, const list <flex1>, const block <flex1>)", (void *)l_reduce1, DECL_LOOP|DECL_SYMBOL|DECL_FLEX }, + { "reduce", "flex1 (variable <flex1>, variable <flex2>, const flex1, const list <flex2>, const block <flex1>)", (void *)l_reduce2, DECL_LOOP|DECL_SYMBOL|DECL_FLEX }, + { 0 } + }; + static_declarations.registerDeclarations ("YCPBuiltinList", declarations); + static_declarations.registerDeclarations ("YCPBuiltinList", declarations_ns); } Modified: trunk/core/libycp/testsuite/tests/builtin/Builtin-List.err URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/libycp/testsuite/tests/builtin/Builtin-List.err?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/libycp/testsuite/tests/builtin/Builtin-List.err (original) +++ trunk/core/libycp/testsuite/tests/builtin/Builtin-List.err Fri Jun 20 15:32:06 2008 @@ -133,3 +133,49 @@ ---------------------------------------------------------------------- sublist (["a", "b", "c"], 2, 1) ---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +"** list::reduce **" +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, [], { return (x + y); }) +---------------------------------------------------------------------- +[Interpreter] tests/builtin/Builtin-List.ycp:73 Empty list [] for 'reduce' +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, 0, [], { return (x + y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, [2, 4, 6], { return (x + y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, 0, [2, 4, 6], { return (x + y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, [], { return (x * y); }) +---------------------------------------------------------------------- +[Interpreter] tests/builtin/Builtin-List.ycp:78 Empty list [] for 'reduce' +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, 1, [], { return (x * y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, [2, 4, 6], { return (x * y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (integer x, integer y, 1, [2, 4, 6], { return (x * y); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (term t, float f, `item (`id (`dummy)), [], { return add (t, tostring (f)); }) +---------------------------------------------------------------------- +Parsed: +---------------------------------------------------------------------- +list::reduce (term t, float f, `item (`id (`dummy)), [3.14, 2.71], { return add (t, tostring (f)); }) +---------------------------------------------------------------------- Modified: trunk/core/libycp/testsuite/tests/builtin/Builtin-List.out URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/libycp/testsuite/tests/builtin/Builtin-List.out?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/libycp/testsuite/tests/builtin/Builtin-List.out (original) +++ trunk/core/libycp/testsuite/tests/builtin/Builtin-List.out Fri Jun 20 15:32:06 2008 @@ -28,3 +28,14 @@ (["b", "c"]) ([]) (["c"]) +("** list::reduce **") +(nil) +(0) +(12) +(12) +(nil) +(1) +(48) +(48) +(`item (`id (`dummy))) +(`item (`id (`dummy), "3.14", "2.71")) Modified: trunk/core/libycp/testsuite/tests/builtin/Builtin-List.ycp URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/libycp/testsuite/tests/builtin/Builtin-List.ycp?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/libycp/testsuite/tests/builtin/Builtin-List.ycp (original) +++ trunk/core/libycp/testsuite/tests/builtin/Builtin-List.ycp Fri Jun 20 15:32:06 2008 @@ -67,3 +67,19 @@ (sublist (["a", "b", "c"], 2, 0)) (sublist (["a", "b", "c"], 2, 1)) + +("** list::reduce **") + +(list::reduce (integer x, integer y, [], { return x + y; })) // error: no initial value and empty list +(list::reduce (integer x, integer y, 0, [], { return x + y; })) +(list::reduce (integer x, integer y, [2, 4, 6], { return x + y; })) +(list::reduce (integer x, integer y, 0, [2, 4, 6], { return x + y; })) + +(list::reduce (integer x, integer y, [], { return x * y; })) // error: no initial value and empty list +(list::reduce (integer x, integer y, 1, [], { return x * y; })) +(list::reduce (integer x, integer y, [2, 4, 6], { return x * y; })) +(list::reduce (integer x, integer y, 1, [2, 4, 6], { return x * y; })) + +(list::reduce (term t, float f, `item(`id(`dummy)), [], { return add(t, tostring(f)); })) +(list::reduce (term t, float f, `item(`id(`dummy)), [3.14, 2.71], { return add(t, tostring(f)); })) + Modified: trunk/core/package/yast2-core.changes URL: http://svn.opensuse.org/viewcvs/yast/trunk/core/package/yast2-core.changes?rev=48453&r1=48452&r2=48453&view=diff ============================================================================== --- trunk/core/package/yast2-core.changes (original) +++ trunk/core/package/yast2-core.changes Fri Jun 20 15:32:06 2008 @@ -1,7 +1,14 @@ ------------------------------------------------------------------- +Fri Jun 20 15:29:36 CEST 2008 - aschnell@suse.de + +- Added list::reduce builtin. +- 2.17.1 + +------------------------------------------------------------------- Fri Jun 13 10:10:07 CEST 2008 - aschnell@suse.de -- Added float::abs, float::ceil, float::floor, float::trunc, float::pow. +- Added float::abs, float::ceil, float::floor, float::trunc and + float::pow. - 2.17.0 ------------------------------------------------------------------- -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org