public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/, src/builtins/
@ 2011-06-11  8:52 Petteri Räty
  0 siblings, 0 replies; only message in thread
From: Petteri Räty @ 2011-06-11  8:52 UTC (permalink / raw
  To: gentoo-commits

commit:     adac84d440f2441e78ffb0175e2009ffc2656d58
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Fri Jun 10 03:16:36 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Fri Jun 10 08:23:13 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=adac84d4

Walker: remove abstraction for arithmetic

---
 bashast/libbashWalker.g          |  112 ++++++++++++------
 src/builtins/inherit_builtin.cpp |    4 +-
 src/core/interpreter.cpp         |    8 --
 src/core/interpreter.h           |  243 --------------------------------------
 4 files changed, 79 insertions(+), 288 deletions(-)

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 9fff16f..608d85c 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -903,70 +903,112 @@ primary returns[std::string libbash_value, unsigned index]
 
 // shell arithmetic
 arithmetics returns[int value]
-	:^(LOGICOR l=arithmetics r=arithmetics) { $value = walker->logicor(l, r); }
-	|^(LOGICAND l=arithmetics r=arithmetics) { $value = walker->logicand(l, r); }
-	|^(PIPE l=arithmetics r=arithmetics) { $value = walker->bitwiseor(l, r); }
-	|^(CARET l=arithmetics r=arithmetics) { $value = walker->bitwisexor(l, r); }
-	|^(AMP l=arithmetics r=arithmetics) { $value = walker->bitwiseand(l, r); }
-	|^(LEQ l=arithmetics r=arithmetics) { $value = walker->less_equal_than(l, r); }
-	|^(GEQ l=arithmetics r=arithmetics) { $value = walker->greater_equal_than(l, r); }
-	|^(LESS_THAN l=arithmetics r=arithmetics) { $value = walker->less_than(l, r); }
-	|^(GREATER_THAN l=arithmetics r=arithmetics) { $value = walker->greater_than(l, r); }
-	|^(NOT_EQUALS l=arithmetics r=arithmetics) { $value = walker->not_equal_to(l, r); }
-	|^(LSHIFT l=arithmetics r=arithmetics) { $value = walker->left_shift(l, r); }
-	|^(RSHIFT l=arithmetics r=arithmetics) { $value = walker->right_shift(l, r); }
-	|^(PLUS l=arithmetics r=arithmetics) { $value = walker->plus(l, r); }
+	:^(LOGICOR l=arithmetics r=arithmetics) { $value = l || r; }
+	|^(LOGICAND l=arithmetics r=arithmetics) { $value = l && r; }
+	|^(PIPE l=arithmetics r=arithmetics) { $value = l | r; }
+	|^(CARET l=arithmetics r=arithmetics) { $value = l ^ r; }
+	|^(AMP l=arithmetics r=arithmetics) { $value = l & r; }
+	|^(LEQ l=arithmetics r=arithmetics) { $value = l <= r; }
+	|^(GEQ l=arithmetics r=arithmetics) { $value = l >= r; }
+	|^(LESS_THAN l=arithmetics r=arithmetics) { $value = l < r; }
+	|^(GREATER_THAN l=arithmetics r=arithmetics) { $value = l > r; }
+	|^(NOT_EQUALS l=arithmetics r=arithmetics) { $value = l != r; }
+	|^(LSHIFT l=arithmetics r=arithmetics) { $value = l << r; }
+	|^(RSHIFT l=arithmetics r=arithmetics) { $value = l >> r; }
+	|^(PLUS l=arithmetics r=arithmetics) { $value = l + r; }
 	|^(PLUS_SIGN l=arithmetics) { $value = l; }
-	|^(MINUS l=arithmetics r=arithmetics) { $value = walker->minus(l, r); }
+	|^(MINUS l=arithmetics r=arithmetics) { $value = l - r; }
 	|^(MINUS_SIGN l=arithmetics) { $value = -l; }
-	|^(TIMES l=arithmetics r=arithmetics) { $value = walker->multiply(l, r); }
-	|^(SLASH l=arithmetics r=arithmetics) { $value = walker->divide(l, r); }
-	|^(PCT l=arithmetics r=arithmetics) { $value = walker->mod(l, r); }
-	|^(EXP l=arithmetics r=arithmetics) { $value = walker->exp(l, r); }
-	|^(BANG l=arithmetics) { $value = walker->negation(l); }
-	|^(TILDE l=arithmetics) { $value = walker->bitwise_negation(l); }
+	|^(TIMES l=arithmetics r=arithmetics) { $value = l * r; }
+	|^(SLASH l=arithmetics r=arithmetics) { $value = l / r; }
+	|^(PCT l=arithmetics r=arithmetics) { $value = l \% r; }
+	|^(EXP l=arithmetics r=arithmetics) {
+		$value = 1;
+		while(r--)
+			$value *= l;
+	}
+	|^(BANG l=arithmetics) { $value = !l; }
+	|^(TILDE l=arithmetics) { $value = ~l; }
 	|^(ARITHMETIC_CONDITION cnd=arithmetics l=arithmetics r=arithmetics) {
-		$value = walker->arithmetic_condition(cnd, l, r);
+		$value = (cnd ? l : r);
 	}
 	|primary {
 		$value = walker->resolve<int>($primary.libbash_value, $primary.index);
 	}
-	|^(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); }
+	|^(PRE_INCR primary) {
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) + 1,
+								   $primary.index);
+	}
+	|^(PRE_DECR primary) {
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) - 1,
+								   $primary.index);
+	}
+	|^(POST_INCR primary) {
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) + 1,
+								   $primary.index);
+		--$value;
+	}
+	|^(POST_DECR primary) {
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) - 1,
+								   $primary.index);
+		++$value;
+	}
 	|^(EQUALS primary l=arithmetics) {
 		$value = walker->set_value($primary.libbash_value, l, $primary.index);
 	}
 	|^(MUL_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::multiply, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) * l,
