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/
Date: Sat,  7 May 2011 12:25:01 +0000 (UTC)	[thread overview]
Message-ID: <45746a47c5205d0b4f3729ef9fa9373894733c8c.betelgeuse@gentoo> (raw)

commit:     45746a47c5205d0b4f3729ef9fa9373894733c8c
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Thu May  5 13:26:54 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sat May  7 12:13:40 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=45746a47

Walker: support defining local variables

---
 bashast/libbashWalker.g          |   20 ++++++++++++++----
 scripts/function_def.bash        |   10 +++++++++
 scripts/function_def.bash.result |    1 +
 src/core/interpreter.cpp         |   40 +++++++++++++++++++++++++++++++++----
 src/core/interpreter.h           |   39 +++++++++++++++++++++---------------
 5 files changed, 84 insertions(+), 26 deletions(-)

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 064556a..1a305ef 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -91,7 +91,11 @@ start: list|EOF;
 
 list: ^(LIST (function_def|logic_command_list)+);
 
-variable_definitions: ^(VARIABLE_DEFINITIONS var_def+);
+variable_definitions
+@declarations {
+	bool local = false;
+}
+	:^(VARIABLE_DEFINITIONS (LOCAL { local = true; })? var_def[local]+);
 
 name_base returns[std::string libbash_value]
 	:NAME { $libbash_value = walker->get_string($NAME); }
@@ -115,13 +119,16 @@ options{ k=1; }
 	:DIGIT { $libbash_value = walker->get_string($DIGIT); }
 	|NUMBER { $libbash_value = walker->get_string($NUMBER); };
 
-var_def
+var_def[bool local]
 @declarations {
 	std::map<int, std::string> values;
 	unsigned index = 0;
 }
 	:^(EQUALS name string_expr?) {
-		walker->set_value($name.libbash_value, $string_expr.libbash_value, $name.index);
+		if(local)
+			walker->define_local($name.libbash_value, $string_expr.libbash_value, false, $name.index);
+		else
+			walker->set_value($name.libbash_value, $string_expr.libbash_value, $name.index);
 	}
 	|^(EQUALS libbash_name=name_base ^(ARRAY (
 										(expr=string_expr
@@ -130,7 +137,10 @@ var_def
 											} expr=string_expr))
 										{ values[index++] = expr.libbash_value; })*
 									 )){
-		walker->define(libbash_name, values);
+		if(local)
+			walker->define_local(libbash_name, values);
+		else
+			walker->define(libbash_name, values);
 	};
 
 string_expr returns[std::string libbash_value, bool quoted]
@@ -282,7 +292,7 @@ simple_command
 @declarations {
 	std::vector<std::string> libbash_args;
 }
