public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
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/, src/core/tests/
Date: Fri,  3 Jun 2011 12:43:17 +0000 (UTC)	[thread overview]
Message-ID: <7404d1ac1630b41a94edcfe791dcda1473d11437.betelgeuse@gentoo> (raw)

commit:     7404d1ac1630b41a94edcfe791dcda1473d11437
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Wed Jun  1 10:17:57 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Wed Jun  1 13:27:34 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=7404d1ac

Core: support arithmetic expression in keyword test

---
 bashast/libbashWalker.g                |    2 +-
 scripts/test_expr.bash                 |    4 ++
 scripts/test_expr.bash.result          |    1 +
 src/core/bash_condition.cpp            |   17 +++++----
 src/core/bash_condition.h              |    5 ++-
 src/core/interpreter.cpp               |    7 ++++
 src/core/interpreter.h                 |    5 +++
 src/core/tests/bash_condition_test.cpp |   62 ++++++++++++++++++--------------
 8 files changed, 67 insertions(+), 36 deletions(-)

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index a55e0b2..ec3897d 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -556,7 +556,7 @@ common_condition returns[bool status]
 }
 	// -eq, -ne, -lt, -le, -gt, or -ge for arithmetic. -nt -ot -ef for files
 	:^(NAME left_str=string_expr right_str=string_expr) {
-		$status = internal::test_binary(walker->get_string($NAME), left_str.libbash_value, right_str.libbash_value);
+		$status = internal::test_binary(walker->get_string($NAME), left_str.libbash_value, right_str.libbash_value, *walker);
 	}
 	// -o for shell option,  -z -n for string, -abcdefghkprstuwxOGLSN for files
 	|^(op=LETTER string_expr) {

diff --git a/scripts/test_expr.bash b/scripts/test_expr.bash
index 79398dd..1fa00b4 100644
--- a/scripts/test_expr.bash
+++ b/scripts/test_expr.bash
@@ -44,3 +44,7 @@ echo $? # 0
 [[ "abc def xyz" != *"defg"* ]] && echo "true14"
 shopt -s extglob
 [[ "123" == *([[:digit:]]) ]] && echo "true15"
+i=2
+[[ i++ -gt 2 ]] && echo wrong
+[[ i++ -gt 2 ]] && echo true16
+unset i

diff --git a/scripts/test_expr.bash.result b/scripts/test_expr.bash.result
index 653200b..e883295 100644
--- a/scripts/test_expr.bash.result
+++ b/scripts/test_expr.bash.result
@@ -22,3 +22,4 @@ true12
 true13
 true14
 true15
+true16

diff --git a/src/core/bash_condition.cpp b/src/core/bash_condition.cpp
index 48b523e..1e44299 100644
--- a/src/core/bash_condition.cpp
+++ b/src/core/bash_condition.cpp
@@ -27,6 +27,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "core/bash_ast.h"
+#include "core/interpreter.h"
 #include "core/interpreter_exception.h"
 
 #include "core/bash_condition.h"
@@ -147,7 +149,8 @@ namespace
 
 bool internal::test_binary(const std::string& op,
                            const std::string& lhs,
-                           const std::string& rhs)
+                           const std::string& rhs,
+                           interpreter& walker)
 {
   if(op.size() != 2)
     throw interpreter_exception("Unrecognized operator " + op);
@@ -163,17 +166,17 @@ bool internal::test_binary(const std::string& op,
     // We do not support arithmetic expressions inside keyword test for now.
     // So the operands can only be raw integers.
     else if(op == "eq")
-      return boost::lexical_cast<int>(lhs) == boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) == walker.eval_arithmetic(rhs);
     else if(op == "ne")
-      return boost::lexical_cast<int>(lhs) != boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) != walker.eval_arithmetic(rhs);
     else if(op == "lt")
-      return boost::lexical_cast<int>(lhs) < boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) < walker.eval_arithmetic(rhs);
     else if(op == "le")
-      return boost::lexical_cast<int>(lhs) <= boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) <= walker.eval_arithmetic(rhs);
     else if(op == "gt")