+								   $primary.index);
 	}
 	|^(DIVIDE_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::divide, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) / l,
+								   $primary.index);
 	}
 	|^(MOD_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::mod, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) \% l,
+								   $primary.index);
 	}
 	|^(PLUS_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::plus, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) + l,
+								   $primary.index);
 	}
 	|^(MINUS_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::minus, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) - l,
+								   $primary.index);
 	}
 	|^(LSHIFT_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::left_shift, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) << l,
+								   $primary.index);
 	}
 	|^(RSHIFT_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::right_shift, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) >> l,
+								   $primary.index);
 	}
 	|^(AND_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::bitwiseand, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) & l,
+								   $primary.index);
 	}
 	|^(XOR_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::bitwisexor, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) ^ l,
+								   $primary.index);
 	}
 	|^(OR_ASSIGN primary l=arithmetics) {
-		$value = walker->assign(&interpreter::bitwiseor, $primary.libbash_value, l, $primary.index);
+		$value = walker->set_value($primary.libbash_value,
+		                           walker->resolve<int>($primary.libbash_value, $primary.index) | l,
+								   $primary.index);
 	}
 	| NUMBER { $value = walker->parse_int($NUMBER);}
 	| DIGIT { $value = walker->parse_int($DIGIT);}

diff --git a/src/builtins/inherit_builtin.cpp b/src/builtins/inherit_builtin.cpp
index e7597df..54ba4ac 100644
--- a/src/builtins/inherit_builtin.cpp
+++ b/src/builtins/inherit_builtin.cpp
@@ -58,7 +58,7 @@ inline bool inherit_builtin::hasq(const std::string& value, const std::string& n
 // We do not support any QA warning
 int inherit_builtin::exec(const std::vector<std::string>& bash_args)
 {
-  _walker.pre_incr("ECLASS_DEPTH", 0);
+  _walker.set_value("ECLASS_DEPTH", _walker.resolve<int>("ECLASS_DEPTH") + 1);
 
   // find eclass directory
   std::string eclassdir;
@@ -114,7 +114,7 @@ int inherit_builtin::exec(const std::vector<std::string>& bash_args)
       _walker.set_value("INHERITED", _walker.resolve<std::string>("INHERITED") + " " + *iter);
   }
 
-  _walker.pre_decr("ECLASS_DEPTH", 0);
+  _walker.set_value("ECLASS_DEPTH", _walker.resolve<int>("ECLASS_DEPTH") - 1);
   if(_walker.resolve<int>("ECLASS_DEPTH") > 0)
   {
     _walker.set_value("ECLASS", PECLASS);

diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 6922df9..87799e9 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -403,14 +403,6 @@ void interpreter::set_option(const std::string& name, bool value)
   iter->second = value;
 }
 
-int interpreter::exp(int left, int right)
-{
-  int init = 1;
-  while(right--)
-    init *= left;
-  return init;
-}
-
 int interpreter::eval_arithmetic(const std::string& expression)
 {
   bash_ast ast(std::stringstream(expression), &bash_ast::parser_arithmetics);

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 6aefc4c..b7274ca 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -24,8 +24,6 @@
 #ifndef LIBBASH_CORE_INTERPRETER_H_
 #define LIBBASH_CORE_INTERPRETER_H_
 
-#include <cmath>
-
 #include <functional>
 #include <memory>
 #include <string>
@@ -191,247 +189,6 @@ public:
   /// \return the value of node->text
   static std::string get_string(pANTLR3_BASE_TREE node);
 
-  /// \brief perform logic or
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int logicor(int left, int right)
-  {
-    return left || right;
-  }
-
-  /// \brief perform logic and
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int logicand(int left, int right)
-  {
-    return left && right;
-  }
-
-  /// \brief perform bitwise or
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int bitwiseor(int left, int right)
-  {
-    return left | right;
-  }
-
-  /// \brief perform bitwise and
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int bitwiseand(int left, int right)
-  {
-    return left & right;
-  }
-
-  /// \brief perform bitwise xor
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int bitwisexor(int left, int right)
-  {
-    return left ^ right;
-  }
-
-  /// \brief perform left or equal to
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int less_equal_than(int left, int right)
-  {
-    return left <= right;
-  }
-
-  /// \brief perform greater or equal to
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int greater_equal_than(int left, int right)
-  {
-    return left >= right;
-  }
-
-  /// \brief perform less than
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int less_than(int left, int right)
-  {
-    return left < right;
-  }
-
-  /// \brief perform greater than
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int greater_than(int left, int right)
-  {
-    return left > right;
-  }
-
-  /// \brief perform not equal to
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int not_equal_to(int left, int right)
-  {
-    return left != right;
-  }
-
-  /// \brief perform left shift
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int left_shift(int left, int right)
-  {
-    return left << right;
-  }
-
-  /// \brief perform right shift
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int right_shift(int left, int right)
-  {
-    return left >> right;
-  }
-
-  /// \brief perform plus
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int plus(int left, int right)
-  {
-    return left + right;
-  }
-
-  /// \brief perform minus
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int minus(int left, int right)
-  {
-    return left - right;
-  }
-
-  /// \brief perform multiply
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int multiply(int left, int right)
-  {
-    return left * right;
-  }
-
-  /// \brief perform divide
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int divide(int left, int right)
-  {
-    // We are not handling division by zero right now
-    return left / right;
-  }
-
-  /// \brief perform modulo
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int mod(int left, int right)
-  {
-    return left % right;
-  }
-
-  /// \brief perform exponential operation
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int exp(int left, int right);
-
-  /// \brief perform logic negation
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int negation(int element)
-  {
-    return !element;
-  }
-
-  /// \brief perform bitwise negation
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int bitwise_negation(int element)
-  {
-    return ~element;
-  }
-
-
-  /// \brief perform the ternary operator
-  /// \param the condition
-  /// \param the first operand
-  /// \param the second operand
-  /// \return the calculated result
-  static int arithmetic_condition(int cnd, int left, int right)
-  {
-    return (cnd? left : right);
-  }
-
-  /// \brief perform pre-increment
-  /// \param the variable name
-  /// \return the increased value
-  int pre_incr(const std::string& name, const unsigned index)
-  {
-    int value = resolve<int>(name, index);
-    set_value(name, ++value);
-    return value;
-  }
-
-  /// \brief perform pre-decrement
-  /// \param the variable name
-  /// \return the decreased value
-  int pre_decr(const std::string& name, const unsigned index)
-  {
-    int value = resolve<int>(name, index);
-    set_value(name, --value);
-    return value;
-  }
-
-  /// \brief perform post-increment
-  /// \param the variable name
-  /// \return the original value
-  int post_incr(const std::string& name, const unsigned index)
-  {
-    int value = resolve<int>(name, index);
-    set_value(name, value + 1);
-    return value;
-  }
-
-  /// \brief perform post-decrement
-  /// \param the variable name
-  /// \return the original value
-  int post_decr(const std::string& name, const unsigned index)
-  {
-    int value = resolve<int>(name, index);
-    set_value(name, value - 1);
-    return value;
-  }
-
-  /// \brief assign with an operator (for example multiply)
-  /// \param a function object to do an operation while assigning
-  /// \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, const unsigned index)
-  {
-    int new_value = f(resolve<int>(name, index), value);
-    set_value(name, new_value, index);
-    return new_value;
-  }
-
   /// \brief resolve string/int variable, local scope will be
   ///        checked first, then global scope
   /// \param variable name



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-06-11  8:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-06-11  8:52 [gentoo-commits] proj/libbash:master commit in: src/core/, bashast/, src/builtins/ 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