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 1QAEVh-0001RS-SK for garchives@archives.gentoo.org; Thu, 14 Apr 2011 04:50:30 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 796151C024; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 106721C01F for ; Thu, 14 Apr 2011 04:50:15 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 5EC261B4156 for ; Thu, 14 Apr 2011 04:50:15 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 0C84880072 for ; Thu, 14 Apr 2011 04:50:14 +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: <60fe984fb2c868848a8fb71fe2272a24a78d350a.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_expansion.ebuild scripts/var_expansion.ebuild.result 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: 60fe984fb2c868848a8fb71fe2272a24a78d350a Date: Thu, 14 Apr 2011 04:50:14 +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: X-Archives-Hash: 8060a70ccc404a15ba7b7638f6d5e468 commit: 60fe984fb2c868848a8fb71fe2272a24a78d350a Author: Mu Qiao gentoo org> AuthorDate: Tue Apr 12 13:44:06 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Wed Apr 13 02:46:03 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D60fe984f Implement array element reference for variable expansion --- bashast/libbashWalker.g | 21 +++++++++++------ scripts/var_expansion.ebuild | 13 +++++++++++ scripts/var_expansion.ebuild.result | 14 ++++++++++++ src/core/interpreter.h | 41 ++++++++++++++++++++++++-----= ----- src/core/symbols.hpp | 23 +++++++++---------- src/core/tests/interpreter_test.cpp | 13 ++++++++-- 6 files changed, 90 insertions(+), 35 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 45034c2..ddeffa0 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -116,23 +116,28 @@ var_name returns[std::string libbash_value, unsigne= d index] =20 var_expansion returns[std::string libbash_value]: ^(USE_DEFAULT var_name libbash_word=3Dword) { - libbash_value =3D walker->do_default_expansion($var_name.libbash_value= , libbash_word); + libbash_value =3D walker->do_default_expansion($var_name.libbash_value= , libbash_word, $var_name.index); } |^(ASSIGN_DEFAULT var_name libbash_word=3Dword) { - libbash_value =3D walker->do_assign_expansion($var_name.libbash_value,= libbash_word); + libbash_value =3D walker->do_assign_expansion($var_name.libbash_value,= libbash_word, $var_name.index); } |^(USE_ALTERNATE var_name libbash_word=3Dword) { - libbash_value =3D walker->do_alternate_expansion($var_name.libbash_val= ue, libbash_word); + libbash_value =3D walker->do_alternate_expansion($var_name.libbash_val= ue, libbash_word, $var_name.index); } |(^(OFFSET var_name arithmetics arithmetics)) =3D> ^(OFFSET var_name of= fset=3Darithmetics length=3Darithmetics) { - libbash_value =3D walker->do_substring_expansion($var_name.libbash_val= ue, offset, length); + libbash_value =3D walker->do_substring_expansion($var_name.libbash_val= ue, offset, length, $var_name.index); } |^(OFFSET var_name offset=3Darithmetics) { - libbash_value =3D walker->do_substring_expansion($var_name.libbash_val= ue, offset); - } - |^(POUND var_name) { - libbash_value =3D boost::lexical_cast(walker->get_length(= $var_name.libbash_value)); - }; + libbash_value =3D walker->do_substring_expansion($var_name.libbash_val= ue, offset, $var_name.index); + } + |^(POUND( + var_name { + libbash_value =3D boost::lexical_cast(walker->get_length= ($var_name.libbash_value, $var_name.index)); + } + |^(libbash_name=3Dname_base ARRAY_SIZE) { + libbash_value =3D boost::lexical_cast(walker->get_array_= length(libbash_name)); + } + )); =20 word returns[std::string libbash_value]: |(num) =3D> libbash_string=3Dnum { $libbash_value =3D libbash_string; } diff --git a/scripts/var_expansion.ebuild b/scripts/var_expansion.ebuild index 0045b5e..96b6bf2 100644 --- a/scripts/var_expansion.ebuild +++ b/scripts/var_expansion.ebuild @@ -1,3 +1,4 @@ +ARRAY=3D(hi hello 1 2 3) EAPI=3D"3" EAPI4=3D"$(($EAPI+1))" FOO001=3D"${EAPI:-hello}" @@ -24,3 +25,15 @@ FOO022=3D${FOO009: -2:100} FOO023=3D${NOT_EXIST:0:2} FOO024=3D${#FOO009} FOO025=3D${#NOT_EXIST} +FOO026=3D"${ARRAY[0]:-hello}" +FOO028=3D"${ARRAY[5]:-hello}" +FOO029=3D"${ARRAY2[0]:=3Dhello}" +FOO030=3D"${ARRAY2[0]:=3Dhi}" +FOO031=3D"${ARRAY2[0]:+hi}" +FOO032=3D"${ARRAY2[1]:+hi}" +FOO033=3D"${ARRAY[1]:1}" +FOO034=3D"${ARRAY[1]:1:3}" +FOO035=3D"${#ARRAY[0]}" +FOO036=3D"${#ARRAY[@]}" +FOO037=3D"${#ARRAY[*]}" +FOO038=3D"${#ARRAY}" diff --git a/scripts/var_expansion.ebuild.result b/scripts/var_expansion.= ebuild.result index e7b767a..8bea3b8 100644 --- a/scripts/var_expansion.ebuild.result +++ b/scripts/var_expansion.ebuild.result @@ -1,3 +1,5 @@ +ARRAY=3Dhi hello 1 2 3 +ARRAY2=3Dhello EAPI=3D3 EAPI4=3D4 FOO001=3D3 @@ -25,3 +27,15 @@ FOO022=3Dlo FOO023=3D FOO024=3D5 FOO025=3D0 +FOO026=3Dhi +FOO028=3Dhello +FOO029=3Dhello +FOO030=3Dhello +FOO031=3Dhi +FOO032=3D +FOO033=3Dello +FOO034=3Dell +FOO035=3D2 +FOO036=3D5 +FOO037=3D5 +FOO038=3D2 diff --git a/src/core/interpreter.h b/src/core/interpreter.h index 5c75bf0..9166379 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -392,11 +392,11 @@ public: /// if the variable is undefined /// \param variable name /// \return whether the value of the variable is null - bool is_unset_or_null(const std::string& name) + bool is_unset_or_null(const std::string& name, const unsigned index) { std::shared_ptr value =3D members.resolve(name); if(value) - return value->is_null(); + return value->is_null(index); else return true; } @@ -449,9 +449,11 @@ public: /// \param the value of the word /// \return the expansion result const std::string do_default_expansion(const std::string& name, - const std::string& value) + const std::string& value, + const unsigned index) { - return (is_unset_or_null(name)? value : resolve(name)); + return (is_unset_or_null(name, index)? + value : resolve(name, index)); } =20 /// \brief perform ${parameter:=3Dword} expansion @@ -459,9 +461,11 @@ public: /// \param the value of the word /// \return the expansion result const std::string do_assign_expansion(const std::string& name, - const std::string& value) + const std::string& value, + const unsigned index) { - return (is_unset_or_null(name)? set_value(name, value) : resolve(name)); + return (is_unset_or_null(name, index)? + set_value(name, value, index) : resolve(name, index= )); } =20 /// \brief perform ${parameter:+word} expansion @@ -469,18 +473,20 @@ public: /// \param the value of the word /// \return the expansion result const std::string do_alternate_expansion(const std::string& name, - const std::string& value) + const std::string& value, + const unsigned index) { - return (is_unset_or_null(name)? "" : value); + return (is_unset_or_null(name, index)? "" : value); } =20 /// \brief perform substring expansion /// \param the offset of the substring /// \return the expansion result const std::string do_substring_expansion(const std::string& name, - int offset) + int offset, + const unsigned index) { - std::string value =3D resolve(name); + std::string value =3D resolve(name, index); if(!get_real_offset(offset, value)) return ""; return value.substr(offset); @@ -492,11 +498,12 @@ public: /// \return the expansion result const std::string do_substring_expansion(const std::string& name, int offset, - int length) + int length, + const unsigned index) { if(length < 0) throw interpreter_exception("length of substring expression should= be greater or equal to zero"); - std::string value =3D resolve(name); + std::string value =3D resolve(name, index); if(!get_real_offset(offset, value)) return ""; return value.substr(offset, length); @@ -513,5 +520,15 @@ public: return value->get_length(index); } =20 + /// \brief get the length of an array + /// \param the name of the array + /// \return the length of the array + unsigned get_array_length(const std::string& name) + { + std::shared_ptr value =3D members.resolve(name); + if(!value) + return 0; + return value->get_array_length(); + } }; #endif diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index 5992a67..7654ebe 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -125,10 +125,6 @@ class variable /// \brief whether the variable is readonly bool readonly; =20 - /// \var private::null_value - /// \brief whether the variable is null - bool null_value; - public: /// \brief retrieve variable name /// \return const string value of variable name @@ -142,9 +138,10 @@ public: const T& v, bool ro=3Dfalse, bool is_null=3Dfalse) - : name(name), readonly(ro), null_value(is_null) + : name(name), readonly(ro) { - value[0] =3D v; + if(!is_null) + value[0] =3D v; } =20 /// \brief retrieve actual value of the variable, if index is out of b= ound, @@ -190,8 +187,10 @@ public: if(readonly) throw interpreter_exception(get_name() + " is readonly variable"); =20 - null_value =3D is_null; - value[index] =3D new_value; + if(is_null) + value.erase(index); + else + value[index] =3D new_value; } =20 /// \brief get the length of a variable @@ -211,9 +210,9 @@ public: =20 /// \brief check whether the value of the variable is null /// \return whether the value of the variable is null - bool is_null() const + bool is_null(const unsigned index=3D0) const { - return null_value; + return value.find(index) =3D=3D value.end(); } }; =20 @@ -222,8 +221,8 @@ template <> inline variable::variable<>(const std::string& name, const std::map& v, bool ro, - bool is_null) - : name(name), value(v.begin(), v.end()), readonly(ro), null_value(is= _null) + bool) + : name(name), value(v.begin(), v.end()), readonly(ro) { } =20 diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpr= eter_test.cpp index 9bb561a..fbf073e 100644 --- a/src/core/tests/interpreter_test.cpp +++ b/src/core/tests/interpreter_test.cpp @@ -59,9 +59,16 @@ TEST(interpreter, is_unset_or_null) { interpreter walker; walker.define("foo", "hello"); - EXPECT_FALSE(walker.is_unset_or_null("foo")); + EXPECT_FALSE(walker.is_unset_or_null("foo", 0)); walker.define("foo", "hello", false, true); - EXPECT_TRUE(walker.is_unset_or_null("foo")); + EXPECT_TRUE(walker.is_unset_or_null("foo", 0)); + + std::map values =3D {{0, "1"}, {1, "2"}, {2, "3"}}; + walker.define("bar", values); + EXPECT_FALSE(walker.is_unset_or_null("bar", 0)); + EXPECT_FALSE(walker.is_unset_or_null("bar", 1)); + EXPECT_FALSE(walker.is_unset_or_null("bar", 2)); + EXPECT_TRUE(walker.is_unset_or_null("bar", 3)); } =20 TEST(interpreter, is_unset) @@ -134,5 +141,5 @@ TEST(interpreter, get_array_values) TEST(interperter, substring_expansion_exception) { interpreter walker; - EXPECT_THROW(walker.do_substring_expansion("", 0, -1), interpreter_exc= eption); + EXPECT_THROW(walker.do_substring_expansion("", 0, -1, 0), interpreter_= exception); }