|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r69297 - in trunk/libs/spirit/example/qi/compiler_tutorial: . calc7
From: joel_at_[hidden]
Date: 2011-02-26 03:35:19
Author: djowel
Date: 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
New Revision: 69297
URL: http://svn.boost.org/trac/boost/changeset/69297
Log:
updates
Text files modified:
trunk/libs/spirit/example/qi/compiler_tutorial/calc6.cpp | 5 ++---
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp | 10 ++++++++++
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp | 30 ++++++++++++++++++++----------
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp | 4 ++++
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp | 5 ++++-
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp | 24 +++++++++++++++++++-----
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp | 2 +-
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp | 14 ++++++++++++++
trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp | 4 ++++
9 files changed, 78 insertions(+), 20 deletions(-)
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-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -291,9 +291,8 @@
;
// Debugging and error handling and reporting support.
- BOOST_SPIRIT_DEBUG_NODE(expression);
- BOOST_SPIRIT_DEBUG_NODE(term);
- BOOST_SPIRIT_DEBUG_NODE(factor);
+ BOOST_SPIRIT_DEBUG_NODES(
+ (expression)(term)(factor));
// Error handling
on_error<fail>(expression, error_handler(_4, _3, _2));
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -20,12 +20,14 @@
struct nil {};
struct signed_;
struct program;
+ struct tagged;
typedef boost::variant<
nil
, unsigned int
, boost::recursive_wrapper<signed_>
, boost::recursive_wrapper<program>
+ , boost::recursive_wrapper<tagged>
>
operand;
@@ -46,6 +48,14 @@
operand first;
std::list<operation> rest;
};
+
+ struct tagged
+ {
+ operand operand_;
+ int id; // Used to annotate the iterator position. This
+ // id is used as a key to a map<int, Iterator>
+ // (not really part of the AST.)
+ };
}}
BOOST_FUSION_ADAPT_STRUCT(
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.cpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -13,29 +13,34 @@
{
void compiler::operator()(unsigned int n) const
{
- code.push_back(op_int);
- code.push_back(n);
+ op(op_int);
+ op(n);
+ }
+
+ void compiler::operator()(ast::operand const& x) const
+ {
+ boost::apply_visitor(*this, x);
}
void compiler::operator()(ast::operation const& x) const
{
- boost::apply_visitor(*this, x.operand_);
+ (*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;
+ case '+': op(op_add); break;
+ case '-': op(op_sub); break;
+ case '*': op(op_mul); break;
+ case '/': op(op_div); break;
default: BOOST_ASSERT(0); break;
}
}
void compiler::operator()(ast::signed_ const& x) const
{
- boost::apply_visitor(*this, x.operand_);
+ (*this)(x.operand_);
switch (x.sign)
{
- case '-': code.push_back(op_neg); break;
+ case '-': op(op_neg); break;
case '+': break;
default: BOOST_ASSERT(0); break;
}
@@ -43,11 +48,16 @@
void compiler::operator()(ast::program const& x) const
{
- boost::apply_visitor(*this, x.first);
+ (*this)(x.first);
BOOST_FOREACH(ast::operation const& oper, x.rest)
{
(*this)(oper);
}
}
+
+ void compiler::operator()(ast::tagged const& x) const
+ {
+ (*this)(x.operand_);
+ }
}
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -23,11 +23,15 @@
compiler(std::vector<int>& code)
: code(code) {}
+ void op(int opcode) const { code.push_back(opcode); }
+
void operator()(ast::nil) const { BOOST_ASSERT(0); }
void operator()(unsigned int n) const;
+ void operator()(ast::operand const& x) const;
void operator()(ast::operation const& x) const;
void operator()(ast::signed_ const& x) const;
void operator()(ast::program const& x) const;
+ void operator()(ast::tagged const& x) const;
};
}
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression.hpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -22,8 +22,8 @@
///////////////////////////////////////////////////////////////////////////////
#include <boost/spirit/include/qi.hpp>
-#include "error_handler.hpp"
#include "ast.hpp"
+#include <map>
namespace client
{
@@ -43,6 +43,9 @@
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;
+
+ std::map<int, Iterator> iters;
+ int current_id;
};
}
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/expression_def.hpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -5,20 +5,25 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "expression.hpp"
+#include "error_handler.hpp"
+#include "annotation.hpp"
namespace client
{
template <typename Iterator>
expression<Iterator>::expression()
: expression::base_type(expr)
+ , current_id(0)
{
qi::char_type char_;
qi::uint_type uint_;
+ qi::_val_type _val;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
using qi::on_error;
+ using qi::on_success;
using qi::fail;
expr =
@@ -51,13 +56,22 @@
;
// 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);
+ BOOST_SPIRIT_DEBUG_NODES(
+ (expr)
+ (additive_expr)
+ (multiplicative_expr)
+ (unary_expr)
+ (primary_expr)
+ );
- // Error handling
+ // Error handling: on error in expr, call error_handler.
on_error<fail>(expr, error_handler(_4, _3, _2));
+
+ // Annotation: on success in primary_expr, call annotation.
+ typedef client::annotation<Iterator> annotation_;
+ typename function<annotation_>
+ annotation = annotation_(current_id, iters);
+ on_success(primary_expr, annotation(_val, _3));
}
}
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/main.cpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -48,7 +48,7 @@
std::vector<int> code; // Our VM code
expression calc; // Our grammar
ast_program program; // Our program (AST)
- compiler compile(code); // Compiles the program
+ compiler compile(code); // Our compiler
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.cpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -11,6 +11,7 @@
void vmachine::execute(std::vector<int> const& code)
{
std::vector<int>::const_iterator pc = code.begin();
+ std::vector<int>::iterator locals = stack.begin();
stack_ptr = stack.begin();
while (pc != code.end())
@@ -41,9 +42,22 @@
stack_ptr[-1] /= stack_ptr[0];
break;
+ case op_load:
+ *stack_ptr++ = locals[*pc++];
+ break;
+
+ case op_store:
+ --stack_ptr;
+ locals[*pc++] = stack_ptr[0];
+ break;
+
case op_int:
*stack_ptr++ = *pc++;
break;
+
+ case op_adstk:
+ stack_ptr = stack.begin() + *pc++;
+ break;
}
}
}
Modified: trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp
==============================================================================
--- trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp (original)
+++ trunk/libs/spirit/example/qi/compiler_tutorial/calc7/vm.hpp 2011-02-26 03:35:11 EST (Sat, 26 Feb 2011)
@@ -21,7 +21,11 @@
op_sub, // subtract top two stack entries
op_mul, // multiply top two stack entries
op_div, // divide top two stack entries
+
+ op_load, // load a variable
+ op_store, // store a variable
op_int, // push constant integer into the stack
+ op_adstk // adjust the stack for local variables
};
class vmachine
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk