From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QFKa7-0003af-CB for garchives@archives.gentoo.org; Thu, 28 Apr 2011 06:20:07 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 1B03A1C040; Thu, 28 Apr 2011 06:19:55 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id BD7271C040 for ; Thu, 28 Apr 2011 06:19:54 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 44EE72AC01F for ; Thu, 28 Apr 2011 06:19:54 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 976A080507 for ; Thu, 28 Apr 2011 06:19:53 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: Subject: [gentoo-commits] proj/libbash:master commit in: src/, /, src/core/, bashast/, test/, utils/ X-VCS-Repository: proj/libbash X-VCS-Files: Makefile.am bashast/libbashWalker.g src/core/bash_ast.cpp src/core/bash_ast.h src/core/parser_builder.cpp src/core/parser_builder.h src/core/walker_builder.cpp src/core/walker_builder.h src/libbash.cpp test/walker_test.cpp utils/ast_printer.cpp X-VCS-Directories: src/ / src/core/ bashast/ test/ utils/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: e86cc25a58e07c717606dbfd95d2497a9dd6f638 Date: Thu, 28 Apr 2011 06:19:53 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 0efb707cc52fcfdff697c74ce9eff26b commit: e86cc25a58e07c717606dbfd95d2497a9dd6f638 Author: Mu Qiao gentoo org> AuthorDate: Tue Apr 26 08:22:20 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Thu Apr 28 02:52:42 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3De86cc25a 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 =3D> bash_ast.cpp} | 27 ++++++++------ src/core/{parser_builder.h =3D> 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 =3D 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) =20 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 =20 @includes{ =20 - #include #include #include =20 class interpreter; - void set_interpreter(std::shared_ptr w); + void set_interpreter(interpreter* w); =20 } =20 @@ -50,9 +49,9 @@ options =20 @members{ =20 - static std::shared_ptr walker; + static interpreter* walker =3D 0; =20 - void set_interpreter(std::shared_ptr w) + void set_interpreter(interpreter* w) { walker =3D 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 . */ /// -/// \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 /// =20 -#include "core/parser_builder.h" +#include "core/bash_ast.h" =20 #include =20 #include "core/interpreter_exception.h" #include "libbashLexer.h" #include "libbashParser.h" -#include "walker_builder.h" +#include "libbashWalker.h" =20 -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(); } =20 -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); } =20 -void parser_builder::init_parser() +void bash_ast::init_parser() { lxr =3D libbashLexerNew(input); if ( lxr =3D=3D NULL ) @@ -72,18 +72,21 @@ void parser_builder::init_parser() nodes =3D antlr3CommonTreeNodeStreamNewTree(langAST->tree, ANTLR3_SIZE= _HINT); } =20 -walker_builder parser_builder::create_walker_builder() +void bash_ast::interpret_with(interpreter& walker) { - return walker_builder(nodes); + set_interpreter(&walker); + plibbashWalker treePsr =3D libbashWalkerNew(nodes); + treePsr->start(treePsr); + treePsr->free(treePsr); } =20 -std::string parser_builder::get_dot_graph() +std::string bash_ast::get_dot_graph() { pANTLR3_STRING graph =3D nodes->adaptor->makeDot(nodes->adaptor, langA= ST->tree); return std::string(reinterpret_cast(graph->chars)); } =20 -std::string parser_builder::get_string_tree() +std::string bash_ast::get_string_tree() { return std::string(reinterpret_cast( langAST->tree->toStringTree(langAST->tree)->chars)); @@ -102,7 +105,7 @@ namespace } } =20 -std::string parser_builder::get_tokens(std::function token_map) +std::string bash_ast::get_tokens(std::function token_map) { std::stringstream result; int line_counter =3D 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 . */ /// -/// \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 /// =20 #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; =20 -/// \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 =20 void init_parser(); public: - explicit parser_builder(std::istream& source); - ~parser_builder(); + explicit bash_ast(std::istream& source); + ~bash_ast(); =20 /// - /// \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 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 . -*/ -/// -/// \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): w= alker(new interpreter) -{ - set_interpreter(walker); - plibbashWalker treePsr =3D 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 . -*/ -/// -/// \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 - -#include - -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 informatio= n - std::shared_ptr 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 =20 #include "core/interpreter.h" -#include "core/parser_builder.h" -#include "core/walker_builder.h" +#include "core/bash_ast.h" =20 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 =3D pbuilder.create_walker_builder(); + interpreter walker; + bash_ast ast(input); + ast.interpret_with(walker); =20 - for(auto iter =3D wbuilder.walker->begin(); iter !=3D wbuilder.walke= r->end(); ++iter) + for(auto iter =3D walker.begin(); iter !=3D walker.end(); ++iter) iter->second->get_all_values(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 =20 #include "core/interpreter.h" -#include "core/parser_builder.h" -#include "core/walker_builder.h" +#include "core/bash_ast.h" =20 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 =3D pbuilder.create_walker_builder(); - EXPECT_STREQ(exp_value, wbuilder.walker->resolve(name).c_= str()); + bash_ast ast(input); + ast.interpret_with(walker); + EXPECT_STREQ(exp_value, walker.resolve(name).c_str()); } =20 #define TEST_STRING_ASSIGNMENT(name, script, var_name, exp_value)\ @@ -62,13 +62,15 @@ TEST_STRING_ASSIGNMENT(str_assignment6, =20 TEST(array_index, out_of_bound) { + interpreter walker; + std::string script =3D "a[-1]=3D\"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); =20 std::string script2 =3D "a=3D(1 2 [-5]=3D1)"; 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 #include =20 -#include "core/parser_builder.h" +#include "core/bash_ast.h" #include "libbashParser.h" =20 namespace po =3D boost::program_options; @@ -50,15 +50,15 @@ BOOST_FUSION_ADAPT_STRUCT( =20 static void print_ast(std::istream& input, bool silent, bool dot) { - parser_builder parser(input); + bash_ast ast(input); =20 if(silent) return; =20 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; } =20 static inline std::string token_mapper(std::unordered_map token_map, @@ -90,14 +90,14 @@ static inline void print_token(std::istream& input, if(silent) return; =20 - parser_builder parser(input); + bash_ast ast(input); std::unordered_map token_map; =20 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