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

commit:     e86cc25a58e07c717606dbfd95d2497a9dd6f638
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 26 08:22:20 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Thu Apr 28 02:52:42 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=e86cc25a

Core: use reference/pointer for the interpreter object

We don't share interpreter object and it's safer to use reference
rather than shared_ptr. Raw pointer is used in the generated C
source code.

---
 Makefile.am                                   |    6 +--
 bashast/libbashWalker.g                       |    7 ++--
 src/core/{parser_builder.cpp => bash_ast.cpp} |   27 ++++++++------
 src/core/{parser_builder.h => bash_ast.h}     |   23 ++++++------
 src/core/walker_builder.cpp                   |   36 -------------------
 src/core/walker_builder.h                     |   46 -------------------------
 src/libbash.cpp                               |   12 +++---
 test/walker_test.cpp                          |   20 ++++++-----
 utils/ast_printer.cpp                         |   16 ++++----
 9 files changed, 56 insertions(+), 137 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 7202cef..aefcf51 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -164,10 +164,8 @@ libcppbash_la_SOURCES = src/common.h \
 						src/core/interpreter.cpp \
 						src/core/interpreter.h \
 						src/core/symbols.hpp \
-						src/core/parser_builder.cpp \
-						src/core/parser_builder.h \
-						src/core/walker_builder.cpp \
-						src/core/walker_builder.h \
+						src/core/bash_ast.cpp \
+						src/core/bash_ast.h \
 						$(GENERATED_WALKER_C) \
 						$(GENERATED_WALKER_H)
 

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index b8fd25a..e278d1b 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -27,12 +27,11 @@ options
 
 @includes{
 
-	#include <memory>
 	#include <string>
 	#include <vector>
 
 	class interpreter;
-	void set_interpreter(std::shared_ptr<interpreter> w);
+	void set_interpreter(interpreter* w);
 
 }
 
@@ -50,9 +49,9 @@ options
 
 @members{
 
-	static std::shared_ptr<interpreter> walker;
+	static interpreter* walker = 0;
 
-	void set_interpreter(std::shared_ptr<interpreter> w)
+	void set_interpreter(interpreter* w)
 	{
 		walker = w;
 	}

diff --git a/src/core/parser_builder.cpp b/src/core/bash_ast.cpp
similarity index 86%
rename from src/core/parser_builder.cpp
rename to src/core/bash_ast.cpp
index c136e90..3010774 100644
--- a/src/core/parser_builder.cpp
+++ b/src/core/bash_ast.cpp
@@ -17,21 +17,21 @@
    along with libbash.  If not, see <http://www.gnu.org/licenses/>.
 */
 ///
-/// \file parser_builder.cpp
+/// \file bash_ast.cpp
 /// \author Mu Qiao
-/// \brief a class that helps build libbashParser from istream
+/// \brief implementation that helps interpret from istream
 ///
 
-#include "core/parser_builder.h"
+#include "core/bash_ast.h"
 
 #include <sstream>
 
 #include "core/interpreter_exception.h"
 #include "libbashLexer.h"
 #include "libbashParser.h"
-#include "walker_builder.h"
+#include "libbashWalker.h"
 
-parser_builder::parser_builder(std::istream& source)
+bash_ast::bash_ast(std::istream& source)
 {
   std::stringstream stream;
   stream << source.rdbuf();
@@ -44,7 +44,7 @@ parser_builder::parser_builder(std::istream& source)
   init_parser();
 }
 
-parser_builder::~parser_builder()
+bash_ast::~bash_ast()
 {
   nodes->free(nodes);
   psr->free(psr);
@@ -53,7 +53,7 @@ parser_builder::~parser_builder()
   input->close(input);
 }
 
-void parser_builder::init_parser()
+void bash_ast::init_parser()
 {
   lxr = libbashLexerNew(input);
   if ( lxr == NULL )
@@ -72,18 +72,21 @@ void parser_builder::init_parser()
   nodes = antlr3CommonTreeNodeStreamNewTree(langAST->tree, ANTLR3_SIZE_HINT);
 }
 
-walker_builder parser_builder::create_walker_builder()
+void bash_ast::interpret_with(interpreter& walker)
 {
-  return walker_builder(nodes);
+  set_interpreter(&walker);
+  plibbashWalker treePsr = libbashWalkerNew(nodes);
+  treePsr->start(treePsr);
+  treePsr->free(treePsr);
 }
 
-std::string parser_builder::get_dot_graph()
+std::string bash_ast::get_dot_graph()
 {
   pANTLR3_STRING graph = nodes->adaptor->makeDot(nodes->adaptor, langAST->tree);
   return std::string(reinterpret_cast<char*>(graph->chars));
 }
 
-std::string parser_builder::get_string_tree()
+std::string bash_ast::get_string_tree()
 {
   return std::string(reinterpret_cast<char*>(
         langAST->tree->toStringTree(langAST->tree)->chars));
@@ -102,7 +105,7 @@ namespace
   }
 }
 
-std::string parser_builder::get_tokens(std::function<std::string(ANTLR3_INT32)> token_map)
+std::string bash_ast::get_tokens(std::function<std::string(ANTLR3_INT32)> token_map)
 {
   std::stringstream result;
   int line_counter = 1;

diff --git a/src/core/parser_builder.h b/src/core/bash_ast.h
similarity index 75%
rename from src/core/parser_builder.h
rename to src/core/bash_ast.h
index cbfa13c..d7ae27c 100644
--- a/src/core/parser_builder.h
+++ b/src/core/bash_ast.h
@@ -17,9 +17,9 @@
    along with libbash.  If not, see <http://www.gnu.org/licenses/>.
 */
 ///
-/// \file parser_builder.h
+/// \file bash_ast.h
 /// \author Mu Qiao
-/// \brief a class that helps build libbashParser from istream
+/// \brief a class that helps interpret from istream
 ///
 
 #ifndef LIBBASH_CORE_PARSER_BUILDER_H_
@@ -35,12 +35,11 @@
 struct libbashLexer_Ctx_struct;
 struct libbashParser_Ctx_struct;
 struct libbashParser_start_return_struct;
-class walker_builder;
+class interpreter;
 
-/// \class parser_builder
-/// \brief a wrapper class that creates libbashParser. It also know
-///        enough information to create walker_builder object.
-class parser_builder
+/// \class bash_ast
+/// \brief a wrapper class that helps interpret from istream
+class bash_ast
 {
   pANTLR3_INPUT_STREAM input;
   std::string script;
@@ -52,13 +51,13 @@ class parser_builder
 
   void init_parser();
 public:
-  explicit parser_builder(std::istream& source);
-  ~parser_builder();
+  explicit bash_ast(std::istream& source);
+  ~bash_ast();
 
   ///
-  /// \brief factory method that creates walker_builder
-  /// \return walker_builder object
-  walker_builder create_walker_builder();
+  /// \brief interpret the script with a given interpreter
+  /// \param the interpreter object
+  void interpret_with(interpreter& walker);
   std::string get_dot_graph();
   std::string get_string_tree();
   std::string get_tokens(std::function<std::string(ANTLR3_INT32)> token_map);

diff --git a/src/core/walker_builder.cpp b/src/core/walker_builder.cpp
deleted file mode 100644
index dbe47d9..0000000
--- a/src/core/walker_builder.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
-   Please use git log for copyright holder and year information
-
-   This file is part of libbash.
-
-   libbash is free software: you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation, either version 2 of the License, or
-   (at your option) any later version.
-
-   libbash is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
-*/
-///
-/// \file walker_builder.cpp
-/// \author Mu Qiao
-/// \brief a class that helps build a libbashWalker from istream
-///
-
-#include "core/walker_builder.h"
-
-#include "core/interpreter.h"
-#include "libbashWalker.h"
-
-walker_builder::walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes): walker(new interpreter)
-{
-  set_interpreter(walker);
-  plibbashWalker treePsr = libbashWalkerNew(nodes);
-  treePsr->start(treePsr);
-  treePsr->free(treePsr);
-}

diff --git a/src/core/walker_builder.h b/src/core/walker_builder.h
deleted file mode 100644
index 6b6714b..0000000
--- a/src/core/walker_builder.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
-   Please use git log for copyright holder and year information
-
-   This file is part of libbash.
-
-   libbash is free software: you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation, either version 2 of the License, or
-   (at your option) any later version.
-
-   libbash is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
-*/
-///
-/// \file walker_builder.h
-/// \author Mu Qiao
-/// \brief a class that helps build a libbashWalker
-///
-
-#ifndef LIBBASH_CORE_WALKER_BUILDER_H_
-#define LIBBASH_CORE_WALKER_BUILDER_H_
-
-#include <memory>
-
-#include <antlr3.h>
-
-class interpreter;
-
-/// \class walker_builder
-/// \brief a wrapper class that creates libbashWalker
-class walker_builder
-{
-public:
-  /// \var public::walker
-  /// \brief the interpreter object that contains all runtime information
-  std::shared_ptr<interpreter> walker;
-
-  explicit walker_builder(pANTLR3_COMMON_TREE_NODE_STREAM nodes);
-};
-
-#endif

diff --git a/src/libbash.cpp b/src/libbash.cpp
index 0d1eadd..4fb4102 100644
--- a/src/libbash.cpp
+++ b/src/libbash.cpp
@@ -27,8 +27,7 @@
 #include <fstream>
 
 #include "core/interpreter.h"
-#include "core/parser_builder.h"
-#include "core/walker_builder.h"
+#include "core/bash_ast.h"
 
 namespace libbash
 {
@@ -39,11 +38,12 @@ namespace libbash
     std::ifstream input(path.c_str());
     if(!input)
       throw interpreter_exception("Unable to create fstream for script: " + path);
-    parser_builder pbuilder(input);
-    walker_builder wbuilder = pbuilder.create_walker_builder();
+    interpreter walker;
+    bash_ast ast(input);
+    ast.interpret_with(walker);
 
-    for(auto iter = wbuilder.walker->begin(); iter != wbuilder.walker->end(); ++iter)
+    for(auto iter = walker.begin(); iter != walker.end(); ++iter)
       iter->second->get_all_values<std::string>(variables[iter->first]);
-    wbuilder.walker->get_all_function_names(functions);
+    walker.get_all_function_names(functions);
   }
 }

