1 |
commit: 9d9afb2e878d6104f76e958958eec6168923181f |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Mar 28 14:57:56 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Mar 29 12:19:26 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=9d9afb2e |
7 |
|
8 |
Implement arithmetic assignment |
9 |
|
10 |
All the arithmetic assignment operations are supported. |
11 |
|
12 |
--- |
13 |
bashast/bashwalker.g | 40 ++++++++++++++++++++++++++++++++++++++++ |
14 |
test/walker_test.cpp | 10 ++++++++++ |
15 |
2 files changed, 50 insertions(+), 0 deletions(-) |
16 |
|
17 |
diff --git a/bashast/bashwalker.g b/bashast/bashwalker.g |
18 |
index 16d1216..d2c2438 100644 |
19 |
--- a/bashast/bashwalker.g |
20 |
+++ b/bashast/bashwalker.g |
21 |
@@ -80,6 +80,46 @@ arithmetics returns[int value] |
22 |
|^(POST_INCR libbash_name=name){ $value = walker->post_incr(libbash_name); } |
23 |
|^(POST_DECR libbash_name=name){ $value = walker->post_decr(libbash_name); } |
24 |
|^(EQUALS libbash_name=name l=arithmetics) { walker->set_value(libbash_name, l); } |
25 |
+ |^(MUL_ASSIGN libbash_name=name l=arithmetics) { |
26 |
+ walker->set_value(libbash_name, |
27 |
+ walker->multiply(walker->resolve<int>(libbash_name), l)); |
28 |
+ } |
29 |
+ |^(DIVIDE_ASSIGN libbash_name=name l=arithmetics) { |
30 |
+ walker->set_value(libbash_name, |
31 |
+ walker->divide(walker->resolve<int>(libbash_name), l)); |
32 |
+ } |
33 |
+ |^(MOD_ASSIGN libbash_name=name l=arithmetics) { |
34 |
+ walker->set_value(libbash_name, |
35 |
+ walker->mod(walker->resolve<int>(libbash_name), l)); |
36 |
+ } |
37 |
+ |^(PLUS_ASSIGN libbash_name=name l=arithmetics) { |
38 |
+ walker->set_value(libbash_name, |
39 |
+ walker->plus(walker->resolve<int>(libbash_name), l)); |
40 |
+ } |
41 |
+ |^(MINUS_ASSIGN libbash_name=name l=arithmetics) { |
42 |
+ walker->set_value(libbash_name, |
43 |
+ walker->minus(walker->resolve<int>(libbash_name), l)); |
44 |
+ } |
45 |
+ |^(LSHIFT_ASSIGN libbash_name=name l=arithmetics) { |
46 |
+ walker->set_value(libbash_name, |
47 |
+ walker->left_shift(walker->resolve<int>(libbash_name), l)); |
48 |
+ } |
49 |
+ |^(RSHIFT_ASSIGN libbash_name=name l=arithmetics) { |
50 |
+ walker->set_value(libbash_name, |
51 |
+ walker->right_shift(walker->resolve<int>(libbash_name), l)); |
52 |
+ } |
53 |
+ |^(AND_ASSIGN libbash_name=name l=arithmetics) { |
54 |
+ walker->set_value(libbash_name, |
55 |
+ walker->bitwiseand(walker->resolve<int>(libbash_name), l)); |
56 |
+ } |
57 |
+ |^(XOR_ASSIGN libbash_name=name l=arithmetics) { |
58 |
+ walker->set_value(libbash_name, |
59 |
+ walker->bitwisexor(walker->resolve<int>(libbash_name), l)); |
60 |
+ } |
61 |
+ |^(OR_ASSIGN libbash_name=name l=arithmetics) { |
62 |
+ walker->set_value(libbash_name, |
63 |
+ walker->bitwiseor(walker->resolve<int>(libbash_name), l)); |
64 |
+ } |
65 |
| NUMBER { $value = walker->parse_int($NUMBER);} |
66 |
| DIGIT { $value = walker->parse_int($DIGIT);} |
67 |
; |
68 |
|
69 |
diff --git a/test/walker_test.cpp b/test/walker_test.cpp |
70 |
index 0a74b10..98672aa 100644 |
71 |
--- a/test/walker_test.cpp |
72 |
+++ b/test/walker_test.cpp |
73 |
@@ -168,3 +168,13 @@ TEST_BINARY_ARITHMETIC(complex_cal3, "(20&5|3||1*100-20&5*10)+~(2*5)", -1 |
74 |
|
75 |
TEST_INT_VARIABLE_VALUE(assignment, "new_var=10", "new_var", 10) |
76 |
TEST_INT_VARIABLE_VALUE(assignment2, "value=10+5/2", "value", 12) |
77 |
+TEST_INT_VARIABLE_VALUE(mul_assignment, "value*=10", "value", 1000) |
78 |
+TEST_INT_VARIABLE_VALUE(divide_assignment, "value/=10", "value", 10) |
79 |
+TEST_INT_VARIABLE_VALUE(mod_assignment, "value%=9", "value", 1) |
80 |
+TEST_INT_VARIABLE_VALUE(plus_assignment, "value+=10", "value", 110) |
81 |
+TEST_INT_VARIABLE_VALUE(minus_assignment, "value-=10", "value", 90) |
82 |
+TEST_INT_VARIABLE_VALUE(left_shift_assignment, "value<<=2", "value", 400) |
83 |
+TEST_INT_VARIABLE_VALUE(right_shift_assignment, "value>>=2", "value", 25) |
84 |
+TEST_INT_VARIABLE_VALUE(and_assignment, "value&=10", "value", 0) |
85 |
+TEST_INT_VARIABLE_VALUE(xor_assignment, "value^=5", "value", 97) |
86 |
+TEST_INT_VARIABLE_VALUE(or_assignment, "value|=10", "value", 110) |