-      return boost::lexical_cast<int>(lhs) > boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) > walker.eval_arithmetic(rhs);
     else if(op == "ge")
-      return boost::lexical_cast<int>(lhs) >= boost::lexical_cast<int>(rhs);
+      return walker.eval_arithmetic(lhs) >= walker.eval_arithmetic(rhs);
     else
       throw interpreter_exception("Unrecognized operator " + op);
   }

diff --git a/src/core/bash_condition.h b/src/core/bash_condition.h
index 8723c28..2df1ec7 100644
--- a/src/core/bash_condition.h
+++ b/src/core/bash_condition.h
@@ -26,13 +26,16 @@
 
 #include <string>
 
+class interpreter;
+
 namespace internal
 {
   bool test_unary(char op, const std::string& target);
 
   bool test_binary(const std::string& op,
                    const std::string& lhs,
-                   const std::string& rhs);
+                   const std::string& rhs,
+                   interpreter& walker);
 }
 
 #endif

diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index fc12a7a..83fd29a 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -35,6 +35,7 @@
 #include <boost/range/adaptor/map.hpp>
 #include <boost/range/algorithm/copy.hpp>
 
+#include "core/bash_ast.h"
 #include "core/unset_exception.h"
 #include "libbashWalker.h"
 
@@ -412,3 +413,9 @@ void interpreter::set_option(const std::string& name, bool value)
 
   iter->second = value;
 }
+
+int interpreter::eval_arithmetic(const std::string& expression)
+{
+  bash_ast ast(std::stringstream(expression), &bash_ast::parser_arithmetics);
+  return ast.interpret_with(*this, &bash_ast::walker_arithmetics);
+}

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 3420ee4..dc2a9bc 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -675,6 +675,11 @@ public:
   /// \return zero unless the name is not a valid shell option
   void set_option(const std::string& name, bool value);
 
+  /// \brief evaluate arithmetic expression and return the result
+  /// \param the arithmetic expression
+  /// \return the evaluated result
+  int eval_arithmetic(const std::string& expression);
+
   /// \brief perform expansion like ${var//foo/bar}
   /// \param the value to be expanded
   /// \param the pattern used to match the value

diff --git a/src/core/tests/bash_condition_test.cpp b/src/core/tests/bash_condition_test.cpp
index de56fee..18a4e18 100644
--- a/src/core/tests/bash_condition_test.cpp
+++ b/src/core/tests/bash_condition_test.cpp
@@ -31,6 +31,7 @@
 #include <gtest/gtest.h>
 
 #include "core/bash_condition.h"
+#include "core/interpreter.h"
 #include "core/interpreter_exception.h"
 
 namespace
