]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/compiler_tutorial/calc7/compiler.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / compiler_tutorial / calc7 / compiler.hpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3
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
9
10 #include "ast.hpp"
11 #include "error_handler.hpp"
12 #include <vector>
13 #include <map>
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>
18
19 namespace client { namespace code_gen
20 {
21 ///////////////////////////////////////////////////////////////////////////
22 // The Program
23 ///////////////////////////////////////////////////////////////////////////
24 struct program
25 {
26 void op(int a);
27 void op(int a, int b);
28 void op(int a, int b, int c);
29
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; }
34
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);
38
39 void print_variables(std::vector<int> const& stack) const;
40 void print_assembler() const;
41
42 private:
43
44 std::map<std::string, int> variables;
45 std::vector<int> code;
46 };
47
48 ///////////////////////////////////////////////////////////////////////////
49 // The Compiler
50 ///////////////////////////////////////////////////////////////////////////
51 struct compiler
52 {
53 typedef bool result_type;
54
55 template <typename ErrorHandler>
56 compiler(client::code_gen::program& program, ErrorHandler& error_handler_)
57 : program(program)
58 {
59 using namespace boost::phoenix::arg_names;
60 namespace phx = boost::phoenix;
61 using boost::phoenix::function;
62
63 error_handler = function<ErrorHandler>(error_handler_)(
64 "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
65 }
66
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;
76
77 client::code_gen::program& program;
78
79 boost::function<
80 void(int tag, std::string const& what)>
81 error_handler;
82 };
83 }}
84
85 #endif