1 |
commit: 57e1da1c18d5c9f7c6f5e35bd9146cacf0ace689 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu May 5 12:09:10 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri May 6 10:26:56 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=57e1da1c |
7 |
|
8 |
Core: refactor interpreter class |
9 |
|
10 |
Some large methods are moved into cpp source file. |
11 |
|
12 |
--- |
13 |
src/core/interpreter.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++ |
14 |
src/core/interpreter.h | 64 ++++------------------------------------ |
15 |
2 files changed, 80 insertions(+), 57 deletions(-) |
16 |
|
17 |
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp |
18 |
index f0bf61e..5363636 100644 |
19 |
--- a/src/core/interpreter.cpp |
20 |
+++ b/src/core/interpreter.cpp |
21 |
@@ -36,6 +36,79 @@ |
22 |
|
23 |
#include "libbashWalker.h" |
24 |
|
25 |
+std::string interpreter::get_string(pANTLR3_BASE_TREE node) |
26 |
+{ |
27 |
+ pANTLR3_COMMON_TOKEN token = node->getToken(node); |
28 |
+ // The tree walker may send null pointer here, so return an empty |
29 |
+ // string if that's the case. |
30 |
+ if(!token->start) |
31 |
+ return ""; |
32 |
+ // Use reinterpret_cast here because we have to cast C code. |
33 |
+ // The real type here is int64_t which is used as a pointer. |
34 |
+ return std::string(reinterpret_cast<const char *>(token->start), |
35 |
+ token->stop - token->start + 1); |
36 |
+} |
37 |
+ |
38 |
+bool interpreter::is_unset_or_null(const std::string& name, |
39 |
+ const unsigned index) const |
40 |
+{ |
41 |
+ auto i = members.find(name); |
42 |
+ if(i == members.end()) |
43 |
+ return true; |
44 |
+ else |
45 |
+ return i->second->is_null(index); |
46 |
+} |
47 |
+ |
48 |
+const std::string interpreter::do_substring_expansion(const std::string& name, |
49 |
+ int offset, |
50 |
+ const unsigned index) const |
51 |
+{ |
52 |
+ std::string value = resolve<std::string>(name, index); |
53 |
+ if(!get_real_offset(offset, value)) |
54 |
+ return ""; |
55 |
+ return value.substr(offset); |
56 |
+} |
57 |
+ |
58 |
+const std::string interpreter::do_substring_expansion(const std::string& name, |
59 |
+ int offset, |
60 |
+ int length, |
61 |
+ const unsigned index) const |
62 |
+{ |
63 |
+ if(length < 0) |
64 |
+ throw interpreter_exception("length of substring expression should be greater or equal to zero"); |
65 |
+ std::string value = resolve<std::string>(name, index); |
66 |
+ if(!get_real_offset(offset, value)) |
67 |
+ return ""; |
68 |
+ return value.substr(offset, length); |
69 |
+} |
70 |
+ |
71 |
+std::string interpreter::do_replace_expansion(const std::string& name, |
72 |
+ std::function<void(std::string&)> replacer, |
73 |
+ const unsigned index) const |
74 |
+{ |
75 |
+ std::string value = resolve<std::string>(name, index); |
76 |
+ replacer(value); |
77 |
+ return value; |
78 |
+} |
79 |
+ |
80 |
+unsigned interpreter::get_length(const std::string& name, |
81 |
+ const unsigned index) const |
82 |
+{ |
83 |
+ auto i = members.find(name); |
84 |
+ if(i == members.end()) |
85 |
+ return 0; |
86 |
+ return i->second->get_length(index); |
87 |
+} |
88 |
+ |
89 |
+unsigned interpreter::get_array_length(const std::string& name) const |
90 |
+{ |
91 |
+ auto i = members.find(name); |
92 |
+ if(i == members.end()) |
93 |
+ return 0; |
94 |
+ else |
95 |
+ return i->second->get_array_length(); |
96 |
+} |
97 |
+ |
98 |
void interpreter::get_all_elements_joined(const std::string& name, |
99 |
const std::string& delim, |
100 |
std::string& result) const |
101 |
|
102 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
103 |
index dc446fc..35ae8ac 100644 |
104 |
--- a/src/core/interpreter.h |
105 |
+++ b/src/core/interpreter.h |
106 |
@@ -155,18 +155,7 @@ public: |
107 |
/// of the given pANTLR3_BASE_TREE node. |
108 |
/// \param the target tree node |
109 |
/// \return the value of node->text |
110 |
- static std::string get_string(pANTLR3_BASE_TREE node) |
111 |
- { |
112 |
- pANTLR3_COMMON_TOKEN token = node->getToken(node); |
113 |
- // The tree walker may send null pointer here, so return an empty |
114 |
- // string if that's the case. |
115 |
- if(!token->start) |
116 |
- return ""; |
117 |
- // Use reinterpret_cast here because we have to cast C code. |
118 |
- // The real type here is int64_t which is used as a pointer. |
119 |
- return std::string(reinterpret_cast<const char *>(token->start), |
120 |
- token->stop - token->start + 1); |
121 |
- } |
122 |
+ static std::string get_string(pANTLR3_BASE_TREE node); |
123 |
|
124 |
/// \brief perform logic or |
125 |
/// \param the first operand |
126 |
@@ -442,14 +431,7 @@ public: |
127 |
/// if the variable is undefined |
128 |
/// \param variable name |
129 |
/// \return whether the value of the variable is null |
130 |
- bool is_unset_or_null(const std::string& name, const unsigned index) const |
131 |
- { |
132 |
- auto i = members.find(name); |
133 |
- if(i == members.end()) |
134 |
- return true; |
135 |
- else |
136 |
- return i->second->is_null(index); |
137 |
- } |
138 |
+ bool is_unset_or_null(const std::string& name, const unsigned index) const; |
139 |
|
140 |
/// \brief check whether the value of the variable is unset |
141 |
/// \param variable name |
142 |
@@ -598,13 +580,7 @@ public: |
143 |
/// \return the expansion result |
144 |
const std::string do_substring_expansion(const std::string& name, |
145 |
int offset, |
146 |
- const unsigned index) const |
147 |
- { |
148 |
- std::string value = resolve<std::string>(name, index); |
149 |
- if(!get_real_offset(offset, value)) |
150 |
- return ""; |
151 |
- return value.substr(offset); |
152 |
- } |
153 |
+ const unsigned index) const; |
154 |
|
155 |
/// \brief perform substring expansion |
156 |
/// \param the offset of the substring |
157 |
@@ -613,15 +589,7 @@ public: |
158 |
const std::string do_substring_expansion(const std::string& name, |
159 |
int offset, |
160 |
int length, |
161 |
- const unsigned index) const |
162 |
- { |
163 |
- if(length < 0) |
164 |
- throw interpreter_exception("length of substring expression should be greater or equal to zero"); |
165 |
- std::string value = resolve<std::string>(name, index); |
166 |
- if(!get_real_offset(offset, value)) |
167 |
- return ""; |
168 |
- return value.substr(offset, length); |
169 |
- } |
170 |
+ const unsigned index) const; |
171 |
|
172 |
/// \brief perform replacement expansion |
173 |
/// \param the name of the varaible that needs to be expanded |
174 |
@@ -630,35 +598,17 @@ public: |
175 |
/// \return the expanded value |
176 |
std::string do_replace_expansion(const std::string& name, |
177 |
std::function<void(std::string&)> replacer, |
178 |
- const unsigned index) const |
179 |
- { |
180 |
- std::string value = resolve<std::string>(name, index); |
181 |
- replacer(value); |
182 |
- return value; |
183 |
- } |
184 |
+ const unsigned index) const; |
185 |
|
186 |
/// \brief get the length of a string variable |
187 |
/// \param the name of the variable |
188 |
/// \return the length |
189 |
- unsigned get_length(const std::string& name, const unsigned index=0) const |
190 |
- { |
191 |
- auto i = members.find(name); |
192 |
- if(i == members.end()) |
193 |
- return 0; |
194 |
- return i->second->get_length(index); |
195 |
- } |
196 |
+ unsigned get_length(const std::string& name, const unsigned index=0) const; |
197 |
|
198 |
/// \brief get the length of an array |
199 |
/// \param the name of the array |
200 |
/// \return the length of the array |
201 |
- unsigned get_array_length(const std::string& name) const |
202 |
- { |
203 |
- auto i = members.find(name); |
204 |
- if(i == members.end()) |
205 |
- return 0; |
206 |
- else |
207 |
- return i->second->get_array_length(); |
208 |
- } |
209 |
+ unsigned get_array_length(const std::string& name) const; |
210 |
|
211 |
/// \brief get all array elements concatenated by space |
212 |
/// \param the name of the array |