@@ -140,45 +141,52 @@ TEST(bash_condition, string_unary_operator)
 
 TEST_F(file_test, binary_operator)
 {
-  EXPECT_TRUE(internal::test_binary("nt", negative, positive));
-  EXPECT_FALSE(internal::test_binary("ot", negative, positive));
+  interpreter walker;
+  EXPECT_TRUE(internal::test_binary("nt", negative, positive, walker));
+  EXPECT_FALSE(internal::test_binary("ot", negative, positive, walker));
 
-  EXPECT_TRUE(internal::test_binary("ot", positive, negative));
-  EXPECT_FALSE(internal::test_binary("nt", positive, negative));
+  EXPECT_TRUE(internal::test_binary("ot", positive, negative, walker));
+  EXPECT_FALSE(internal::test_binary("nt", positive, negative, walker));
 
-  EXPECT_FALSE(internal::test_binary("ot", positive, positive));
-  EXPECT_FALSE(internal::test_binary("nt", positive, positive));
+  EXPECT_FALSE(internal::test_binary("ot", positive, positive, walker));
+  EXPECT_FALSE(internal::test_binary("nt", positive, positive, walker));
 
-  EXPECT_TRUE(internal::test_binary("ef", positive, positive));
-  EXPECT_FALSE(internal::test_binary("ef", positive, negative));
-  EXPECT_FALSE(internal::test_binary("ef", "not exist", negative));
+  EXPECT_TRUE(internal::test_binary("ef", positive, positive, walker));
+  EXPECT_FALSE(internal::test_binary("ef", positive, negative, walker));
+  EXPECT_FALSE(internal::test_binary("ef", "not exist", negative, walker));
 
-  EXPECT_THROW(internal::test_binary("efd", positive, negative), interpreter_exception);
+  EXPECT_THROW(internal::test_binary("efd", positive, negative, walker), interpreter_exception);
 }
 
 TEST(bash_condition, arithmetic_operator)
 {
-  EXPECT_TRUE(internal::test_binary("eq", "1", "1"));
-  EXPECT_FALSE(internal::test_binary("eq", "2", "1"));
+  interpreter walker;
+  walker.define("foo", 1);
 
-  EXPECT_TRUE(internal::test_binary("ne", "2", "1"));
-  EXPECT_FALSE(internal::test_binary("ne", "1", "1"));
+  EXPECT_TRUE(internal::test_binary("eq", "1", "1", walker));
+  EXPECT_FALSE(internal::test_binary("eq", "2", "1", walker));
 
-  EXPECT_TRUE(internal::test_binary("lt", "0", "1"));
-  EXPECT_FALSE(internal::test_binary("lt", "1", "1"));
-  EXPECT_FALSE(internal::test_binary("lt", "2", "1"));
+  EXPECT_TRUE(internal::test_binary("ne", "2", "1", walker));
+  EXPECT_FALSE(internal::test_binary("ne", "1", "1", walker));
 
-  EXPECT_TRUE(internal::test_binary("le", "0", "1"));
-  EXPECT_TRUE(internal::test_binary("le", "1", "1"));
-  EXPECT_FALSE(internal::test_binary("le", "2", "1"));
+  EXPECT_TRUE(internal::test_binary("lt", "0", "1", walker));
+  EXPECT_FALSE(internal::test_binary("lt", "1", "1", walker));
+  EXPECT_FALSE(internal::test_binary("lt", "2", "1", walker));
 
-  EXPECT_TRUE(internal::test_binary("gt", "1", "0"));
-  EXPECT_FALSE(internal::test_binary("gt", "1", "1"));
-  EXPECT_FALSE(internal::test_binary("gt", "0", "1"));
+  EXPECT_TRUE(internal::test_binary("le", "0", "1", walker));
+  EXPECT_TRUE(internal::test_binary("le", "1", "1", walker));
+  EXPECT_FALSE(internal::test_binary("le", "2", "1", walker));
 
-  EXPECT_TRUE(internal::test_binary("ge", "1", "1"));
-  EXPECT_TRUE(internal::test_binary("ge", "2", "1"));
-  EXPECT_FALSE(internal::test_binary("ge", "0", "1"));
+  EXPECT_TRUE(internal::test_binary("gt", "1", "0", walker));
+  EXPECT_FALSE(internal::test_binary("gt", "1", "1", walker));
+  EXPECT_FALSE(internal::test_binary("gt", "0", "1", walker));
 
-  EXPECT_FALSE(internal::test_binary("ge", "blah", "1"));
+  EXPECT_TRUE(internal::test_binary("ge", "1", "1", walker));
+  EXPECT_TRUE(internal::test_binary("ge", "2", "1", walker));
+  EXPECT_FALSE(internal::test_binary("ge", "0", "1", walker));
+
+  EXPECT_FALSE(internal::test_binary("ge", "blah", "1", walker));
+
+  EXPECT_FALSE(internal::test_binary("ge", "foo++", "2", walker));
+  EXPECT_TRUE(internal::test_binary("ge", "foo++", "2", walker));
 }



             reply	other threads:[~2011-06-03 12:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-03 12:43 Petteri Räty [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-06-03  9:08 [gentoo-commits] proj/libbash:master commit in: scripts/, src/core/, bashast/, src/core/tests/ Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-22 21:00 Petteri Räty
2011-04-28  6:19 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-06 15:07 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=7404d1ac1630b41a94edcfe791dcda1473d11437.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