1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(BOOST_SPIRIT_CALC7_COMPILER_HPP)
8 #define BOOST_SPIRIT_CALC7_COMPILER_HPP
11 #include "error_handler.hpp"
14 #include <boost/function.hpp>
15 #include <boost/spirit/include/phoenix_core.hpp>
16 #include <boost/spirit/include/phoenix_function.hpp>
17 #include <boost/spirit/include/phoenix_operator.hpp>
19 namespace client { namespace code_gen
21 ///////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////
27 void op(int a, int b);
28 void op(int a, int b, int c);
30 int& operator[](std::size_t i) { return code[i]; }
31 int const& operator[](std::size_t i) const { return code[i]; }
32 void clear() { code.clear(); variables.clear(); }
33 std::vector<int> const& operator()() const { return code; }
35 int nvars() const { return variables.size(); }
36 int const* find_var(std::string const& name) const;
37 void add_var(std::string const& name);
39 void print_variables(std::vector<int> const& stack) const;
40 void print_assembler() const;
44 std::map<std::string, int> variables;
45 std::vector<int> code;
48 ///////////////////////////////////////////////////////////////////////////
50 ///////////////////////////////////////////////////////////////////////////
53 typedef bool result_type;
55 template <typename ErrorHandler>
56 compiler(client::code_gen::program& program, ErrorHandler& error_handler_)
59 using namespace boost::phoenix::arg_names;
60 namespace phx = boost::phoenix;
61 using boost::phoenix::function;
63 error_handler = function<ErrorHandler>(error_handler_)(
64 "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
67 bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
68 bool operator()(unsigned int x) const;
69 bool operator()(ast::variable const& x) const;
70 bool operator()(ast::operation const& x) const;
71 bool operator()(ast::signed_ const& x) const;
72 bool operator()(ast::expression const& x) const;
73 bool operator()(ast::assignment const& x) const;
74 bool operator()(ast::variable_declaration const& x) const;
75 bool operator()(ast::statement_list const& x) const;
77 client::code_gen::program& program;
80 void(int tag, std::string const& what)>