From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/
Date: Thu, 14 Apr 2011 04:50:16 +0000 (UTC) [thread overview]
Message-ID: <e69a687ae1d3db7c61179b8492ee51e47dc0569a.betelgeuse@gentoo> (raw)
commit: e69a687ae1d3db7c61179b8492ee51e47dc0569a
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 12 11:11:32 2011 +0000
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Apr 14 01:33:29 2011 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=e69a687a
Implement array element in arithmetic expansion
Array element reference and assignment in arithmetic expansion are
supported now.
---
bashast/libbashWalker.g | 72 ++++++++++++++++----------
scripts/arithmetic_assignment.ebuild | 2 +
scripts/arithmetic_assignment.ebuild.result | 3 +
scripts/binary_arithmetic.ebuild | 28 ++++++++++-
scripts/binary_arithmetic.ebuild.result | 26 +++++++++-
src/core/interpreter.h | 22 ++++----
6 files changed, 112 insertions(+), 41 deletions(-)
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 47634e8..05c1127 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -178,6 +178,22 @@ var_ref [bool double_quoted] returns[std::string libbash_value]:
}
|^(VAR_REF libbash_string=var_expansion) { $libbash_value = libbash_string; };
+// Only used in arithmetic expansion
+primary returns[std::string libbash_value, unsigned index]:
+ (^(VAR_REF name)) => ^(VAR_REF name) {
+ $libbash_value = $name.libbash_value;
+ $index = $name.index;
+ }
+ |name {
+ $libbash_value = $name.libbash_value;
+ $index = $name.index;
+ }
+ // array[@] and array[*] is meaningless to arithmetic expansion so true/false are both ok.
+ |^(VAR_REF libbash_string=var_ref[false]) {
+ $libbash_value = libbash_string;
+ $index = 0;
+ };
+
// shell arithmetic
arithmetics returns[int value]
:
@@ -205,45 +221,45 @@ arithmetics returns[int value]
|^(ARITHMETIC_CONDITION cnd=arithmetics l=arithmetics r=arithmetics){
$value = walker->arithmetic_condition(cnd, l, r);
}
- |^(VAR_REF name) {
- $value = walker->resolve<int>($name.libbash_value);
+ |primary {
+ $value = walker->resolve<int>($primary.libbash_value, $primary.index);
}
- |^(PRE_INCR ^(VAR_REF name)){ $value = walker->pre_incr($name.libbash_value); }
- |^(PRE_DECR ^(VAR_REF name)){ $value = walker->pre_decr($name.libbash_value); }
- |^(POST_INCR ^(VAR_REF name)){ $value = walker->post_incr($name.libbash_value); }
- |^(POST_DECR ^(VAR_REF name)){ $value = walker->post_decr($name.libbash_value); }
- |^(EQUALS name l=arithmetics) {
- $value = walker->set_value($name.libbash_value, l);
+ |^(PRE_INCR primary){ $value = walker->pre_incr($primary.libbash_value, $primary.index); }
+ |^(PRE_DECR primary){ $value = walker->pre_decr($primary.libbash_value, $primary.index); }
+ |^(POST_INCR primary){ $value = walker->post_incr($primary.libbash_value, $primary.index); }
+ |^(POST_DECR primary){ $value = walker->post_decr($primary.libbash_value, $primary.index); }
+ |^(EQUALS primary l=arithmetics) {
+ $value = walker->set_value($primary.libbash_value, l, $primary.index);
}
- |^(MUL_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::multiply, $name.libbash_value, l);
+ |^(MUL_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::multiply, $primary.libbash_value, l, $primary.index);
}
- |^(DIVIDE_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::divide, $name.libbash_value, l);
+ |^(DIVIDE_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::divide, $primary.libbash_value, l, $primary.index);
}
- |^(MOD_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::mod, $name.libbash_value, l);
+ |^(MOD_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::mod, $primary.libbash_value, l, $primary.index);
}
- |^(PLUS_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::plus, $name.libbash_value, l);
+ |^(PLUS_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::plus, $primary.libbash_value, l, $primary.index);
}
- |^(MINUS_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::minus, $name.libbash_value, l);
+ |^(MINUS_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::minus, $primary.libbash_value, l, $primary.index);
}
- |^(LSHIFT_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::left_shift, $name.libbash_value, l);
+ |^(LSHIFT_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::left_shift, $primary.libbash_value, l, $primary.index);
}
- |^(RSHIFT_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::right_shift, $name.libbash_value, l);
+ |^(RSHIFT_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::right_shift, $primary.libbash_value, l, $primary.index);
}
- |^(AND_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::bitwiseand, $name.libbash_value, l);
+ |^(AND_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::bitwiseand, $primary.libbash_value, l, $primary.index);
}
- |^(XOR_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::bitwisexor, $name.libbash_value, l);
+ |^(XOR_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::bitwisexor, $primary.libbash_value, l, $primary.index);
}
- |^(OR_ASSIGN name l=arithmetics) {
- $value = walker->assign(&interpreter::bitwiseor, $name.libbash_value, l);
+ |^(OR_ASSIGN primary l=arithmetics) {
+ $value = walker->assign(&interpreter::bitwiseor, $primary.libbash_value, l, $primary.index);
}
| NUMBER { $value = walker->parse_int($NUMBER);}
| DIGIT { $value = walker->parse_int($DIGIT);}
diff --git a/scripts/arithmetic_assignment.ebuild b/scripts/arithmetic_assignment.ebuild
index 20bcc44..27e2ee5 100644
--- a/scripts/arithmetic_assignment.ebuild
+++ b/scripts/arithmetic_assignment.ebuild
@@ -9,4 +9,6 @@ FOO007="$((value>>=2))"
FOO008="$((value&=10))"
FOO009="$((value^=5))"
FOO010="$((value|=10))"
+FOO011=("CREATED" 2)
+FOO012="$((${FOO011[0]}=10))"
value="$((100))"
diff --git a/scripts/arithmetic_assignment.ebuild.result b/scripts/arithmetic_assignment.ebuild.result
index 64863b5..9f47242 100644
--- a/scripts/arithmetic_assignment.ebuild.result
+++ b/scripts/arithmetic_assignment.ebuild.result
@@ -1,3 +1,4 @@
+CREATED=10
FOO001=1000
FOO002=100
FOO003=1
@@ -8,4 +9,6 @@ FOO007=1
FOO008=0
FOO009=5
FOO010=15
+FOO011=CREATED 2
+FOO012=10
value=100
diff --git a/scripts/binary_arithmetic.ebuild b/scripts/binary_arithmetic.ebuild
index a674f5c..bdc3d94 100644
--- a/scripts/binary_arithmetic.ebuild
+++ b/scripts/binary_arithmetic.ebuild
@@ -1,3 +1,4 @@
+ARRAY=(1 2 3 4 5)
FOO001="$((0 || -2))"
FOO002="$((0 || 0))"
FOO003="$((-1 && 10))"
@@ -36,4 +37,29 @@ FOO033="$((++value+value++))"
FOO034="$((10*(2+5)<<3%2**5))"
FOO035="$((10*value<<3%2**5))"
FOO036="$(( (20&5|3||1*100-20&5*10)+~(2*5) ))"
-value="$((100))"
+FOO037="$((ARRAY[0]++))"
+FOO038="$((++ARRAY[0]))"
+FOO039="$((ARRAY[0]--))"
+FOO040="$((--ARRAY[0]))"
+FOO041="$((ARRAY[8]=9))"
+FOO042="$((ARRAY[8]*=10))"
+FOO043="$((ARRAY[8]/=10))"
+FOO044="$((ARRAY[8]%=2))"
+FOO045="$((ARRAY[8]+=8))"
+FOO046="$((ARRAY[8]-=0))"
+FOO047="$((ARRAY[8]<<=1))"
+FOO048="$((ARRAY[8]>>=1))"
+FOO049="$((ARRAY[8]&=5))"
+FOO050="$((ARRAY[8]|=10))"
+FOO051="$((ARRAY[8]^=3))"
+PARTIAL[8]=5
+FOO052="$((PARTIAL[8]*=1))"
+# The following 3 just expand to non-exist variables
+FOO053="$((${#ARRAY[@]}))"
+FOO054="$((${ARRAY[5]:-10}))"
+FOO055="$((${ARRAY:0}))"
+value=100
+FOO056="value"
+FOO057="$((${FOO056}++))"
+FOO058="$((${FOO056}+=10))"
+ARRAY=(1 2 3 4 5)
diff --git a/scripts/binary_arithmetic.ebuild.result b/scripts/binary_arithmetic.ebuild.result
index b258fd0..63cfa28 100644
--- a/scripts/binary_arithmetic.ebuild.result
+++ b/scripts/binary_arithmetic.ebuild.result
@@ -1,3 +1,4 @@
+ARRAY=1 2 3 4 5
FOO001=1
FOO002=0
FOO003=1
@@ -34,4 +35,27 @@ FOO033=206
FOO034=560
FOO035=8320
FOO036=-10
-value=100
+FOO037=1
+FOO038=3
+FOO039=3
+FOO040=1
+FOO041=9
+FOO042=90
+FOO043=9
+FOO044=1
+FOO045=9
+FOO046=9
+FOO047=18
+FOO048=9
+FOO049=1
+FOO050=11
+FOO051=8
+FOO052=5
+FOO053=0
+FOO054=0
+FOO055=0
+FOO056=value
+FOO057=100
+FOO058=111
+PARTIAL=5
+value=111
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 4d51d5b..5ac5027 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -321,9 +321,9 @@ public:
/// \brief perform pre-increment
/// \param the variable name
/// \return the increased value
- int pre_incr(const std::string& name)
+ int pre_incr(const std::string& name, const unsigned index)
{
- int value = resolve<int>(name);
+ int value = resolve<int>(name, index);
set_value(name, ++value);
return value;
}
@@ -331,9 +331,9 @@ public:
/// \brief perform pre-decrement
/// \param the variable name
/// \return the decreased value
- int pre_decr(const std::string& name)
+ int pre_decr(const std::string& name, const unsigned index)
{
- int value = resolve<int>(name);
+ int value = resolve<int>(name, index);
set_value(name, --value);
return value;
}
@@ -341,9 +341,9 @@ public:
/// \brief perform post-increment
/// \param the variable name
/// \return the original value
- int post_incr(const std::string& name)
+ int post_incr(const std::string& name, const unsigned index)
{
- int value = resolve<int>(name);
+ int value = resolve<int>(name, index);
set_value(name, value + 1);
return value;
}
@@ -351,9 +351,9 @@ public:
/// \brief perform post-decrement
/// \param the variable name
/// \return the original value
- int post_decr(const std::string& name)
+ int post_decr(const std::string& name, const unsigned index)
{
- int value = resolve<int>(name);
+ int value = resolve<int>(name, index);
set_value(name, value - 1);
return value;
}
@@ -363,10 +363,10 @@ public:
/// \param the name of the variable
/// \param the value to assign
/// \return the new value of the variable
- int assign(std::function<int(int,int)> f, const std::string& name, int value)
+ int assign(std::function<int(int,int)> f, const std::string& name, int value, const unsigned index)
{
- int new_value = f(resolve<int>(name), value);
- set_value(name, new_value);
+ int new_value = f(resolve<int>(name, index), value);
+ set_value(name, new_value, index);
return new_value;
}
next reply other threads:[~2011-04-14 4:51 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-14 4:50 Petteri Räty [this message]
-- strict thread matches above, loose matches on Subject: below --
2012-08-19 14:54 [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/ Petteri Räty
2011-08-04 13:53 Petteri Räty
2011-06-09 8:15 Petteri Räty
2011-06-03 14:48 Petteri Räty
2011-05-15 11:19 Petteri Räty
2011-05-11 7:19 Petteri Räty
2011-05-07 12:25 Petteri Räty
2011-04-20 14:04 Petteri Räty
2011-04-20 14:04 Petteri Räty
2011-04-20 14:04 Petteri Räty
2011-04-06 15:07 Petteri Räty
2011-04-06 7:43 Petteri Räty
2011-04-04 16:09 Petteri Räty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e69a687ae1d3db7c61179b8492ee51e47dc0569a.betelgeuse@gentoo \
--to=betelgeuse@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox