summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/example/x3/calc/calc8
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/spirit/example/x3/calc/calc8
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/spirit/example/x3/calc/calc8')
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp98
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp33
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/common.hpp28
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp217
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp80
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/config.hpp26
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp44
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp13
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp26
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp91
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/main.cpp114
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp13
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp26
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp84
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp64
-rw-r--r--src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp51
16 files changed, 1008 insertions, 0 deletions
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp
new file mode 100644
index 00000000..aacde561
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/ast.hpp
@@ -0,0 +1,98 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_AST_HPP)
+#define BOOST_SPIRIT_X3_CALC8_AST_HPP
+
+#include <boost/spirit/home/x3/support/ast/variant.hpp>
+#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
+#include <boost/fusion/include/io.hpp>
+#include <list>
+
+namespace client { namespace ast
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // The AST
+ ///////////////////////////////////////////////////////////////////////////
+ namespace x3 = boost::spirit::x3;
+
+ struct nil {};
+ struct signed_;
+ struct expression;
+
+ struct variable : x3::position_tagged
+ {
+ variable(std::string const& name = "") : name(name) {}
+ std::string name;
+ };
+
+ struct operand :
+ x3::variant<
+ nil
+ , unsigned int
+ , variable
+ , x3::forward_ast<signed_>
+ , x3::forward_ast<expression>
+ >
+ {
+ using base_type::base_type;
+ using base_type::operator=;
+ };
+
+ struct signed_
+ {
+ char sign;
+ operand operand_;
+ };
+
+ struct operation : x3::position_tagged
+ {
+ char operator_;
+ operand operand_;
+ };
+
+ struct expression : x3::position_tagged
+ {
+ operand first;
+ std::list<operation> rest;
+ };
+
+ struct assignment : x3::position_tagged
+ {
+ variable lhs;
+ expression rhs;
+ };
+
+ struct variable_declaration
+ {
+ assignment assign;
+ };
+
+ struct statement :
+ x3::variant<
+ variable_declaration
+ , assignment>
+ {
+ using base_type::base_type;
+ using base_type::operator=;
+ };
+
+ typedef std::list<statement> statement_list;
+
+ // print functions for debugging
+ inline std::ostream& operator<<(std::ostream& out, nil)
+ {
+ out << "nil";
+ return out;
+ }
+
+ inline std::ostream& operator<<(std::ostream& out, variable const& var)
+ {
+ out << var.name; return out;
+ }
+}}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp
new file mode 100644
index 00000000..ca776109
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/ast_adapted.hpp
@@ -0,0 +1,33 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_AST_ADAPTED_HPP)
+#define BOOST_SPIRIT_X3_CALC8_AST_ADAPTED_HPP
+
+#include "ast.hpp"
+#include <boost/fusion/include/adapt_struct.hpp>
+
+BOOST_FUSION_ADAPT_STRUCT(client::ast::signed_,
+ sign, operand_
+)
+
+BOOST_FUSION_ADAPT_STRUCT(client::ast::operation,
+ operator_, operand_
+)
+
+BOOST_FUSION_ADAPT_STRUCT(client::ast::expression,
+ first, rest
+)
+
+BOOST_FUSION_ADAPT_STRUCT(client::ast::variable_declaration,
+ assign
+)
+
+BOOST_FUSION_ADAPT_STRUCT(client::ast::assignment,
+ lhs, rhs
+)
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp
new file mode 100644
index 00000000..0a559573
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/common.hpp
@@ -0,0 +1,28 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_COMMON_HPP)
+#define BOOST_SPIRIT_X3_CALC8_COMMON_HPP
+
+#include <boost/spirit/home/x3.hpp>
+
+namespace client { namespace parser
+{
+ using x3::raw;
+ using x3::lexeme;
+ using x3::alpha;
+ using x3::alnum;
+
+ struct identifier_class;
+ typedef x3::rule<identifier_class, std::string> identifier_type;
+ identifier_type const identifier = "identifier";
+
+ auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]];
+
+ BOOST_SPIRIT_DEFINE(identifier);
+}}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp
new file mode 100644
index 00000000..6410a981
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.cpp
@@ -0,0 +1,217 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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/variant/apply_visitor.hpp>
+#include <boost/assert.hpp>
+#include <iostream>
+
+namespace client { namespace code_gen
+{
+ void program::op(int a)
+ {
+ code.push_back(a);
+ }
+
+ void program::op(int a, int b)
+ {
+ code.push_back(a);
+ code.push_back(b);
+ }
+
+ void program::op(int a, int b, int c)
+ {
+ code.push_back(a);
+ code.push_back(b);
+ code.push_back(c);
+ }
+
+ int const* program::find_var(std::string const& name) const
+ {
+ auto i = variables.find(name);
+ if (i == variables.end())
+ return 0;
+ return &i->second;
+ }
+
+ void program::add_var(std::string const& name)
+ {
+ std::size_t n = variables.size();
+ variables[name] = int(n);
+ }
+
+ void program::print_variables(std::vector<int> const& stack) const
+ {
+ for (auto const& p : variables)
+ {
+ std::cout << " " << p.first << ": " << stack[p.second] << std::endl;
+ }
+ }
+
+ void program::print_assembler() const
+ {
+ auto pc = code.begin();
+
+ std::vector<std::string> locals(variables.size());
+ typedef std::pair<std::string, int> pair;
+ for (pair const& p : variables)
+ {
+ locals[p.second] = p.first;
+ std::cout << "local "
+ << p.first << ", @" << p.second << std::endl;
+ }
+
+ while (pc != code.end())
+ {
+ switch (*pc++)
+ {
+ case op_neg:
+ std::cout << "op_neg" << std::endl;
+ break;
+
+ case op_add:
+ std::cout << "op_add" << std::endl;
+ break;
+
+ case op_sub:
+ std::cout << "op_sub" << std::endl;
+ break;
+
+ case op_mul:
+ std::cout << "op_mul" << std::endl;
+ break;
+
+ case op_div:
+ std::cout << "op_div" << std::endl;
+ break;
+
+ case op_load:
+ std::cout << "op_load " << locals[*pc++] << std::endl;
+ break;
+
+ case op_store:
+ std::cout << "op_store " << locals[*pc++] << std::endl;
+ break;
+
+ case op_int:
+ std::cout << "op_int " << *pc++ << std::endl;
+ break;
+
+ case op_stk_adj:
+ std::cout << "op_stk_adj " << *pc++ << std::endl;
+ break;
+ }
+ }
+ }
+
+ bool compiler::operator()(unsigned int x) const
+ {
+ program.op(op_int, x);
+ return true;
+ }
+
+ bool compiler::operator()(ast::variable const& x) const
+ {
+ int const* p = program.find_var(x.name);
+ if (p == 0)
+ {
+ error_handler(x, "Undeclared variable: " + x.name);
+ return false;
+ }
+ program.op(op_load, *p);
+ return true;
+ }
+
+ bool compiler::operator()(ast::operation const& x) const
+ {
+ if (!boost::apply_visitor(*this, x.operand_))
+ return false;
+ switch (x.operator_)
+ {
+ case '+': program.op(op_add); break;
+ case '-': program.op(op_sub); break;
+ case '*': program.op(op_mul); break;
+ case '/': program.op(op_div); break;
+ default: BOOST_ASSERT(0); return false;
+ }
+ return true;
+ }
+
+ bool compiler::operator()(ast::signed_ const& x) const
+ {
+ if (!boost::apply_visitor(*this, x.operand_))
+ return false;
+ switch (x.sign)
+ {
+ case '-': program.op(op_neg); break;
+ case '+': break;
+ default: BOOST_ASSERT(0); return false;
+ }
+ return true;
+ }
+
+ bool compiler::operator()(ast::expression const& x) const
+ {
+ if (!boost::apply_visitor(*this, x.first))
+ return false;
+ for (ast::operation const& oper : x.rest)
+ {
+ if (!(*this)(oper))
+ return false;
+ }
+ return true;
+ }
+
+ bool compiler::operator()(ast::assignment const& x) const
+ {
+ if (!(*this)(x.rhs))
+ return false;
+ int const* p = program.find_var(x.lhs.name);
+ if (p == 0)
+ {
+ error_handler(x.lhs, "Undeclared variable: " + x.lhs.name);
+ return false;
+ }
+ program.op(op_store, *p);
+ return true;
+ }
+
+ bool compiler::operator()(ast::variable_declaration const& x) const
+ {
+ int const* p = program.find_var(x.assign.lhs.name);
+ if (p != 0)
+ {
+ error_handler(x.assign.lhs, "Duplicate variable: " + x.assign.lhs.name);
+ return false;
+ }
+ bool r = (*this)(x.assign.rhs);
+ if (r) // don't add the variable if the RHS fails
+ {
+ program.add_var(x.assign.lhs.name);
+ program.op(op_store, *program.find_var(x.assign.lhs.name));
+ }
+ return r;
+ }
+
+ bool compiler::operator()(ast::statement_list const& x) const
+ {
+ program.clear();
+
+ // op_stk_adj 0 for now. we'll know how many variables we'll have later
+ program.op(op_stk_adj, 0);
+ for (ast::statement const& s : x)
+ {
+ if (!boost::apply_visitor(*this, s))
+ {
+ program.clear();
+ return false;
+ }
+ }
+ program[1] = int(program.nvars()); // now store the actual number of variables
+ return true;
+ }
+}}
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp
new file mode 100644
index 00000000..509298c9
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/compiler.hpp
@@ -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)
+=============================================================================*/
+#if !defined(BOOST_SPIRIT_X3_CALC8_COMPILER_HPP)
+#define BOOST_SPIRIT_X3_CALC8_COMPILER_HPP
+
+#include "ast.hpp"
+#include "error_handler.hpp"
+#include <vector>
+#include <map>
+
+namespace client { namespace code_gen
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // The Program
+ ///////////////////////////////////////////////////////////////////////////
+ struct program
+ {
+ void op(int a);
+ void op(int a, int b);
+ void op(int a, int b, int c);
+
+ int& operator[](std::size_t i) { return code[i]; }
+ int operator[](std::size_t i) const { return code[i]; }
+ void clear() { code.clear(); variables.clear(); }
+ std::vector<int> const& operator()() const { return code; }
+
+ std::size_t nvars() const { return variables.size(); }
+ int const* find_var(std::string const& name) const;
+ void add_var(std::string const& name);
+
+ void print_variables(std::vector<int> const& stack) const;
+ void print_assembler() const;
+
+ private:
+
+ std::map<std::string, int> variables;
+ std::vector<int> code;
+ };
+
+ ////////////////////////////////////////////////////////////////////////////
+ // The Compiler
+ ////////////////////////////////////////////////////////////////////////////
+ struct compiler
+ {
+ typedef bool result_type;
+ typedef std::function<
+ void(x3::position_tagged, std::string const&)>
+ error_handler_type;
+
+ template <typename ErrorHandler>
+ compiler(
+ client::code_gen::program& program
+ , ErrorHandler const& error_handler)
+ : program(program)
+ , error_handler(
+ [&](x3::position_tagged pos, std::string const& msg)
+ { error_handler(pos, msg); }
+ )
+ {}
+
+ bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
+ bool operator()(unsigned int x) const;
+ bool operator()(ast::variable const& x) const;
+ bool operator()(ast::operation const& x) const;
+ bool operator()(ast::signed_ const& x) const;
+ bool operator()(ast::expression const& x) const;
+ bool operator()(ast::assignment const& x) const;
+ bool operator()(ast::variable_declaration const& x) const;
+ bool operator()(ast::statement_list const& x) const;
+
+ client::code_gen::program& program;
+ error_handler_type error_handler;
+ };
+}}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp
new file mode 100644
index 00000000..d6664564
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/config.hpp
@@ -0,0 +1,26 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_CONFIG_HPP)
+#define BOOST_SPIRIT_X3_CALC8_CONFIG_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include "error_handler.hpp"
+
+namespace client { namespace parser
+{
+ typedef std::string::const_iterator iterator_type;
+ typedef x3::phrase_parse_context<x3::ascii::space_type>::type phrase_context_type;
+ typedef error_handler<iterator_type> error_handler_type;
+
+ typedef x3::context<
+ error_handler_tag
+ , std::reference_wrapper<error_handler_type>
+ , phrase_context_type>
+ context_type;
+}}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp
new file mode 100644
index 00000000..25cb268c
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/error_handler.hpp
@@ -0,0 +1,44 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_ERROR_HANDLER_HPP)
+#define BOOST_SPIRIT_X3_CALC8_ERROR_HANDLER_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
+#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
+#include "expression.hpp"
+#include "statement.hpp"
+
+namespace client { namespace parser
+{
+ namespace x3 = boost::spirit::x3;
+
+ ////////////////////////////////////////////////////////////////////////////
+ // Our error handler
+ ////////////////////////////////////////////////////////////////////////////
+ template <typename Iterator>
+ using error_handler = x3::error_handler<Iterator>;
+
+ // tag used to get our error handler from the context
+ using error_handler_tag = x3::error_handler_tag;
+
+ struct error_handler_base
+ {
+ template <typename Iterator, typename Exception, typename Context>
+ x3::error_handler_result on_error(
+ Iterator& first, Iterator const& last
+ , Exception const& x, Context const& context)
+ {
+ std::string message = "Error! Expecting: " + x.which() + " here:";
+ auto& error_handler = x3::get<error_handler_tag>(context).get();
+ error_handler(x.where(), message);
+ return x3::error_handler_result::fail;
+ }
+ };
+}}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp
new file mode 100644
index 00000000..e7463cd3
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression.cpp
@@ -0,0 +1,13 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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"
+#include "config.hpp"
+
+namespace client { namespace parser
+{
+ BOOST_SPIRIT_INSTANTIATE(expression_type, iterator_type, context_type);
+}}
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp
new file mode 100644
index 00000000..d619510c
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression.hpp
@@ -0,0 +1,26 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_EXPRESSION_HPP)
+#define BOOST_SPIRIT_X3_CALC8_EXPRESSION_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include "ast.hpp"
+
+namespace client
+{
+ namespace x3 = boost::spirit::x3;
+ namespace parser
+ {
+ struct expression_class;
+ typedef x3::rule<expression_class, ast::expression> expression_type;
+ BOOST_SPIRIT_DECLARE(expression_type);
+ }
+
+ parser::expression_type const& expression();
+}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp
new file mode 100644
index 00000000..fe9eaee9
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/expression_def.hpp
@@ -0,0 +1,91 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_EXPRESSION_DEF_HPP)
+#define BOOST_SPIRIT_X3_CALC8_EXPRESSION_DEF_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
+#include "ast.hpp"
+#include "ast_adapted.hpp"
+#include "expression.hpp"
+#include "common.hpp"
+#include "error_handler.hpp"
+
+namespace client { namespace parser
+{
+ using x3::uint_;
+ using x3::char_;
+ using x3::raw;
+ using x3::lexeme;
+ using namespace x3::ascii;
+
+ struct additive_expr_class;
+ struct multiplicative_expr_class;
+ struct unary_expr_class;
+ struct primary_expr_class;
+
+ typedef x3::rule<additive_expr_class, ast::expression> additive_expr_type;
+ typedef x3::rule<multiplicative_expr_class, ast::expression> multiplicative_expr_type;
+ typedef x3::rule<unary_expr_class, ast::operand> unary_expr_type;
+ typedef x3::rule<primary_expr_class, ast::operand> primary_expr_type;
+
+ expression_type const expression = "expression";
+ additive_expr_type const additive_expr = "additive_expr";
+ multiplicative_expr_type const multiplicative_expr = "multiplicative_expr";
+ unary_expr_type unary_expr = "unary_expr";
+ primary_expr_type primary_expr = "primary_expr";
+
+ auto const additive_expr_def =
+ multiplicative_expr
+ >> *( (char_('+') > multiplicative_expr)
+ | (char_('-') > multiplicative_expr)
+ )
+ ;
+
+ auto const multiplicative_expr_def =
+ unary_expr
+ >> *( (char_('*') > unary_expr)
+ | (char_('/') > unary_expr)
+ )
+ ;
+
+ auto const unary_expr_def =
+ primary_expr
+ | (char_('-') > primary_expr)
+ | (char_('+') > primary_expr)
+ ;
+
+ auto const primary_expr_def =
+ uint_
+ | identifier
+ | '(' > expression > ')'
+ ;
+
+ auto const expression_def = additive_expr;
+
+ BOOST_SPIRIT_DEFINE(
+ expression
+ , additive_expr
+ , multiplicative_expr
+ , unary_expr
+ , primary_expr
+ );
+
+ struct unary_expr_class : x3::annotate_on_success {};
+ struct primary_expr_class : x3::annotate_on_success {};
+
+}}
+
+namespace client
+{
+ parser::expression_type const& expression()
+ {
+ return parser::expression;
+ }
+}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp
new file mode 100644
index 00000000..e87ccbb7
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp
@@ -0,0 +1,114 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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.
+// [ JDG May 17, 2014 ] Ported from qi calc7 example.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "ast.hpp"
+#include "vm.hpp"
+#include "compiler.hpp"
+#include "statement.hpp"
+#include "error_handler.hpp"
+#include "config.hpp"
+#include <iostream>
+
+///////////////////////////////////////////////////////////////////////////////
+// Main program
+///////////////////////////////////////////////////////////////////////////////
+int
+main()
+{
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+ std::cout << "Statement parser...\n\n";
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+ std::cout << "Type some statements... ";
+ std::cout << "An empty line ends input, compiles, runs and prints results\n\n";
+ std::cout << "Example:\n\n";
+ std::cout << " var a = 123;\n";
+ std::cout << " var b = 456;\n";
+ std::cout << " var c = a + b * 2;\n\n";
+ std::cout << "-------------------------\n";
+
+ std::string str;
+ std::string source;
+ while (std::getline(std::cin, str))
+ {
+ if (str.empty())
+ break;
+ source += str + '\n';
+ }
+
+ using client::parser::iterator_type;
+ iterator_type iter(source.begin());
+ iterator_type end(source.end());
+
+ client::vmachine vm; // Our virtual machine
+ client::code_gen::program program; // Our VM program
+ client::ast::statement_list ast; // Our AST
+
+ using boost::spirit::x3::with;
+ using client::parser::error_handler_type;
+ using client::parser::error_handler_tag;
+ error_handler_type error_handler(iter, end, std::cerr); // Our error handler
+
+ // Our compiler
+ client::code_gen::compiler compile(program, error_handler);
+
+ // Our parser
+ auto const parser =
+ // we pass our error handler to the parser so we can access
+ // it later on in our on_error and on_sucess handlers
+ with<error_handler_tag>(std::ref(error_handler))
+ [
+ client::statement()
+ ];
+
+ using boost::spirit::x3::ascii::space;
+ bool success = phrase_parse(iter, end, parser, space, ast);
+
+ std::cout << "-------------------------\n";
+
+ if (success && iter == end)
+ {
+ if (compile(ast))
+ {
+ std::cout << "Success\n";
+ std::cout << "-------------------------\n";
+ vm.execute(program());
+
+ std::cout << "-------------------------\n";
+ std::cout << "Assembler----------------\n\n";
+ program.print_assembler();
+
+ std::cout << "-------------------------\n";
+ std::cout << "Results------------------\n\n";
+ program.print_variables(vm.get_stack());
+ }
+ else
+ {
+ std::cout << "Compile failure\n";
+ }
+ }
+ else
+ {
+ std::cout << "Parse failure\n";
+ }
+
+ std::cout << "-------------------------\n\n";
+ return 0;
+}
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp
new file mode 100644
index 00000000..ec4bca61
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement.cpp
@@ -0,0 +1,13 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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 "statement_def.hpp"
+#include "config.hpp"
+
+namespace client { namespace parser
+{
+ BOOST_SPIRIT_INSTANTIATE(statement_type, iterator_type, context_type);
+}}
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp
new file mode 100644
index 00000000..7b8973d0
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement.hpp
@@ -0,0 +1,26 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_STATEMENT_HPP)
+#define BOOST_SPIRIT_X3_CALC8_STATEMENT_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include "ast.hpp"
+
+namespace client
+{
+ namespace x3 = boost::spirit::x3;
+ namespace parser
+ {
+ struct statement_class;
+ typedef x3::rule<statement_class, ast::statement_list> statement_type;
+ BOOST_SPIRIT_DECLARE(statement_type);
+ }
+
+ parser::statement_type const& statement();
+}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp
new file mode 100644
index 00000000..8772c76f
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/statement_def.hpp
@@ -0,0 +1,84 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_STATEMENT_DEF_HPP)
+#define BOOST_SPIRIT_X3_CALC8_STATEMENT_DEF_HPP
+
+#include <boost/spirit/home/x3.hpp>
+#include <boost/spirit/home/x3/support/utility/annotate_on_success.hpp>
+#include "ast.hpp"
+#include "ast_adapted.hpp"
+#include "statement.hpp"
+#include "expression.hpp"
+#include "common.hpp"
+#include "error_handler.hpp"
+
+namespace client { namespace parser
+{
+ using x3::raw;
+ using x3::lexeme;
+ using namespace x3::ascii;
+
+ struct statement_list_class;
+ struct variable_declaration_class;
+ struct assignment_class;
+ struct variable_class;
+
+ typedef x3::rule<statement_list_class, ast::statement_list> statement_list_type;
+ typedef x3::rule<variable_declaration_class, ast::variable_declaration> variable_declaration_type;
+ typedef x3::rule<assignment_class, ast::assignment> assignment_type;
+ typedef x3::rule<variable_class, ast::variable> variable_type;
+
+ statement_type const statement = "statement";
+ statement_list_type const statement_list = "statement_list";
+ variable_declaration_type const variable_declaration = "variable_declaration";
+ assignment_type const assignment = "assignment";
+ variable_type const variable = "variable";
+
+ // Import the expression rule
+ namespace { auto const& expression = client::expression(); }
+
+ auto const statement_list_def =
+ +(variable_declaration | assignment)
+ ;
+
+ auto const variable_declaration_def =
+ lexeme["var" >> !(alnum | '_')] // make sure we have whole words
+ > assignment
+ ;
+
+ auto const assignment_def =
+ variable
+ > '='
+ > expression
+ > ';'
+ ;
+
+ auto const variable_def = identifier;
+ auto const statement_def = statement_list;
+
+ BOOST_SPIRIT_DEFINE(
+ statement
+ , statement_list
+ , variable_declaration
+ , assignment
+ , variable
+ );
+
+ struct statement_class : error_handler_base, x3::annotate_on_success {};
+ struct assignment_class : x3::annotate_on_success {};
+ struct variable_class : x3::annotate_on_success {};
+}}
+
+namespace client
+{
+ parser::statement_type const& statement()
+ {
+ return parser::statement;
+ }
+}
+
+#endif
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp b/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp
new file mode 100644
index 00000000..ff6c8400
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/vm.cpp
@@ -0,0 +1,64 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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)
+ {
+ auto pc = code.begin();
+ auto locals = stack.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_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_stk_adj:
+ stack_ptr = stack.begin() + *pc++;
+ break;
+ }
+ }
+ }
+}
diff --git a/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp b/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp
new file mode 100644
index 00000000..29072977
--- /dev/null
+++ b/src/boost/libs/spirit/example/x3/calc/calc8/vm.hpp
@@ -0,0 +1,51 @@
+/*=============================================================================
+ Copyright (c) 2001-2014 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_X3_CALC8_VM_HPP)
+#define BOOST_SPIRIT_X3_CALC8_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_load, // load a variable
+ op_store, // store a variable
+ op_int, // push constant integer into the stack
+ op_stk_adj // adjust the stack for local variables
+ };
+
+ class vmachine
+ {
+ public:
+
+ vmachine(unsigned stackSize = 4096)
+ : stack(stackSize)
+ , stack_ptr(stack.begin())
+ {
+ }
+
+ void execute(std::vector<int> const& code);
+ std::vector<int> const& get_stack() const { return stack; };
+
+ private:
+
+ std::vector<int> stack;
+ std::vector<int>::iterator stack_ptr;
+ };
+}
+
+#endif