$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r69233 - in trunk/libs/spirit/example/qi/compiler_tutorial: . calc7
From: joel_at_[hidden]
Date: 2011-02-24 01:31:15
Author: djowel
Date: 2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
New Revision: 69233
URL: http://svn.boost.org/trac/boost/changeset/69233
Log:
more updates
Added:
   trunk/libs/spirit/example/qi/compiler_tutorial/Jamfile   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/error_handler.hpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.cpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp   (contents, props changed)
   trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp   (contents, props changed)
Text files modified: 
   trunk/libs/spirit/example/qi/compiler_tutorial/calc5.cpp |     2 +-                                      
   trunk/libs/spirit/example/qi/compiler_tutorial/calc6.cpp |     4 +++-                                    
   2 files changed, 4 insertions(+), 2 deletions(-)
Added: trunk/libs/spirit/example/qi/compiler_tutorial/Jamfile
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/Jamfile	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,28 @@
+#==============================================================================
+#   Copyright (c) 2001-2011 Joel de Guzman
+#
+#   Distributed under the Boost Software License, Version 1.0. (See accompanying
+#   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+#==============================================================================
+project spirit-qi-compiler_tutorial
+    : requirements
+        <toolset>gcc:<c++-template-depth>300
+        <toolset>darwin:<cxxflags>-ftemplate-depth-300
+    :
+    :
+    ;
+
+exe calc1 : calc1.cpp ;
+exe calc2 : calc2.cpp ;
+exe calc3 : calc3.cpp ;
+exe calc4 : calc4.cpp ;
+exe calc5 : calc5.cpp ;
+exe calc6 : calc6.cpp ;
+
+exe calc7 :
+    calc7/vm.cpp
+    calc7/compiler.cpp
+    calc7/expression.cpp
+    calc7/main.cpp
+    ;
+
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc5.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc5.cpp	(original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc5.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -285,7 +285,7 @@
     typedef std::string::const_iterator iterator_type;
     typedef client::calculator<iterator_type> calculator;
     typedef client::ast::program ast_program;
-    typedef client::ast::print ast_print;
+    typedef client::ast::printer ast_print;
     typedef client::ast::eval ast_eval;
 
     std::string str;
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc6.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc6.cpp	(original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc6.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -10,7 +10,9 @@
 //  virtual machine. This is actually one of the very first Spirit example
 //  circa 2000. Now, it's ported to Spirit2.
 //
-//  [ JDG April 28, 2008 : For BoostCon 2008 ]
+//  [ JDG Sometime 2000 ]       pre-boost
+//  [ JDG September 18, 2002 ]  spirit1
+//  [ JDG April 8, 2007 ]       spirit2
 //  [ JDG February 18, 2011 : Pure attributes. No semantic actions. ]
 //
 ///////////////////////////////////////////////////////////////////////////////
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,69 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_CALC7_AST_HPP)
+#define BOOST_SPIRIT_CALC7_AST_HPP
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/variant/recursive_variant.hpp>
+#include <boost/fusion/include/adapt_struct.hpp>
+#include <list>
+
+namespace client { namespace ast
+{
+    ///////////////////////////////////////////////////////////////////////////
+    //  The AST
+    ///////////////////////////////////////////////////////////////////////////
+    struct nil {};
+    struct signed_;
+    struct program;
+
+    typedef boost::variant<
+            nil
+          , unsigned int
+          , boost::recursive_wrapper<signed_>
+          , boost::recursive_wrapper<program>
+        >
+    operand;
+
+    struct signed_
+    {
+        char sign;
+        operand operand_;
+    };
+
+    struct operation
+    {
+        char operator_;
+        operand operand_;
+    };
+
+    struct program
+    {
+        operand first;
+        std::list<operation> rest;
+    };
+}}
+
+BOOST_FUSION_ADAPT_STRUCT(
+    client::ast::signed_,
+    (char, sign)
+    (client::ast::operand, operand_)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+    client::ast::operation,
+    (char, operator_)
+    (client::ast::operand, operand_)
+)
+
+BOOST_FUSION_ADAPT_STRUCT(
+    client::ast::program,
+    (client::ast::operand, first)
+    (std::list<client::ast::operation>, rest)
+)
+
+#endif
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,53 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include "compiler.hpp"
+#include "vm.hpp"
+#include <boost/foreach.hpp>
+#include <boost/variant/apply_visitor.hpp>
+
+namespace client
+{
+    void compiler::operator()(unsigned int n) const
+    {
+        code.push_back(op_int);
+        code.push_back(n);
+    }
+
+    void compiler::operator()(ast::operation const& x) const
+    {
+        boost::apply_visitor(*this, x.operand_);
+        switch (x.operator_)
+        {
+            case '+': code.push_back(op_add); break;
+            case '-': code.push_back(op_sub); break;
+            case '*': code.push_back(op_mul); break;
+            case '/': code.push_back(op_div); break;
+            default: BOOST_ASSERT(0); break;
+        }
+    }
+
+    void compiler::operator()(ast::signed_ const& x) const
+    {
+        boost::apply_visitor(*this, x.operand_);
+        switch (x.sign)
+        {
+            case '-': code.push_back(op_neg); break;
+            case '+': break;
+            default: BOOST_ASSERT(0); break;
+        }
+    }
+
+    void compiler::operator()(ast::program const& x) const
+    {
+        boost::apply_visitor(*this, x.first);
+        BOOST_FOREACH(ast::operation const& oper, x.rest)
+        {
+            (*this)(oper);
+        }
+    }
+}
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,34 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_CALC7_COMPILER_HPP)
+#define BOOST_SPIRIT_CALC7_COMPILER_HPP
+
+#include "ast.hpp"
+#include <vector>
+
+namespace client
+{
+    ///////////////////////////////////////////////////////////////////////////
+    //  The Compiler
+    ///////////////////////////////////////////////////////////////////////////
+    struct compiler
+    {
+        typedef void result_type;
+
+        std::vector<int>& code;
+        compiler(std::vector<int>& code)
+          : code(code) {}
+
+        void operator()(ast::nil) const { BOOST_ASSERT(0); }
+        void operator()(unsigned int n) const;
+        void operator()(ast::operation const& x) const;
+        void operator()(ast::signed_ const& x) const;
+        void operator()(ast::program const& x) const;
+    };
+}
+
+#endif
\ No newline at end of file
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/error_handler.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/error_handler.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,48 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_CALC7_ERROR_HANDLER_HPP)
+#define BOOST_SPIRIT_CALC7_ERROR_HANDLER_HPP
+
+#include <boost/spirit/include/support_info.hpp>
+#include <iostream>
+#include <string>
+
+namespace client
+{
+    namespace qi = boost::spirit::qi;
+    using boost::phoenix::function;
+
+    ///////////////////////////////////////////////////////////////////////////////
+    //  The error handler
+    ///////////////////////////////////////////////////////////////////////////////
+    struct error_handler_
+    {
+        template <typename, typename, typename>
+        struct result { typedef void type; };
+
+        template <typename Iterator>
+        void operator()(
+            boost::spirit::qi::info const& what
+          , Iterator err_pos, Iterator last) const
+        {
+            std::cout
+                << "Error! Expecting "
+                << what                         // what failed?
+                << " here: \""
+                << std::string(err_pos, last)   // iterators to error-pos, end
+                << "\""
+                << std::endl
+            ;
+        }
+    };
+
+    boost::phoenix::function<error_handler_> const
+        error_handler = error_handler_();
+}
+
+#endif
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,10 @@
+/*=============================================================================
+    Copyright (c) 2001-2010 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include "expression_def.hpp"
+
+typedef std::string::const_iterator iterator_type;
+template struct client::expression<iterator_type>;
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,51 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_CALC7_EXPRESSION_HPP)
+#define BOOST_SPIRIT_CALC7_EXPRESSION_HPP
+
+///////////////////////////////////////////////////////////////////////////////
+// Spirit v2.5 allows you to suppress automatic generation
+// of predefined terminals to speed up complation. With
+// BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
+// responsible in creating instances of the terminals that
+// you need (e.g. see qi::uint_type uint_ below).
+#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
+///////////////////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////////
+// Uncomment this if you want to enable debugging
+//#define BOOST_SPIRIT_QI_DEBUG
+///////////////////////////////////////////////////////////////////////////////
+
+#include <boost/spirit/include/qi.hpp>
+#include "error_handler.hpp"
+#include "ast.hpp"
+
+namespace client
+{
+    namespace qi = boost::spirit::qi;
+    namespace ascii = boost::spirit::ascii;
+
+    ///////////////////////////////////////////////////////////////////////////////
+    //  The expression grammar
+    ///////////////////////////////////////////////////////////////////////////////
+    template <typename Iterator>
+    struct expression : qi::grammar<Iterator, ast::program(), ascii::space_type>
+    {
+        expression();
+
+        qi::rule<Iterator, ast::program(), ascii::space_type> expr;
+        qi::rule<Iterator, ast::program(), ascii::space_type> additive_expr;
+        qi::rule<Iterator, ast::program(), ascii::space_type> multiplicative_expr;
+        qi::rule<Iterator, ast::operand(), ascii::space_type> unary_expr;
+        qi::rule<Iterator, ast::operand(), ascii::space_type> primary_expr;
+    };
+}
+
+#endif
+
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,64 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include "expression.hpp"
+
+namespace client
+{
+    template <typename Iterator>
+    expression<Iterator>::expression()
+      : expression::base_type(expr)
+    {
+        qi::char_type char_;
+        qi::uint_type uint_;
+        qi::_2_type _2;
+        qi::_3_type _3;
+        qi::_4_type _4;
+
+        using qi::on_error;
+        using qi::fail;
+
+        expr =
+            additive_expr.alias()
+            ;
+
+        additive_expr =
+            multiplicative_expr
+            >> *(   (char_('+') > multiplicative_expr)
+                |   (char_('-') > multiplicative_expr)
+                )
+            ;
+
+        multiplicative_expr =
+            unary_expr
+            >> *(   (char_('*') > unary_expr)
+                |   (char_('/') > unary_expr)
+                )
+            ;
+
+        unary_expr =
+                primary_expr
+            |   (char_('-') > primary_expr)
+            |   (char_('+') > primary_expr)
+            ;
+
+        primary_expr =
+            uint_
+            |   '(' > expr > ')'
+            ;
+
+        // Debugging and error handling and reporting support.
+        BOOST_SPIRIT_DEBUG_NODE(additive_expr);
+        BOOST_SPIRIT_DEBUG_NODE(multiplicative_expr);
+        BOOST_SPIRIT_DEBUG_NODE(unary_expr);
+        BOOST_SPIRIT_DEBUG_NODE(primary_expr);
+
+        // Error handling
+        on_error<fail>(expr, error_handler(_4, _3, _2));
+    }
+}
+
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,80 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+///////////////////////////////////////////////////////////////////////////////
+//
+//  Now we'll introduce variables and assignment. This time, we'll also
+//  be renaming some of the rules -- a strategy for a grander scheme
+//  to come ;-)
+//
+//  This version also shows off grammar modularization. Here you will
+//  see how expressions and statements are built as modular grammars.
+//
+//  [ JDG April 9, 2007 ]       spirit2
+//  [ JDG February 18, 2011 : Pure attributes. No semantic actions. ]
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "expression.hpp"
+#include "vm.hpp"
+#include "compiler.hpp"
+
+///////////////////////////////////////////////////////////////////////////////
+//  Main program
+///////////////////////////////////////////////////////////////////////////////
+int
+main()
+{
+    std::cout << "/////////////////////////////////////////////////////////\n\n";
+    std::cout << "Expression parser...\n\n";
+    std::cout << "/////////////////////////////////////////////////////////\n\n";
+    std::cout << "Type an expression...or [q or Q] to quit\n\n";
+
+    typedef std::string::const_iterator iterator_type;
+    typedef client::expression<iterator_type> expression;
+    typedef client::ast::program ast_program;
+    typedef client::compiler compiler;
+
+    std::string str;
+    while (std::getline(std::cin, str))
+    {
+        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
+            break;
+
+        client::vmachine mach;  // Our virtual machine
+        std::vector<int> code;  // Our VM code
+        expression calc;        // Our grammar
+        ast_program program;    // Our program (AST)
+        compiler compile(code); // Compiles the program
+
+        std::string::const_iterator iter = str.begin();
+        std::string::const_iterator end = str.end();
+        boost::spirit::ascii::space_type space;
+        bool r = phrase_parse(iter, end, calc, space, program);
+
+        if (r && iter == end)
+        {
+            std::cout << "-------------------------\n";
+            std::cout << "Parsing succeeded\n";
+            compile(program);
+            mach.execute(code);
+            std::cout << "\nResult: " << mach.top() << std::endl;
+            std::cout << "-------------------------\n";
+        }
+        else
+        {
+            std::string rest(iter, end);
+            std::cout << "-------------------------\n";
+            std::cout << "Parsing failed\n";
+            std::cout << "-------------------------\n";
+        }
+    }
+
+    std::cout << "Bye... :-) \n\n";
+    return 0;
+}
+
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,52 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#include "vm.hpp"
+
+namespace client
+{
+    void vmachine::execute(std::vector<int> const& code)
+    {
+        std::vector<int>::const_iterator pc = code.begin();
+        stack_ptr = stack.begin();
+
+        while (pc != code.end())
+        {
+            switch (*pc++)
+            {
+                case op_neg:
+                    stack_ptr[-1] = -stack_ptr[-1];
+                    break;
+
+                case op_add:
+                    --stack_ptr;
+                    stack_ptr[-1] += stack_ptr[0];
+                    break;
+
+                case op_sub:
+                    --stack_ptr;
+                    stack_ptr[-1] -= stack_ptr[0];
+                    break;
+
+                case op_mul:
+                    --stack_ptr;
+                    stack_ptr[-1] *= stack_ptr[0];
+                    break;
+
+                case op_div:
+                    --stack_ptr;
+                    stack_ptr[-1] /= stack_ptr[0];
+                    break;
+
+                case op_int:
+                    *stack_ptr++ = *pc++;
+                    break;
+            }
+        }
+    }
+}
+
+
Added: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp	2011-02-24 01:31:06 EST (Thu, 24 Feb 2011)
@@ -0,0 +1,48 @@
+/*=============================================================================
+    Copyright (c) 2001-2011 Joel de Guzman
+
+    Distributed under the Boost Software License, Version 1.0. (See accompanying
+    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_CALC7_VM_HPP)
+#define BOOST_SPIRIT_CALC7_VM_HPP
+
+#include <vector>
+
+namespace client
+{
+    ///////////////////////////////////////////////////////////////////////////
+    //  The Virtual Machine
+    ///////////////////////////////////////////////////////////////////////////
+    enum byte_code
+    {
+        op_neg,     //  negate the top stack entry
+        op_add,     //  add top two stack entries
+        op_sub,     //  subtract top two stack entries
+        op_mul,     //  multiply top two stack entries
+        op_div,     //  divide top two stack entries
+        op_int,     //  push constant integer into the stack
+    };
+
+    class vmachine
+    {
+    public:
+
+        vmachine(unsigned stackSize = 4096)
+          : stack(stackSize)
+          , stack_ptr(stack.begin())
+        {
+        }
+
+        int top() const { return stack_ptr[-1]; };
+        void execute(std::vector<int> const& code);
+
+    private:
+
+        std::vector<int> stack;
+        std::vector<int>::iterator stack_ptr;
+    };
+}
+
+#endif
+