From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Sb6pp-0001VW-3M for garchives@archives.gentoo.org; Sun, 03 Jun 2012 09:10:54 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A2570E0829; Sun, 3 Jun 2012 09:08:44 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 5FE51E07FC for ; Sun, 3 Jun 2012 09:08:44 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 89BC01B4007 for ; Sun, 3 Jun 2012 09:08:43 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 40FB5E5437 for ; Sun, 3 Jun 2012 09:08:41 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: <1332763482.6d95304b292a398ddfd42a2ef1115cf8e94928b5.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g scripts/var_def.bash src/core/interpreter.h src/core/symbols.hpp src/core/tests/interpreter_test.cpp X-VCS-Directories: scripts/ src/core/ bashast/ src/core/tests/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 6d95304b292a398ddfd42a2ef1115cf8e94928b5 X-VCS-Branch: master Date: Sun, 3 Jun 2012 09:08:41 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: b9b77818-2299-4fae-bc10-5b8bf47f791f X-Archives-Hash: c4f33a5120616ea549a2ba6669ceadad commit: 6d95304b292a398ddfd42a2ef1115cf8e94928b5 Author: Mu Qiao gentoo org> AuthorDate: Mon Mar 26 09:34:49 2012 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Mon Mar 26 12:04:42 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D6d95304b Walker: support appending array to array --- bashast/libbashWalker.g | 29 ++++++++++++++++++++++------- scripts/var_def.bash | 8 ++++++++ src/core/interpreter.h | 9 +++++++++ src/core/symbols.hpp | 5 +++++ src/core/tests/interpreter_test.cpp | 9 +++++++++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 5e3fd6e..5d5b557 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -217,19 +217,34 @@ var_def[bool local] else walker->set_value($name.libbash_value, $string_expr.libbash_value, $n= ame.index); } - |^(EQUALS libbash_name=3Dname_base ^(ARRAY ( - (expr=3Dstring_expr - |^(EQUALS value=3Darithmetics { - set_index(libbash_name, index, value); - } expr=3Dstring_expr)) - { values[index++] =3D expr.libbash_value; })* - )){ + |^(EQUALS libbash_name=3Dname_base array_def_helper[libbash_name, value= s, index]){ if(local) walker->define_local(libbash_name, values); else walker->define(libbash_name, values); + } + |^(PLUS_ASSIGN libbash_name=3Dname_base { + index =3D walker->get_max_index(libbash_name) + 1; + if(index =3D=3D 1) // The variable is not defined + index =3D 0; + } array_def_helper[libbash_name, values, index]){ + if(local) + throw libbash::unsupported_exception("Appending array to local variab= le is not supported"); + for(auto iter =3D values.begin(); iter !=3D values.end(); ++iter) + walker->set_value(libbash_name, iter->second, iter->first); }; =20 +array_def_helper[const std::string& libbash_name, std::map& values, unsigned index] + :^(ARRAY ( + ( + expr=3Dstring_expr + |^(EQUALS value=3Darithmetics { + set_index(libbash_name, index, value); + } expr=3Dstring_expr) + ) + { values[index++] =3D expr.libbash_value; } + )*); + string_expr returns[std::string libbash_value, bool quoted] @init { $quoted =3D true; diff --git a/scripts/var_def.bash b/scripts/var_def.bash index e757b16..96747c0 100644 --- a/scripts/var_def.bash +++ b/scripts/var_def.bash @@ -88,3 +88,11 @@ function foo() { foo bar=3D@ echo $bar +ARRAY11=3D(1 2 3 [10]=3D15) +ARRAY11+=3D(1 [15]=3D20) +echo ${ARRAY11[@]} +ARRAY12+=3D(4 5 6) +echo ${ARRAY12[@]} +ARRAY13=3D() +ARRAY13+=3D(4 5 6) +echo ${ARRAY13[@]} diff --git a/src/core/interpreter.h b/src/core/interpreter.h index fcc3294..fbb9d48 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -461,6 +461,15 @@ public: /// \return the length of the array variable::size_type get_array_length(const std::string& name) const; =20 + /// \brief get the max index of an array + /// \param name the name of the array + /// \return the max index of the array + variable::size_type get_max_index(const std::string& name) const + { + auto var =3D resolve_variable(name); + return var ? var->get_max_index() : 0; + } + /// \brief get all array elements concatenated by space /// \param name the name of the array /// \param[out] result the concatenated string diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index 8dd8cd3..e9cb019 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -215,6 +215,11 @@ public: return value.size(); } =20 + size_type get_max_index() const + { + return value.size() =3D=3D 0 ? 0 : value.rbegin()->first; + } + /// \brief check whether the value of the variable is null /// \return whether the value of the variable is null bool is_unset(const unsigned index=3D0) const diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpr= eter_test.cpp index ecbcfd9..c3278d0 100644 --- a/src/core/tests/interpreter_test.cpp +++ b/src/core/tests/interpreter_test.cpp @@ -146,6 +146,15 @@ TEST(interpreter, get_array_values) EXPECT_FALSE(walker.resolve_array("undefined", array_values)); } =20 +TEST(interpreter, get_max_index) +{ + interpreter walker; + std::map values =3D {{0, "1"}, {1, "2"}, {5, "3= "}}; + walker.define("array", values); + + EXPECT_EQ(5, walker.get_max_index("array")); +} + TEST(interpreter, unset_arrays) { interpreter walker;