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 1QFKa3-0003Z3-7J for garchives@archives.gentoo.org; Thu, 28 Apr 2011 06:20:03 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id B4A4C1C048; Thu, 28 Apr 2011 06:19:55 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 708791C048 for ; Thu, 28 Apr 2011 06:19:55 +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 E27312AC014 for ; Thu, 28 Apr 2011 06:19:54 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id CDC7F80509 for ; Thu, 28 Apr 2011 06:19:53 +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: 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.result src/core/interpreter.cpp src/core/interpreter.h src/core/symbols.hpp src/core/tests/interpreter_test.cpp src/core/tests/symbols_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: be5289bca55b033742300ed0f5f69121b2bb76c4 Date: Thu, 28 Apr 2011 06:19:53 +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: 626e246176625226427ecf62c03bb675 commit: be5289bca55b033742300ed0f5f69121b2bb76c4 Author: Mu Qiao gentoo org> AuthorDate: Thu Apr 21 11:58:16 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Thu Apr 28 02:57:53 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3Dbe5289bc Core: support unsetting variables, fix is_null Empty variable values are all treated as null value. This simplifies the previous code and fixes some logic errors. --- bashast/libbashWalker.g | 5 +-- scripts/var_def.bash.result | 4 +- src/core/interpreter.cpp | 21 ++++++++++++++++++ src/core/interpreter.h | 19 +++++++++++----- src/core/symbols.hpp | 40 +++++++++++++++++++++++++----= ----- src/core/tests/interpreter_test.cpp | 29 ++++++++++++++++++++++++- src/core/tests/symbols_test.cpp | 12 ++++++++- 7 files changed, 105 insertions(+), 25 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index e278d1b..203cd28 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -119,10 +119,9 @@ var_def @declarations { std::map values; unsigned index =3D 0; - bool is_null =3D true; } - :^(EQUALS name (string_expr { is_null =3D false; })?) { - walker->set_value($name.libbash_value, $string_expr.libbash_value, $na= me.index, is_null); + :^(EQUALS name string_expr?) { + walker->set_value($name.libbash_value, $string_expr.libbash_value, $na= me.index); } |^(EQUALS libbash_name=3Dname_base ^(ARRAY ( (expr=3Dstring_expr diff --git a/scripts/var_def.bash.result b/scripts/var_def.bash.result index e840ebe..1b9e287 100644 --- a/scripts/var_def.bash.result +++ b/scripts/var_def.bash.result @@ -5,8 +5,8 @@ $- has not been implemented yet $! has not been implemented yet ARRAY01=3D1 2 3 4 5 ARRAY02=3D1 2 4 5 -ARRAY03=3D2 3 -ARRAY04=3D2 3 +ARRAY03=3D 2 3 +ARRAY04=3D 2 3 ARRAY05=3D1 2 3 4 5 ARRAY06=3D1 2 3 4 5 ARRAY07=3D1 2 3 4 5 diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp index 9548e8a..f0bf61e 100644 --- a/src/core/interpreter.cpp +++ b/src/core/interpreter.cpp @@ -178,3 +178,24 @@ void interpreter::get_all_function_names(std::vector= & function_name { boost::copy(functions | boost::adaptors::map_keys, back_inserter(funct= ion_names)); } + +void interpreter::unset(const std::string& name) +{ + auto iter =3D members.find(name); + if(iter =3D=3D members.end()) + return; + else if(iter->second->is_readonly()) + throw interpreter_exception("Can't unset readonly variable " + name)= ; + else + members.erase(name); +} + +void interpreter::unset(const std::string& name, + const unsigned index) +{ + auto iter =3D members.find(name); + if(iter =3D=3D members.end()) + return; + else + iter->second->unset_value(index); +} diff --git a/src/core/interpreter.h b/src/core/interpreter.h index e471b94..dc446fc 100644 --- a/src/core/interpreter.h +++ b/src/core/interpreter.h @@ -468,14 +468,13 @@ public: template const T& set_value(const std::string& name, const T& new_value, - const unsigned index=3D0, - bool is_null=3Dfalse) + const unsigned index=3D0) { auto i =3D members.find(name); if(i =3D=3D members.end()) - define(name, new_value, false, is_null, index); + define(name, new_value, false, index); else - i->second->set_value(new_value, index, is_null); + i->second->set_value(new_value, index); return new_value; } =20 @@ -494,6 +493,15 @@ public: return resolve("?"); } =20 + /// \brief unset a variable + /// \param the name of the variable + void unset(const std::string& name); + + /// \brief unset a array member + /// \param the name of the array + /// \param the index of the member + void unset(const std::string& name, const unsigned index); + /// \brief define a new global variable /// \param the name of the variable /// \param the value of the variable @@ -503,11 +511,10 @@ public: void define(const std::string& name, const T& value, bool readonly=3Dfalse, - bool is_null=3Dfalse, const unsigned index=3D0) { std::shared_ptr target( - new variable(name, value, readonly, is_null, index)); + new variable(name, value, readonly, index)); members[name] =3D target; } =20 diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp index b11d338..89fd657 100644 --- a/src/core/symbols.hpp +++ b/src/core/symbols.hpp @@ -137,12 +137,10 @@ public: variable(const std::string& name, const T& v, bool ro=3Dfalse, - bool is_null=3Dfalse, const unsigned index=3D0) : name(name), readonly(ro) { - if(!is_null) - value[index] =3D v; + value[index] =3D v; } =20 /// \brief retrieve actual value of the variable, if index is out of b= ound, @@ -182,16 +180,22 @@ public: /// \param whether to set the variable to null value, default is false template void set_value(const T& new_value, - const unsigned index=3D0, - bool is_null=3Dfalse) + const unsigned index=3D0) { if(readonly) throw interpreter_exception(get_name() + " is readonly variable"); =20 - if(is_null) - value.erase(index); - else - value[index] =3D new_value; + value[index] =3D new_value; + } + + /// \brief unset the variable, only used for array variable + /// \param the index to be unset + void unset_value(const unsigned index) + { + if(readonly) + throw interpreter_exception(get_name() + " is readonly variable"); + + value.erase(index); } =20 /// \brief get the length of a variable @@ -211,10 +215,24 @@ 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 unsigned index=3D0) const + bool is_unset(const unsigned index=3D0) const { return value.find(index) =3D=3D value.end(); } + + /// \brief check whether the value of the variable is unset + /// \return whether the value of the variable is unset + bool is_null(const unsigned index=3D0) const + { + return get_value(index) =3D=3D ""; + } + + /// \brief check whether the value of the variable is readonly + /// \return whether the value of the variable is readonly + bool is_readonly() const + { + return readonly; + } }; =20 // specialization for arrays @@ -222,7 +240,7 @@ template <> inline variable::variable<>(const std::string& name, const std::map& v, bool ro, - bool, unsigned) + unsigned) : name(name), value(v.begin(), v.end()), readonly(ro) { } diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpr= eter_test.cpp index 2e92a22..54f25b0 100644 --- a/src/core/tests/interpreter_test.cpp +++ b/src/core/tests/interpreter_test.cpp @@ -54,7 +54,7 @@ TEST(interpreter, define_resolve_array) EXPECT_STREQ("3", walker.resolve("array", 2).c_str()); EXPECT_STREQ("", walker.resolve("undefined",100).c_str()); =20 - walker.define("partial", 10, false, false, 8); + walker.define("partial", 10, false, 8); EXPECT_EQ(1, walker.get_array_length("partial")); EXPECT_EQ(10, walker.resolve("partial", 8)); } @@ -142,6 +142,33 @@ TEST(interpreter, get_array_values) EXPECT_EQ(3, array_values[2]); } =20 +TEST(interpreter, unset_values) +{ + interpreter walker; + std::map values =3D {{0, "1"}, {1, "2"}, {2, "3"}}; + walker.define("array", values); + walker.define("ro_array", values, true); + walker.define("var", "123"); + walker.define("ro_var", "123", true); + + EXPECT_STREQ("2", walker.resolve("array", 1).c_str()); + walker.unset("array", 1); + EXPECT_STREQ("", walker.resolve("array", 1).c_str()); + walker.unset("array"); + EXPECT_STREQ("", walker.resolve("array", 0).c_str()); + EXPECT_STREQ("", walker.resolve("array", 1).c_str()); + EXPECT_STREQ("", walker.resolve("array", 2).c_str()); + + EXPECT_THROW(walker.unset("ro_array", 1), interpreter_exception); + EXPECT_THROW(walker.unset("ro_array"), interpreter_exception); + + EXPECT_STREQ("123", walker.resolve("var").c_str()); + walker.unset("var"); + EXPECT_STREQ("", walker.resolve("var").c_str()); + + EXPECT_THROW(walker.unset("ro_var"), interpreter_exception); +} + TEST(interperter, substring_expansion_exception) { interpreter walker; diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_tes= t.cpp index d614947..f151318 100644 --- a/src/core/tests/symbols_test.cpp +++ b/src/core/tests/symbols_test.cpp @@ -122,9 +122,17 @@ TEST(symbol_test, is_null) { variable var("foo", 10); EXPECT_FALSE(var.is_null()); - var.set_value("bar", 0, true); + var.set_value(""); EXPECT_TRUE(var.is_null()); - EXPECT_TRUE(variable("foo", "", false, true).is_null()); + EXPECT_TRUE(variable("foo", "").is_null()); +} + +TEST(symbol_test, is_unset) +{ + map values =3D {{0, "1"}, {1, "2"}, {2, "3"}}; + variable array("foo", values); + array.unset_value(1); + EXPECT_TRUE(array.is_unset(1)); } =20 TEST(symbol_test, get_length)