1 |
commit: ee97a0f09065b4a3117fb1312dfb8b2667015220 |
2 |
Author: Petteri Räty <petsku <AT> petteriraty <DOT> eu> |
3 |
AuthorDate: Mon Mar 28 19:35:29 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Mar 30 12:51:00 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=ee97a0f0 |
7 |
|
8 |
Use function objects to simplify code |
9 |
|
10 |
Using function objects we can make the special assignment operator |
11 |
implementations share code. |
12 |
|
13 |
--- |
14 |
bashast/libbashWalker.g | 30 ++++++++++-------------------- |
15 |
src/core/interpreter.h | 10 ++++++++++ |
16 |
2 files changed, 20 insertions(+), 20 deletions(-) |
17 |
|
18 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
19 |
index 6de281f..f7ed4ab 100644 |
20 |
--- a/bashast/libbashWalker.g |
21 |
+++ b/bashast/libbashWalker.g |
22 |
@@ -81,44 +81,34 @@ arithmetics returns[int value] |
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 |
+ walker->assign(&interpreter::multiply, libbash_name, l); |
29 |
} |
30 |
|^(DIVIDE_ASSIGN libbash_name=name l=arithmetics) { |
31 |
- walker->set_value(libbash_name, |
32 |
- walker->divide(walker->resolve<int>(libbash_name), l)); |
33 |
+ walker->assign(&interpreter::divide, libbash_name, l); |
34 |
} |
35 |
|^(MOD_ASSIGN libbash_name=name l=arithmetics) { |
36 |
- walker->set_value(libbash_name, |
37 |
- walker->mod(walker->resolve<int>(libbash_name), l)); |
38 |
+ walker->assign(&interpreter::mod, libbash_name, l); |
39 |
} |
40 |
|^(PLUS_ASSIGN libbash_name=name l=arithmetics) { |
41 |
- walker->set_value(libbash_name, |
42 |
- walker->plus(walker->resolve<int>(libbash_name), l)); |
43 |
+ walker->assign(&interpreter::plus, libbash_name, l); |
44 |
} |
45 |
|^(MINUS_ASSIGN libbash_name=name l=arithmetics) { |
46 |
- walker->set_value(libbash_name, |
47 |
- walker->minus(walker->resolve<int>(libbash_name), l)); |
48 |
+ walker->assign(&interpreter::minus, libbash_name, l); |
49 |
} |
50 |
|^(LSHIFT_ASSIGN libbash_name=name l=arithmetics) { |
51 |
- walker->set_value(libbash_name, |
52 |
- walker->left_shift(walker->resolve<int>(libbash_name), l)); |
53 |
+ walker->assign(&interpreter::left_shift, libbash_name, l); |
54 |
} |
55 |
|^(RSHIFT_ASSIGN libbash_name=name l=arithmetics) { |
56 |
- walker->set_value(libbash_name, |
57 |
- walker->right_shift(walker->resolve<int>(libbash_name), l)); |
58 |
+ walker->assign(&interpreter::right_shift, libbash_name, l); |
59 |
} |
60 |
|^(AND_ASSIGN libbash_name=name l=arithmetics) { |
61 |
- walker->set_value(libbash_name, |
62 |
- walker->bitwiseand(walker->resolve<int>(libbash_name), l)); |
63 |
+ walker->assign(&interpreter::bitwiseand, libbash_name, l); |
64 |
} |
65 |
|^(XOR_ASSIGN libbash_name=name l=arithmetics) { |
66 |
- walker->set_value(libbash_name, |
67 |
- walker->bitwisexor(walker->resolve<int>(libbash_name), l)); |
68 |
+ walker->assign(&interpreter::bitwisexor, libbash_name, l); |
69 |
} |
70 |
|^(OR_ASSIGN libbash_name=name l=arithmetics) { |
71 |
- walker->set_value(libbash_name, |
72 |
- walker->bitwiseor(walker->resolve<int>(libbash_name), l)); |
73 |
+ walker->assign(&interpreter::bitwiseor, libbash_name, l); |
74 |
} |
75 |
| NUMBER { $value = walker->parse_int($NUMBER);} |
76 |
| DIGIT { $value = walker->parse_int($DIGIT);} |
77 |
|
78 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
79 |
index 1d6ced9..81e0837 100644 |
80 |
--- a/src/core/interpreter.h |
81 |
+++ b/src/core/interpreter.h |
82 |
@@ -29,6 +29,7 @@ |
83 |
|
84 |
#include <memory> |
85 |
#include <string> |
86 |
+#include <functional> |
87 |
|
88 |
#include <antlr3basetree.h> |
89 |
|
90 |
@@ -289,6 +290,15 @@ public: |
91 |
return value; |
92 |
} |
93 |
|
94 |
+ /// \brief assign with an operator (for example multiply) |
95 |
+ /// \param a function object to do an operation while assigning |
96 |
+ /// \param the name of the variable |
97 |
+ /// \param the value to assign |
98 |
+ void assign(std::function<int(int,int)> f, const std::string& name, int value) |
99 |
+ { |
100 |
+ set_value(name,f(resolve<int>(name), value)); |
101 |
+ } |
102 |
+ |
103 |
/// \brief resolve any variable |
104 |
/// \param variable name |
105 |
/// \return the value of the variable, call default constructor if |