diff --git a/test/walker_test.cpp b/test/walker_test.cpp
index 6f13176..c3aacbb 100644
--- a/test/walker_test.cpp
+++ b/test/walker_test.cpp
@@ -27,17 +27,17 @@
 #include <gtest/gtest.h>
 
 #include "core/interpreter.h"
-#include "core/parser_builder.h"
-#include "core/walker_builder.h"
+#include "core/bash_ast.h"
 
 static void check_string_assignment(const char* script,
                                     const std::string& name,
                                     const char* exp_value)
 {
+  interpreter walker;
   std::istringstream input(script);
-  parser_builder pbuilder(input);
-  walker_builder wbuilder = pbuilder.create_walker_builder();
-  EXPECT_STREQ(exp_value, wbuilder.walker->resolve<std::string>(name).c_str());
+  bash_ast ast(input);
+  ast.interpret_with(walker);
+  EXPECT_STREQ(exp_value, walker.resolve<std::string>(name).c_str());
 }
 
 #define TEST_STRING_ASSIGNMENT(name, script, var_name, exp_value)\
@@ -62,13 +62,15 @@ TEST_STRING_ASSIGNMENT(str_assignment6,
 
 TEST(array_index, out_of_bound)
 {
+  interpreter walker;
+
   std::string script = "a[-1]=\"1\"";
   std::istringstream input(script);
-  parser_builder pbuilder(input);
-  EXPECT_THROW(pbuilder.create_walker_builder(), interpreter_exception);
+  bash_ast ast(input);
+  EXPECT_THROW(ast.interpret_with(walker), interpreter_exception);
 
   std::string script2 = "a=(1 2 [-5]=1)";
   std::istringstream input2(script2);
-  parser_builder pbuilder2(input2);
-  EXPECT_THROW(pbuilder2.create_walker_builder(), interpreter_exception);
+  bash_ast ast2(input2);
+  EXPECT_THROW(ast2.interpret_with(walker), interpreter_exception);
 }

diff --git a/utils/ast_printer.cpp b/utils/ast_printer.cpp
index 66d8c8a..dae4233 100644
--- a/utils/ast_printer.cpp
+++ b/utils/ast_printer.cpp
@@ -33,7 +33,7 @@
 #include <boost/spirit/include/qi.hpp>
 #include <boost/fusion/include/adapt_struct.hpp>
 
-#include "core/parser_builder.h"
+#include "core/bash_ast.h"
 #include "libbashParser.h"
 
 namespace po = boost::program_options;
@@ -50,15 +50,15 @@ BOOST_FUSION_ADAPT_STRUCT(
 
 static void print_ast(std::istream& input, bool silent, bool dot)
 {
-  parser_builder parser(input);
+  bash_ast ast(input);
 
   if(silent)
     return;
 
   if(dot)
-    std::cout << parser.get_dot_graph() << std::endl;
+    std::cout << ast.get_dot_graph() << std::endl;
   else
-    std::cout << parser.get_string_tree() << std::endl;
+    std::cout << ast.get_string_tree() << std::endl;
 }
 
 static inline std::string token_mapper(std::unordered_map<ANTLR3_INT32, std::string> token_map,
@@ -90,14 +90,14 @@ static inline void print_token(std::istream& input,
   if(silent)
     return;
 
-  parser_builder parser(input);
+  bash_ast ast(input);
   std::unordered_map<ANTLR3_INT32, std::string> token_map;
 
   if(build_token_map(token_map, token_path))
   {
-    std::cout << parser.get_tokens(std::bind(&token_mapper,
-                                             token_map,
-                                             std::placeholders::_1))
+    std::cout << ast.get_tokens(std::bind(&token_mapper,
+                                          token_map,
+                                          std::placeholders::_1))
     << std::endl;
   }
   else



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

only message in thread, other threads:[~2011-04-28  6:20 UTC | newest]

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