1 |
commit: b68e0e6715cbb89c2bc1e07da5ada9e61551d5a9 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Mar 30 13:48:49 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Mar 30 13:48:49 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=b68e0e67 |
7 |
|
8 |
Return the new value when setting a variable's value |
9 |
|
10 |
This will make set_value work the same way as assign. |
11 |
|
12 |
--- |
13 |
bashast/libbashWalker.g | 3 +-- |
14 |
src/core/interpreter.h | 13 +++---------- |
15 |
src/core/tests/interpreter_test.cpp | 8 ++++---- |
16 |
3 files changed, 8 insertions(+), 16 deletions(-) |
17 |
|
18 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
19 |
index fc65689..9231d63 100644 |
20 |
--- a/bashast/libbashWalker.g |
21 |
+++ b/bashast/libbashWalker.g |
22 |
@@ -80,8 +80,7 @@ arithmetics returns[int value] |
23 |
|^(POST_INCR libbash_name=name){ $value = walker->post_incr(libbash_name); } |
24 |
|^(POST_DECR libbash_name=name){ $value = walker->post_decr(libbash_name); } |
25 |
|^(EQUALS libbash_name=name l=arithmetics) { |
26 |
- walker->set_value(libbash_name, l); |
27 |
- $value = l; |
28 |
+ $value = walker->set_value(libbash_name, l); |
29 |
} |
30 |
|^(MUL_ASSIGN libbash_name=name l=arithmetics) { |
31 |
$value = walker->assign(&interpreter::multiply, libbash_name, l); |
32 |
|
33 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
34 |
index 6c99815..1da370c 100644 |
35 |
--- a/src/core/interpreter.h |
36 |
+++ b/src/core/interpreter.h |
37 |
@@ -319,23 +319,16 @@ public: |
38 |
/// it's readonly, will define the variable if it doesn't exist |
39 |
/// \param variable name |
40 |
/// \param new value |
41 |
- /// \return the original value of the variable, will call default |
42 |
- /// constructor if the variable doesn't exist |
43 |
+ /// \return the new value of the variable |
44 |
template <typename T> |
45 |
- T set_value(const std::string& name, const T& new_value) |
46 |
+ const T& set_value(const std::string& name, const T& new_value) |
47 |
{ |
48 |
std::shared_ptr<symbol> value = members.resolve(name); |
49 |
- T old_value{}; |
50 |
if(!value) |
51 |
- { |
52 |
define(name, new_value, false); |
53 |
- } |
54 |
else |
55 |
- { |
56 |
- old_value = std::static_pointer_cast<variable>(value)->get_value<T>(); |
57 |
std::static_pointer_cast<variable>(value)->set_value(new_value); |
58 |
- } |
59 |
- return old_value; |
60 |
+ return new_value; |
61 |
} |
62 |
|
63 |
/// \brief define a new variable |
64 |
|
65 |
diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp |
66 |
index b183b0b..e5976e5 100644 |
67 |
--- a/src/core/tests/interpreter_test.cpp |
68 |
+++ b/src/core/tests/interpreter_test.cpp |
69 |
@@ -48,9 +48,9 @@ TEST(interpreter, set_int_value) |
70 |
{ |
71 |
interpreter walker; |
72 |
walker.define("aint", 4); |
73 |
- EXPECT_EQ(4, walker.set_value("aint", 10)); |
74 |
+ EXPECT_EQ(10, walker.set_value("aint", 10)); |
75 |
EXPECT_EQ(10, walker.resolve<int>("aint")); |
76 |
- EXPECT_EQ(0, walker.set_value("undefined", 10)); |
77 |
+ EXPECT_EQ(10, walker.set_value("undefined", 10)); |
78 |
EXPECT_EQ(10, walker.resolve<int>("undefined")); |
79 |
|
80 |
walker.define("aint_ro", 4, true); |
81 |
@@ -63,9 +63,9 @@ TEST(interpreter, set_string_value) |
82 |
{ |
83 |
interpreter walker; |
84 |
walker.define("astring", "hi"); |
85 |
- EXPECT_STREQ("hi", walker.set_value<string>("astring", "hello").c_str()); |
86 |
+ EXPECT_STREQ("hello", walker.set_value<string>("astring", "hello").c_str()); |
87 |
EXPECT_STREQ("hello", walker.resolve<string>("astring").c_str()); |
88 |
- EXPECT_STREQ("", walker.set_value<string>("undefined", "hello").c_str()); |
89 |
+ EXPECT_STREQ("hello", walker.set_value<string>("undefined", "hello").c_str()); |
90 |
EXPECT_STREQ("hello", walker.resolve<string>("undefined").c_str()); |
91 |
|
92 |
walker.define("astring_ro", "hi", true); |