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_CALC8_COMPILER_HPP)
8 #define BOOST_SPIRIT_CALC8_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 
operator []client::code_gen::program30         int& operator[](std::size_t i) { return code[i]; }
operator []client::code_gen::program31         int const& operator[](std::size_t i) const { return code[i]; }
clearclient::code_gen::program32         void clear() { code.clear(); variables.clear(); }
sizeclient::code_gen::program33         std::size_t size() const { return code.size(); }
operator ()client::code_gen::program34         std::vector<int> const& operator()() const { return code; }
35 
nvarsclient::code_gen::program36         int nvars() const { return variables.size(); }
37         int const* find_var(std::string const& name) const;
38         void add_var(std::string const& name);
39 
40         void print_variables(std::vector<int> const& stack) const;
41         void print_assembler() const;
42 
43     private:
44 
45         std::map<std::string, int> variables;
46         std::vector<int> code;
47     };
48 
49     ///////////////////////////////////////////////////////////////////////////
50     //  The Compiler
51     ///////////////////////////////////////////////////////////////////////////
52     struct compiler
53     {
54         typedef bool result_type;
55 
56         template <typename ErrorHandler>
compilerclient::code_gen::compiler57         compiler(client::code_gen::program& program, ErrorHandler& error_handler_)
58           : program(program)
59         {
60             using namespace boost::phoenix::arg_names;
61             namespace phx = boost::phoenix;
62             using boost::phoenix::function;
63 
64             error_handler = function<ErrorHandler>(error_handler_)(
65                 "Error! ", _2, phx::cref(error_handler_.iters)[_1]);
66         }
67 
operator ()client::code_gen::compiler68         bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
69         bool operator()(unsigned int x) const;
70         bool operator()(bool x) const;
71         bool operator()(ast::variable const& x) const;
72         bool operator()(ast::operation const& x) const;
73         bool operator()(ast::unary const& x) const;
74         bool operator()(ast::expression const& x) const;
75         bool operator()(ast::assignment const& x) const;
76         bool operator()(ast::variable_declaration const& x) const;
77         bool operator()(ast::statement_list const& x) const;
78         bool operator()(ast::statement const& x) const;
79         bool operator()(ast::if_statement const& x) const;
80         bool operator()(ast::while_statement const& x) const;
81 
82         bool start(ast::statement_list const& x) const;
83 
84         client::code_gen::program& program;
85 
86         boost::function<
87             void(int tag, std::string const& what)>
88         error_handler;
89     };
90 }}
91 
92 #endif
93