aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-06-09 23:01:59 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-06-11 16:51:16 +0800
commit21daf9a6dc8459eaf648f61e6116c63b5c97442b (patch)
tree23aad5771eea3c50b5c009d94d61fb62d60adf25
parentCore: call resolve_variable for searching variables (diff)
downloadlibbash-21daf9a6dc8459eaf648f61e6116c63b5c97442b.tar.gz
libbash-21daf9a6dc8459eaf648f61e6116c63b5c97442b.tar.bz2
libbash-21daf9a6dc8459eaf648f61e6116c63b5c97442b.zip
Walker: reimplement positional parameter
We don't need to define $1, $2 ... because $* is all of them. The numbers are actually indexes of $*. This makes it easier to implement shift and $#.
-rw-r--r--bashast/libbashWalker.g5
-rw-r--r--src/core/interpreter.cpp26
2 files changed, 10 insertions, 21 deletions
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index 44df1a9..2648ecb 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -338,7 +338,10 @@ var_name returns[std::string libbash_value, unsigned index]
@init {
$var_name.index = 0;
}
- :libbash_string=num { $libbash_value = libbash_string; }
+ :libbash_string=num {
+ $index = boost::lexical_cast<unsigned>(libbash_string);
+ $libbash_value = ($index != 0 ? "*" : "0");
+ }
|name {
$libbash_value = $name.libbash_value;
$index = $name.index;
diff --git a/src/core/interpreter.cpp b/src/core/interpreter.cpp
index 4acfbb0..4c8c0d5 100644
--- a/src/core/interpreter.cpp
+++ b/src/core/interpreter.cpp
@@ -105,22 +105,12 @@ std::shared_ptr<variable> interpreter::resolve_variable(const std::string& name)
if(name.empty())
return std::shared_ptr<variable>();
- // positional parameter
- if(isdigit(name[0]) && !local_members.empty())
+ BOOST_REVERSE_FOREACH(auto& frame, local_members)
{
- auto iter_local = local_members.back().find(name);
- if(iter_local != local_members.back().end())
+ auto iter_local = frame.find(name);
+ if(iter_local != frame.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())
@@ -286,14 +276,10 @@ void interpreter::define_function_arguments(scope& current_stack,
{
std::map<unsigned, std::string> positional_args;
- for(auto i = 0u; i != arguments.size(); ++i)
- {
- const std::string& name = boost::lexical_cast<std::string>(i + 1);
- current_stack[name].reset(new variable(name, arguments[i]));
- positional_args[i] = arguments[i];
- }
+ for(auto i = 1u; i <= arguments.size(); ++i)
+ positional_args[i] = arguments[i - 1];
- define("*", positional_args);
+ current_stack["*"].reset(new variable("*", positional_args, true));
}
void interpreter::call(const std::string& name,