* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/, src/core/tests/
@ 2011-03-30 14:00 Petteri Räty
0 siblings, 0 replies; only message in thread
From: Petteri Räty @ 2011-03-30 14:00 UTC (permalink / raw
To: gentoo-commits
commit: b68e0e6715cbb89c2bc1e07da5ada9e61551d5a9
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed Mar 30 13:48:49 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed Mar 30 13:48:49 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=b68e0e67
Return the new value when setting a variable's value
This will make set_value work the same way as assign.
---
bashast/libbashWalker.g | 3 +--
src/core/interpreter.h | 13 +++----------
src/core/tests/interpreter_test.cpp | 8 ++++----
3 files changed, 8 insertions(+), 16 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index fc65689..9231d63 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -80,8 +80,7 @@ arithmetics returns[int value]
|^(POST_INCR libbash_name=name){ $value = walker->post_incr(libbash_name); }
|^(POST_DECR libbash_name=name){ $value = walker->post_decr(libbash_name); }
|^(EQUALS libbash_name=name l=arithmetics) {
- walker->set_value(libbash_name, l);
- $value = l;
+ $value = walker->set_value(libbash_name, l);
}
|^(MUL_ASSIGN libbash_name=name l=arithmetics) {
$value = walker->assign(&interpreter::multiply, libbash_name, l);
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 6c99815..1da370c 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -319,23 +319,16 @@ public:
/// it's readonly, will define the variable if it doesn't exist
/// \param variable name
/// \param new value
- /// \return the original value of the variable, will call default
- /// constructor if the variable doesn't exist
+ /// \return the new value of the variable
template <typename T>
- T set_value(const std::string& name, const T& new_value)
+ const T& set_value(const std::string& name, const T& new_value)
{
std::shared_ptr<symbol> value = members.resolve(name);
- T old_value{};
if(!value)
- {
define(name, new_value, false);
- }
else
- {
- old_value = std::static_pointer_cast<variable>(value)->get_value<T>();
std::static_pointer_cast<variable>(value)->set_value(new_value);
- }
- return old_value;
+ return new_value;
}
/// \brief define a new variable
diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
index b183b0b..e5976e5 100644
--- a/src/core/tests/interpreter_test.cpp
+++ b/src/core/tests/interpreter_test.cpp
@@ -48,9 +48,9 @@ TEST(interpreter, set_int_value)
{
interpreter walker;
walker.define("aint", 4);
- EXPECT_EQ(4, walker.set_value("aint", 10));
+ EXPECT_EQ(10, walker.set_value("aint", 10));
EXPECT_EQ(10, walker.resolve<int>("aint"));
- EXPECT_EQ(0, walker.set_value("undefined", 10));
+ EXPECT_EQ(10, walker.set_value("undefined", 10));
EXPECT_EQ(10, walker.resolve<int>("undefined"));
walker.define("aint_ro", 4, true);
@@ -63,9 +63,9 @@ TEST(interpreter, set_string_value)
{
interpreter walker;
walker.define("astring", "hi");
- EXPECT_STREQ("hi", walker.set_value<string>("astring", "hello").c_str());
+ EXPECT_STREQ("hello", walker.set_value<string>("astring", "hello").c_str());
EXPECT_STREQ("hello", walker.resolve<string>("astring").c_str());
- EXPECT_STREQ("", walker.set_value<string>("undefined", "hello").c_str());
+ EXPECT_STREQ("hello", walker.set_value<string>("undefined", "hello").c_str());
EXPECT_STREQ("hello", walker.resolve<string>("undefined").c_str());
walker.define("astring_ro", "hi", true);
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2011-03-30 14:01 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-30 14:00 [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/, src/core/tests/ Petteri Räty
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox