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: Sun, 15 May 2011 11:19:14 +0000 (UTC)	[thread overview]
Message-ID: <d869ca70e03de7a07d041900592e8b3af2b2a562.betelgeuse@gentoo> (raw)

commit:     d869ca70e03de7a07d041900592e8b3af2b2a562
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Sun May 15 06:08:10 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Sun May 15 10:40:51 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=d869ca70

Walker: fix a bug in command stack handling

All commands, built-ins and functions should have their own local
scope(or command environment). Take "foo=abc bar" for example, the
variable foo need to be stored in local scope. We only cared about
the local scope for functions, which caused the example crash. Now
it's fixed by creating local scopes in the walker grammar.

---
 bashast/libbashWalker.g               |   18 ++++++++++++------
 scripts/command_execution.bash        |    1 +
 scripts/command_execution.bash.result |    1 +
 src/core/interpreter.cpp              |    6 +-----
 src/core/interpreter.h                |   20 ++++++++++++++++++++
 5 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 5b22e7a..bd87ee8 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -439,13 +439,19 @@ simple_command
 @declarations {
 	std::vector<std::string> libbash_args;
 }
-	:^(COMMAND string_expr (argument[libbash_args])* var_def[true]*) {
-		if(walker->has_function($string_expr.libbash_value))
+	:^(COMMAND string_expr (argument[libbash_args])* execute_command[$string_expr.libbash_value, libbash_args]);
+
+execute_command[const std::string& name, std::vector<std::string>& libbash_args]
+@declarations {
+	interpreter::local_scope current_scope(*walker);
+}
+	:var_def[true]* {
+		if(walker->has_function(name))
 		{
 			ANTLR3_MARKER command_index = INDEX();
 			try
 			{
-				walker->set_status(walker->call($string_expr.libbash_value,
+				walker->set_status(walker->call(name,
 												libbash_args,
 												ctx,
 												compound_command));
@@ -455,13 +461,13 @@ simple_command
 				SEEK(command_index);
 			}
 		}
-		else if(cppbash_builtin::is_builtin($string_expr.libbash_value))
+		else if(cppbash_builtin::is_builtin(name))
 		{
-			walker->set_status(walker->execute_builtin($string_expr.libbash_value, libbash_args));
+			walker->set_status(walker->execute_builtin(name, libbash_args));
 		}
 		else
 		{
-			std::cerr << $string_expr.libbash_value << " is not supported yet" << std::endl;
+			std::cerr << name << " is not supported yet" << std::endl;
 			walker->set_status(1);
 		}
 	};

diff --git a/scripts/command_execution.bash b/scripts/command_execution.bash
index 3664356..821d061 100644
--- a/scripts/command_execution.bash
+++ b/scripts/command_execution.bash
@@ -13,3 +13,4 @@ false && echo "wrong"
 false || echo "right"
 true || echo "wrong"
 echo "end"
+FOO="abc" echo "command environment"

diff --git a/scripts/command_execution.bash.result b/scripts/command_execution.bash.result
index d9f3021..8bd7226 100644
--- a/scripts/command_execution.bash.result
+++ b/scripts/command_execution.bash.result
@@ -3,5 +3,6 @@ hello world
 right
 right
 end
+command environment
 FOO001=hello
 FOO002=Hello World

diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 1102138..2214613 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -206,8 +206,7 @@ int interpreter::call(const std::string& name,
     return -1;
   func_index = iter->second;
 
-  // Prepare function stack and arguments
-  local_members.push_back(scope());
+  // Prepare arguments
   define_function_arguments(local_members.back(), arguments);
 
   auto INPUT = ctx->pTreeParser->ctnstream;
@@ -221,9 +220,6 @@ int interpreter::call(const std::string& name,
   // Reset to the previous index
   ISTREAM->seek(ISTREAM, curr);
 
-  // Clear function stack
-  local_members.pop_back();
-
   return 0;
 }
 

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index d71885a..c46afe1 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -87,6 +87,26 @@ class interpreter
 
 public:
 
+  ///
+  /// \class local_scope
+  /// \brief RAII concept for local scope management
+  ///
+  class local_scope
+  {
+    interpreter& walker;
+
+  public:
+    local_scope(interpreter& w): walker(w)
+    {
+      walker.local_members.push_back(scope());
+    }
+
+    ~local_scope()
+    {
+      walker.local_members.pop_back();
+    }
+  };
+
   interpreter(): out(&std::cout), err(&std::cerr), in(&std::cin)
   {
     define("IFS", " \t\n");



             reply	other threads:[~2011-05-15 11:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-15 11:19 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-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-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=d869ca70e03de7a07d041900592e8b3af2b2a562.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