aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-07-21 17:53:09 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-08-02 15:46:29 +0800
commit043e0195a2c690e7aa450f7d66c90051bc5040b0 (patch)
treeb5f1b1465a85712ff031921df05853abc64f4a9e
parentParser: support -a and -i option for local (diff)
downloadlibbash-043e0195a2c690e7aa450f7d66c90051bc5040b0.tar.gz
libbash-043e0195a2c690e7aa450f7d66c90051bc5040b0.tar.bz2
libbash-043e0195a2c690e7aa450f7d66c90051bc5040b0.zip
Core: raise exception when using local outside function
-rw-r--r--src/core/interpreter.h2
-rw-r--r--test/walker_test.cpp17
2 files changed, 19 insertions, 0 deletions
diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 801e24f..1ee02dd 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -304,6 +304,8 @@ public:
bool readonly=false,
const unsigned index=0)
{
+ if(local_members.empty())
+ throw libbash::runtime_exception("Define local variables outside function scope");
local_members.back()[name].reset(new variable(name, value, readonly, index));
}
diff --git a/test/walker_test.cpp b/test/walker_test.cpp
index 4f87c26..e5b0583 100644
--- a/test/walker_test.cpp
+++ b/test/walker_test.cpp
@@ -100,3 +100,20 @@ TEST(brace_expansion, not_in_raw_string)
bash_ast ast(input);
EXPECT_THROW(ast.interpret_with(walker), libbash::unsupported_exception);
}
+
+TEST(local_builtin, outside_function)
+{
+ interpreter walker;
+
+ std::string script = "local a";
+ std::istringstream input(script);
+ bash_ast ast(input);
+ try
+ {
+ ast.interpret_with(walker);
+ }
+ catch(libbash::runtime_exception& e)
+ {
+ EXPECT_STREQ(e.what(), "Define local variables outside function scope");
+ }
+}