* [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, test/
@ 2011-04-14 4:50 Petteri Räty
0 siblings, 0 replies; 3+ messages in thread
From: Petteri Räty @ 2011-04-14 4:50 UTC (permalink / raw
To: gentoo-commits
commit: 793965cb2dac5bb015538620796bc628b7618f44
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed Apr 13 02:29:35 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed Apr 13 08:26:40 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=793965cb
Support assigning empty value to variable
Syntax like 'a=' and 'a[1]=' is supported now.
---
bashast/bashast.g | 4 ++--
bashast/libbashWalker.g | 5 +++--
scripts/var_def.ebuild | 7 +++++++
scripts/var_def.ebuild.result | 4 ++++
src/core/interpreter.h | 7 ++++---
test/variable_printer.cpp | 2 +-
6 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/bashast/bashast.g b/bashast/bashast.g
index 74fd5df..5fc5e7b 100644
--- a/bashast/bashast.g
+++ b/bashast/bashast.g
@@ -214,8 +214,8 @@ cond_comparison
//Defining a variable
//It's not legal to do FOO[1]=(a b c)
var_def
- : name LSQUARE BLANK? explicit_arithmetic BLANK* RSQUARE EQUALS fname -> ^(EQUALS ^(name explicit_arithmetic) fname)
- | name EQUALS^ value;
+ : name LSQUARE BLANK? explicit_arithmetic BLANK* RSQUARE EQUALS fname? -> ^(EQUALS ^(name explicit_arithmetic) fname?)
+ | name EQUALS^ value?;
//Possible values of a variable
value : fname
| LPAREN! wspace!? arr_val RPAREN!;
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index f4fff5d..43304f1 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -71,9 +71,10 @@ var_def
@declarations {
std::map<int, std::string> values;
int index = 0;
+ bool is_null = true;
}:
- ^(EQUALS name libbash_string=string_expr){
- walker->set_value($name.libbash_value, libbash_string, $name.index);
+ ^(EQUALS name (libbash_string=string_expr { is_null = false; })?){
+ walker->set_value($name.libbash_value, libbash_string, $name.index, is_null);
}
|^(EQUALS libbash_name=name_base ^(ARRAY (
(libbash_string=string_expr
diff --git a/scripts/var_def.ebuild b/scripts/var_def.ebuild
index 5d336ee..06ebb3b 100644
--- a/scripts/var_def.ebuild
+++ b/scripts/var_def.ebuild
@@ -16,5 +16,12 @@ ARRAY=(1 2 3 [5]=4 5)
ARRAY2=(1 2 3)
ARRAY2[2]=4
ARRAY2[3]=5
+EMPTY=
ARRAY_LAST=${ARRAY[6]}
PARTIAL[5]=5
+EMPTY_ARRAY=()
+ARRAY3=(1 2 3)
+ARRAY3[0]=
+ARRAY4=(1 2 3)
+# The following one is equivalent to ARRAY4[0]=
+ARRAY4=
diff --git a/scripts/var_def.ebuild.result b/scripts/var_def.ebuild.result
index f288519..6433df7 100644
--- a/scripts/var_def.ebuild.result
+++ b/scripts/var_def.ebuild.result
@@ -1,10 +1,14 @@
ARRAY=1 2 3 4 5
ARRAY2=1 2 4 5
+ARRAY3=2 3
+ARRAY4=2 3
ARRAY_LAST=5
DEPEND=dev-db/sqlite:3
dev-util/pkgconfig
DESCRIPTION=SunPinyin is a SLM (Statistical Language Model) based IME
EAPI=1
+EMPTY=
+EMPTY_ARRAY=
HOMEPAGE=http://sunpinyin.googlecode.com
IUSE=
KEYWORDS=~amd64 ~x86
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 9166379..1e2babe 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -418,13 +418,14 @@ public:
template <typename T>
const T& set_value(const std::string& name,
const T& new_value,
- const unsigned index=0)
+ const unsigned index=0,
+ bool is_null=false)
{
std::shared_ptr<variable> value = members.resolve(name);
if(!value)
- define(name, new_value, false);
+ define(name, new_value, false, is_null);
else
- value->set_value(new_value, index);
+ value->set_value(new_value, index, is_null);
return new_value;
}
diff --git a/test/variable_printer.cpp b/test/variable_printer.cpp
index cb17b9d..32fc161 100644
--- a/test/variable_printer.cpp
+++ b/test/variable_printer.cpp
@@ -46,7 +46,7 @@ int main(int argc, char** argv)
std::map<std::string, std::vector<std::string>> sorted(variables.begin(), variables.end());
using namespace boost::spirit::karma;
- std::cout << format((string << '=' << (string % ' ')) % eol, sorted) << std::endl;
+ std::cout << format((string << '=' << -(string % ' ')) % eol, sorted) << std::endl;
return 0;
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, test/
@ 2011-04-14 4:50 Petteri Räty
0 siblings, 0 replies; 3+ messages in thread
From: Petteri Räty @ 2011-04-14 4:50 UTC (permalink / raw
To: gentoo-commits
commit: 604169c50644cafde6af6b3cbed4aea6db4df721
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed Apr 13 07:07:33 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Apr 14 01:20:31 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=604169c5
Support referring a whole array
Syntax like ${array[@]} and ${array[*]} is supported now. Currently
the only difference between the two is when they are used in a
double quoted string, ${array[*]} will use the first character of
IFS as delimiter rather than space. Rename some variables in test
script for better ordering.
---
bashast/libbashWalker.g | 13 ++++++++++---
scripts/var_def.ebuild | 27 +++++++++++++++++----------
scripts/var_def.ebuild.result | 14 ++++++++++----
src/core/interpreter.cpp | 35 +++++++++++++++++++++++++++++++++++
src/core/interpreter.h | 13 +++++++++++++
test/variable_printer.cpp | 2 ++
6 files changed, 87 insertions(+), 17 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 9a3b7f2..47634e8 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -109,13 +109,13 @@ string_expr returns[std::string libbash_value]:
^(STRING(
(DOUBLE_QUOTED_STRING) => ^(DOUBLE_QUOTED_STRING (libbash_string=double_quoted_string { $libbash_value += libbash_string; })*)
|(ARITHMETIC_EXPRESSION) => ^(ARITHMETIC_EXPRESSION value=arithmetics { $libbash_value = boost::lexical_cast<std::string>(value); })
- |(var_ref) => libbash_string=var_ref { $libbash_value = libbash_string; }
+ |(var_ref[false]) => libbash_string=var_ref[false] { $libbash_value = libbash_string; }
|(libbash_string=any_string { $libbash_value += libbash_string; })+
));
//double quoted string rule, allows expansions
double_quoted_string returns[std::string libbash_value]:
- (var_ref) => libbash_string=var_ref { $libbash_value = libbash_string; }
+ (var_ref[true]) => libbash_string=var_ref[true] { $libbash_value = libbash_string; }
|(ARITHMETIC_EXPRESSION) => ^(ARITHMETIC_EXPRESSION value=arithmetics) { $libbash_value = boost::lexical_cast<std::string>(value); }
|libbash_string=any_string { $libbash_value = libbash_string; };
@@ -167,8 +167,15 @@ word returns[std::string libbash_value]:
|value=arithmetics { $libbash_value = boost::lexical_cast<std::string>(value); };
//variable reference
-var_ref returns[std::string libbash_value]:
+var_ref [bool double_quoted] returns[std::string libbash_value]:
^(VAR_REF name) { $libbash_value = walker->resolve<std::string>($name.libbash_value, $name.index); }
+ |^(VAR_REF ^(libbash_string=name_base AT)) { walker->get_all_elements(libbash_string, $libbash_value); }
+ |^(VAR_REF ^(libbash_string=name_base TIMES)) {
+ if(double_quoted)
+ walker->get_all_elements_IFS_joined(libbash_string, $libbash_value);
+ else
+ walker->get_all_elements(libbash_string, $libbash_value);
+ }
|^(VAR_REF libbash_string=var_expansion) { $libbash_value = libbash_string; };
// shell arithmetic
diff --git a/scripts/var_def.ebuild b/scripts/var_def.ebuild
index 06ebb3b..c93a6de 100644
--- a/scripts/var_def.ebuild
+++ b/scripts/var_def.ebuild
@@ -12,16 +12,23 @@ DEPEND="${RDEPEND}
dev-util/pkgconfig"
MY_PATCH=ldflags.patch
PATCH=("1.patch" 2.patch)
-ARRAY=(1 2 3 [5]=4 5)
-ARRAY2=(1 2 3)
-ARRAY2[2]=4
-ARRAY2[3]=5
+ARRAY01=(1 2 3 [5]=4 5)
+ARRAY02=(1 2 3)
+ARRAY02[2]=4
+ARRAY02[3]=5
EMPTY=
-ARRAY_LAST=${ARRAY[6]}
PARTIAL[5]=5
+ARRAY_LAST=${ARRAY01[6]}
EMPTY_ARRAY=()
-ARRAY3=(1 2 3)
-ARRAY3[0]=
-ARRAY4=(1 2 3)
-# The following one is equivalent to ARRAY4[0]=
-ARRAY4=
+ARRAY03=(1 2 3)
+ARRAY03[0]=
+ARRAY04=(1 2 3)
+# The following one is equivalent to ARRAY04[0]=
+ARRAY04=
+ARRAY05=(1 2 3 4 5)
+ARRAY06=${ARRAY05[@]}
+ARRAY07=${ARRAY05[*]}
+ARRAY08="${ARRAY05[@]}"
+ARRAY09="${ARRAY05[*]}"
+IFS=";,:"
+ARRAY10="${ARRAY05[*]}"
diff --git a/scripts/var_def.ebuild.result b/scripts/var_def.ebuild.result
index 6433df7..058bd98 100644
--- a/scripts/var_def.ebuild.result
+++ b/scripts/var_def.ebuild.result
@@ -1,7 +1,13 @@
-ARRAY=1 2 3 4 5
-ARRAY2=1 2 4 5
-ARRAY3=2 3
-ARRAY4=2 3
+ARRAY01=1 2 3 4 5
+ARRAY02=1 2 4 5
+ARRAY03=2 3
+ARRAY04=2 3
+ARRAY05=1 2 3 4 5
+ARRAY06=1 2 3 4 5
+ARRAY07=1 2 3 4 5
+ARRAY08=1 2 3 4 5
+ARRAY09=1 2 3 4 5
+ARRAY10=1;2;3;4;5
ARRAY_LAST=5
DEPEND=dev-db/sqlite:3
dev-util/pkgconfig
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 0086216..9f10949 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -21,3 +21,38 @@
/// \author Mu Qiao
/// \brief implementations for bash interpreter (visitor pattern).
///
+
+#include "core/interpreter.h"
+
+#include <boost/algorithm/string/join.hpp>
+
+void interpreter::get_all_elements_joined(const std::string& name,
+ const std::string& delim,
+ std::string& result)
+{
+ std::vector<std::string> source;
+ std::shared_ptr<variable> value = members.resolve(name);
+ if(value)
+ {
+ value->get_all_values(source);
+ result = boost::algorithm::join(source, delim);
+ }
+ else
+ {
+ result = "";
+ }
+}
+
+void interpreter::get_all_elements(const std::string& name,
+ std::string& result)
+{
+ get_all_elements_joined(name, " ", result);
+}
+
+void interpreter::get_all_elements_IFS_joined(const std::string& name,
+ std::string& result)
+{
+ get_all_elements_joined(name,
+ resolve<std::string>("IFS").substr(0, 1),
+ result);
+}
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 1e2babe..40663c0 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -56,8 +56,17 @@ class interpreter{
return !(offset < 0 || offset >= static_cast<int>(str.size()));
}
+ void get_all_elements_joined(const std::string& name,
+ const std::string& delim,
+ std::string& result);
+
public:
+ interpreter()
+ {
+ define("IFS", " \t\n");
+ }
+
///
/// \brief return the number of variables
/// \return the number of variables
@@ -531,5 +540,9 @@ public:
return 0;
return value->get_array_length();
}
+
+ void get_all_elements(const std::string&, std::string&);
+
+ void get_all_elements_IFS_joined(const std::string&, std::string&);
};
#endif
diff --git a/test/variable_printer.cpp b/test/variable_printer.cpp
index 32fc161..ed4aa69 100644
--- a/test/variable_printer.cpp
+++ b/test/variable_printer.cpp
@@ -44,6 +44,8 @@ int main(int argc, char** argv)
libbash::interpret(argv[1], variables);
std::map<std::string, std::vector<std::string>> sorted(variables.begin(), variables.end());
+ // Currently we don't need internal variables
+ sorted.erase("IFS");
using namespace boost::spirit::karma;
std::cout << format((string << '=' << -(string % ' ')) % eol, sorted) << std::endl;
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, test/
@ 2011-04-27 15:11 Petteri Räty
0 siblings, 0 replies; 3+ messages in thread
From: Petteri Räty @ 2011-04-27 15:11 UTC (permalink / raw
To: gentoo-commits
commit: 33d754abccf45c6144be7341dcccca03623ad418
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Apr 25 10:03:39 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue Apr 26 07:09:25 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=33d754ab
Walker: support special variables in var_ref
The syntax for special variables is now supported. Only $? has a
runtime now.
---
bashast/libbashWalker.g | 6 ++++++
scripts/var_def.bash | 6 ++++++
scripts/var_def.bash.result | 6 ++++++
src/core/interpreter.h | 8 ++++++++
test/script_compiler.sh | 2 +-
5 files changed, 27 insertions(+), 1 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index eec0218..11b4c19 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -268,6 +268,12 @@ var_ref [bool double_quoted] returns[std::string libbash_value]
else
walker->get_all_elements(libbash_string, $libbash_value);
}
+ |^(VAR_REF TIMES) { std::cerr << "$* has not been implemented yet" << std::endl; }
+ |^(VAR_REF AT) { std::cerr << "$@ has not been implemented yet" << std::endl; }
+ |^(VAR_REF POUND) { std::cerr << "$# has not been implemented yet" << std::endl; }
+ |^(VAR_REF QMARK) { $libbash_value = walker->get_status<std::string>(); }
+ |^(VAR_REF MINUS) { std::cerr << "$- has not been implemented yet" << std::endl; }
+ |^(VAR_REF BANG) { std::cerr << "$! has not been implemented yet" << std::endl; }
|^(VAR_REF libbash_string=var_expansion) { $libbash_value = libbash_string; };
command
diff --git a/scripts/var_def.bash b/scripts/var_def.bash
index 56f8f8d..9b27dc3 100644
--- a/scripts/var_def.bash
+++ b/scripts/var_def.bash
@@ -35,3 +35,9 @@ ARRAY10="${ARRAY05[*]}"
FOO001="networkmanager"
FOO002="0.8.2"
FOO003=${FOO001}-${FOO002}
+FOO004=$*
+FOO004=$@
+FOO004=$#
+FOO004=$?
+FOO004=$-
+FOO004=$!
diff --git a/scripts/var_def.bash.result b/scripts/var_def.bash.result
index 292b9c7..e840ebe 100644
--- a/scripts/var_def.bash.result
+++ b/scripts/var_def.bash.result
@@ -1,3 +1,8 @@
+$* has not been implemented yet
+$@ has not been implemented yet
+$# has not been implemented yet
+$- has not been implemented yet
+$! has not been implemented yet
ARRAY01=1 2 3 4 5
ARRAY02=1 2 4 5
ARRAY03=2 3
@@ -18,6 +23,7 @@ EMPTY_ARRAY=
FOO001=networkmanager
FOO002=0.8.2
FOO003=networkmanager-0.8.2
+FOO004=
HOMEPAGE=http://sunpinyin.googlecode.com
IUSE=
KEYWORDS=~amd64 ~x86
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 9d14f7d..5a90e53 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -485,6 +485,14 @@ public:
set_value("?", status);
}
+ /// \brief get the return status of the last command
+ /// \param the value of the return status
+ template <typename T=int>
+ T get_status(void)
+ {
+ return resolve<T>("?");
+ }
+
/// \brief define a new global variable
/// \param the name of the variable
/// \param the value of the variable
diff --git a/test/script_compiler.sh b/test/script_compiler.sh
index d409673..021855e 100755
--- a/test/script_compiler.sh
+++ b/test/script_compiler.sh
@@ -4,7 +4,7 @@ declare -i error=0
for script in $@
do
- ./variable_printer $script | diff -u $script.result -
+ ./variable_printer $script 2>&1 | diff -u $script.result -
error+=$?
done
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-04-27 15:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-27 15:11 [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, test/ Petteri Räty
-- strict thread matches above, loose matches on Subject: below --
2011-04-14 4:50 Petteri Räty
2011-04-14 4:50 Petteri Räty
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox