Gentoo Archives: gentoo-commits

From: "Petteri Räty" <betelgeuse@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/, test/
Date: Wed, 30 Mar 2011 14:01:11
Message-Id: ae0f0d679f455f3529fc105afd3202bfd77103d7.betelgeuse@gentoo
1 commit: ae0f0d679f455f3529fc105afd3202bfd77103d7
2 Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
3 AuthorDate: Wed Mar 30 13:20:39 2011 +0000
4 Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
5 CommitDate: Wed Mar 30 13:47:49 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=ae0f0d67
7
8 Return the value of arithmetic assignment expression
9
10 The return value of arithmetic assignment expression should be
11 equal to the new value of the variable that got assigned.
12
13 ---
14 bashast/libbashWalker.g | 25 ++++++++++++++-----------
15 src/core/interpreter.h | 7 +++++--
16 test/walker_test.cpp | 40 ++++++++++++++++++++++------------------
17 3 files changed, 41 insertions(+), 31 deletions(-)
18
19 diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
20 index f7ed4ab..fc65689 100644
21 --- a/bashast/libbashWalker.g
22 +++ b/bashast/libbashWalker.g
23 @@ -79,36 +79,39 @@ arithmetics returns[int value]
24 |^(PRE_DECR libbash_name=name){ $value = walker->pre_decr(libbash_name); }
25 |^(POST_INCR libbash_name=name){ $value = walker->post_incr(libbash_name); }
26 |^(POST_DECR libbash_name=name){ $value = walker->post_decr(libbash_name); }
27 - |^(EQUALS libbash_name=name l=arithmetics) { walker->set_value(libbash_name, l); }
28 + |^(EQUALS libbash_name=name l=arithmetics) {
29 + walker->set_value(libbash_name, l);
30 + $value = l;
31 + }
32 |^(MUL_ASSIGN libbash_name=name l=arithmetics) {
33 - walker->assign(&interpreter::multiply, libbash_name, l);
34 + $value = walker->assign(&interpreter::multiply, libbash_name, l);
35 }
36 |^(DIVIDE_ASSIGN libbash_name=name l=arithmetics) {
37 - walker->assign(&interpreter::divide, libbash_name, l);
38 + $value = walker->assign(&interpreter::divide, libbash_name, l);
39 }
40 |^(MOD_ASSIGN libbash_name=name l=arithmetics) {
41 - walker->assign(&interpreter::mod, libbash_name, l);
42 + $value = walker->assign(&interpreter::mod, libbash_name, l);
43 }
44 |^(PLUS_ASSIGN libbash_name=name l=arithmetics) {
45 - walker->assign(&interpreter::plus, libbash_name, l);
46 + $value = walker->assign(&interpreter::plus, libbash_name, l);
47 }
48 |^(MINUS_ASSIGN libbash_name=name l=arithmetics) {
49 - walker->assign(&interpreter::minus, libbash_name, l);
50 + $value = walker->assign(&interpreter::minus, libbash_name, l);
51 }
52 |^(LSHIFT_ASSIGN libbash_name=name l=arithmetics) {
53 - walker->assign(&interpreter::left_shift, libbash_name, l);
54 + $value = walker->assign(&interpreter::left_shift, libbash_name, l);
55 }
56 |^(RSHIFT_ASSIGN libbash_name=name l=arithmetics) {
57 - walker->assign(&interpreter::right_shift, libbash_name, l);
58 + $value = walker->assign(&interpreter::right_shift, libbash_name, l);
59 }
60 |^(AND_ASSIGN libbash_name=name l=arithmetics) {
61 - walker->assign(&interpreter::bitwiseand, libbash_name, l);
62 + $value = walker->assign(&interpreter::bitwiseand, libbash_name, l);
63 }
64 |^(XOR_ASSIGN libbash_name=name l=arithmetics) {
65 - walker->assign(&interpreter::bitwisexor, libbash_name, l);
66 + $value = walker->assign(&interpreter::bitwisexor, libbash_name, l);
67 }
68 |^(OR_ASSIGN libbash_name=name l=arithmetics) {
69 - walker->assign(&interpreter::bitwiseor, libbash_name, l);
70 + $value = walker->assign(&interpreter::bitwiseor, libbash_name, l);
71 }
72 | NUMBER { $value = walker->parse_int($NUMBER);}
73 | DIGIT { $value = walker->parse_int($DIGIT);}
74
75 diff --git a/src/core/interpreter.h b/src/core/interpreter.h
76 index 81e0837..6c99815 100644
77 --- a/src/core/interpreter.h
78 +++ b/src/core/interpreter.h
79 @@ -294,9 +294,12 @@ public:
80 /// \param a function object to do an operation while assigning
81 /// \param the name of the variable
82 /// \param the value to assign
83 - void assign(std::function<int(int,int)> f, const std::string& name, int value)
84 + /// \return the new value of the variable
85 + int assign(std::function<int(int,int)> f, const std::string& name, int value)
86 {
87 - set_value(name,f(resolve<int>(name), value));
88 + int new_value = f(resolve<int>(name), value);
89 + set_value(name, new_value);
90 + return new_value;
91 }
92
93 /// \brief resolve any variable
94
95 diff --git a/test/walker_test.cpp b/test/walker_test.cpp
96 index 66ae75d..41720ad 100644
97 --- a/test/walker_test.cpp
98 +++ b/test/walker_test.cpp
99 @@ -66,10 +66,14 @@ public:
100 return treePsr->arithmetics(treePsr);
101 }
102
103 - int get_arithmetic_variable(const char* script, const string& name)
104 + void check_arithmetic_assignment(const char* script,
105 + const string& name,
106 + int exp_value)
107 {
108 - run_arithmetic(script);
109 - return walker->resolve<int>(name);
110 + // the return value of the arithmetic expression should be equal to
111 + // the new value of the variable
112 + EXPECT_EQ(exp_value, run_arithmetic(script));
113 + EXPECT_EQ(exp_value, walker->resolve<int>(name));
114 }
115 };
116
117 @@ -162,19 +166,19 @@ TEST_BINARY_ARITHMETIC(complex_cal, "10*(2+5)<<3%2**5", 560)
118 TEST_BINARY_ARITHMETIC(complex_cal2, "10*${value}<<3%2**5", 8000)
119 TEST_BINARY_ARITHMETIC(complex_cal3, "(20&5|3||1*100-20&5*10)+~(2*5)", -10)
120
121 -#define TEST_INT_VARIABLE_VALUE(name, script, var_name, exp_value)\
122 +#define TEST_ARITHMETIC_ASSIGNMENT(name, script, var_name, exp_value)\
123 TEST_F(walker_test, name) \
124 - { EXPECT_EQ(exp_value, get_arithmetic_variable(script, var_name)); }
125 -
126 -TEST_INT_VARIABLE_VALUE(assignment, "new_var=10", "new_var", 10)
127 -TEST_INT_VARIABLE_VALUE(assignment2, "value=10+5/2", "value", 12)
128 -TEST_INT_VARIABLE_VALUE(mul_assignment, "value*=10", "value", 1000)
129 -TEST_INT_VARIABLE_VALUE(divide_assignment, "value/=10", "value", 10)
130 -TEST_INT_VARIABLE_VALUE(mod_assignment, "value%=9", "value", 1)
131 -TEST_INT_VARIABLE_VALUE(plus_assignment, "value+=10", "value", 110)
132 -TEST_INT_VARIABLE_VALUE(minus_assignment, "value-=10", "value", 90)
133 -TEST_INT_VARIABLE_VALUE(left_shift_assignment, "value<<=2", "value", 400)
134 -TEST_INT_VARIABLE_VALUE(right_shift_assignment, "value>>=2", "value", 25)
135 -TEST_INT_VARIABLE_VALUE(and_assignment, "value&=10", "value", 0)
136 -TEST_INT_VARIABLE_VALUE(xor_assignment, "value^=5", "value", 97)
137 -TEST_INT_VARIABLE_VALUE(or_assignment, "value|=10", "value", 110)
138 + { check_arithmetic_assignment(script, var_name, exp_value); }
139 +
140 +TEST_ARITHMETIC_ASSIGNMENT(assignment, "new_var=10", "new_var", 10)
141 +TEST_ARITHMETIC_ASSIGNMENT(assignment2, "value=10+5/2", "value", 12)
142 +TEST_ARITHMETIC_ASSIGNMENT(mul_assignment, "value*=10", "value", 1000)
143 +TEST_ARITHMETIC_ASSIGNMENT(divide_assignment, "value/=10", "value", 10)
144 +TEST_ARITHMETIC_ASSIGNMENT(mod_assignment, "value%=9", "value", 1)
145 +TEST_ARITHMETIC_ASSIGNMENT(plus_assignment, "value+=10", "value", 110)
146 +TEST_ARITHMETIC_ASSIGNMENT(minus_assignment, "value-=10", "value", 90)
147 +TEST_ARITHMETIC_ASSIGNMENT(left_shift_assignment, "value<<=2", "value", 400)
148 +TEST_ARITHMETIC_ASSIGNMENT(right_shift_assignment, "value>>=2", "value", 25)
149 +TEST_ARITHMETIC_ASSIGNMENT(and_assignment, "value&=10", "value", 0)
150 +TEST_ARITHMETIC_ASSIGNMENT(xor_assignment, "value^=5", "value", 97)
151 +TEST_ARITHMETIC_ASSIGNMENT(or_assignment, "value|=10", "value", 110)