-	:^(COMMAND string_expr (argument[libbash_args])* var_def*) {
+	:^(COMMAND string_expr (argument[libbash_args])* var_def[true]*) {
 		if(walker->has_function($string_expr.libbash_value))
 		{
 			walker->set_status(walker->call($string_expr.libbash_value,

diff --git a/scripts/function_def.bash b/scripts/function_def.bash
index 194d030..cbce4cd 100644
--- a/scripts/function_def.bash
+++ b/scripts/function_def.bash
@@ -26,3 +26,13 @@ FOO001="4		5"
 ARRAY=(1 2 3)
 func_with_args ${ARRAY[@]} $FOO001
 func_with_args 100 $ARG2 $ARG3 $ARG4
+
+func_nested1() {
+    echo $foo_nested ${bar_nested[0]}
+}
+func_nested2() {
+    local foo_nested=hi bar_nested=(1 2
+    3)
+    func_nested1
+}
+func_nested2

diff --git a/scripts/function_def.bash.result b/scripts/function_def.bash.result
index de9f3f8..bfc30b5 100644
--- a/scripts/function_def.bash.result
+++ b/scripts/function_def.bash.result
@@ -1,3 +1,4 @@
+hi 1
 ARG1=100
 ARG2=2
 ARG3=3

diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 5363636..aa6c4de 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -24,6 +24,8 @@
 
 #include "core/interpreter.h"
 
+#include <cctype>
+
 #include <functional>
 
 #include <boost/algorithm/string/classification.hpp>
@@ -31,6 +33,7 @@
 #include <boost/algorithm/string/replace.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/trim.hpp>
+#include <boost/foreach.hpp>
 #include <boost/range/adaptor/map.hpp>
 #include <boost/range/algorithm/copy.hpp>
 
@@ -49,6 +52,33 @@ std::string interpreter::get_string(pANTLR3_BASE_TREE node)
                      token->stop - token->start + 1);
 }
 
+std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name) const
+{
+  if(name.empty())
+    throw interpreter_exception("Variable name shouldn't be empty");
+  // positional parameter
+  if(isdigit(name[0]) && !local_members.empty())
+  {
+    auto iter_local = local_members.back().find(name);
+    if(iter_local != local_members.back().end())
+      return iter_local->second;
+  }
+  else
+  {
+    BOOST_REVERSE_FOREACH(auto& frame, local_members)
+    {
+      auto iter_local = frame.find(name);
+      if(iter_local != frame.end())
+        return iter_local->second;
+    }
+  }
+
+  auto iter_global = members.find(name);
+  if(iter_global == members.end())
+    return std::shared_ptr<variable>();
+  return iter_global->second;
+}
+
 bool interpreter::is_unset_or_null(const std::string& name,
                                    const unsigned index) const
 {
@@ -155,13 +185,13 @@ void interpreter::split_word(const std::string& word, std::vector<std::string>&
   output.insert(output.end(), splitted_values.begin(), splitted_values.end());
 }
 
-inline void define_function_arguments(std::unique_ptr<scope>& current_stack,
+inline void define_function_arguments(scope& current_stack,
                                       const std::vector<std::string>& arguments)
 {
   for(auto i = 0u; i != arguments.size(); ++i)
   {
     const std::string& name = boost::lexical_cast<std::string>(i + 1);
-    (*current_stack)[name] = std::shared_ptr<variable>(new variable(name, arguments[i]));
+    current_stack[name].reset(new variable(name, arguments[i]));
   }
 }
 
@@ -177,8 +207,8 @@ int interpreter::call(const std::string& name,
   func_index = iter->second;
 
   // Prepare function stack and arguments
-  local_members.push(std::unique_ptr<scope>(new scope));
-  define_function_arguments(local_members.top(), arguments);
+  local_members.push_back(scope());
+  define_function_arguments(local_members.back(), arguments);
 
   auto INPUT = ctx->pTreeParser->ctnstream;
   auto ISTREAM = INPUT->tnstream->istream;
@@ -192,7 +222,7 @@ int interpreter::call(const std::string& name,
   ISTREAM->seek(ISTREAM, curr);
 
   // Clear function stack
-  local_members.pop();
+  local_members.pop_back();
 
   return 0;
 }

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 35ae8ac..7673479 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -29,7 +29,6 @@
 
 #include <functional>
 #include <memory>
-#include <stack>
 #include <string>
 
 #include <antlr3basetree.h>
@@ -60,7 +59,7 @@ class interpreter
   /// \var private::local_members
   /// \brief local scope for function arguments, execution environment and
   ///        local variables
-  std::stack<std::unique_ptr<scope>> local_members;
+  std::vector<scope> local_members;
 
   std::ostream* out;
 
@@ -83,6 +82,8 @@ class interpreter
                                const std::string& delim,
                                std::string& result) const;
 
+  std::shared_ptr<variable> resolve_variable(const std::string&) const;
+
 public:
 
   interpreter(): out(&std::cout), err(&std::cerr), in(&std::cin)
@@ -401,17 +402,11 @@ public:
   template <typename T>
   T resolve(const std::string& name, const unsigned index=0) const
   {
-    if(!local_members.empty())
-    {
-      auto iter_local = local_members.top()->find(name);
-      if(iter_local != local_members.top()->end())
-        return iter_local->second->get_value<T>(index);
-    }
-
-    auto iter_global = members.find(name);
-    if(iter_global == members.end())
-      return T();
-    return iter_global->second->get_value<T>(index);
+    auto var = resolve_variable(name);
+    if(var)
+      return var->get_value<T>(index);
+    else
+      return T{};
   }
 
   /// \brief resolve array variable
@@ -495,9 +490,21 @@ public:
               bool readonly=false,
               const unsigned index=0)
   {
-    std::shared_ptr<variable> target(
-        new variable(name, value, readonly, index));
-    members[name] = target;
+    members[name].reset(new variable(name, value, readonly, index));
+  }
+
+  /// \brief define a new local variable
+  /// \param the name of the variable
+  /// \param the value of the variable
+  /// \param whether it's readonly, default is false
+  /// \param whether it's null, default is false
+  template <typename T>
+  void define_local(const std::string& name,
+                    const T& value,
+                    bool readonly=false,
+                    const unsigned index=0)
+  {
+    local_members.back()[name].reset(new variable(name, value, readonly, index));
   }
 
   /// \brief define a new function



             reply	other threads:[~2011-05-07 12:25 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-07 12:25 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-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-14  4:50 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=45746a47c5205d0b4f3729ef9fa9373894733c8c.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