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_X3_CALC8_COMPILER_HPP)
8 #define BOOST_SPIRIT_X3_CALC8_COMPILER_HPP
9 
10 #include "ast.hpp"
11 #include "error_handler.hpp"
12 #include <vector>
13 #include <map>
14 
15 namespace client { namespace code_gen
16 {
17     ///////////////////////////////////////////////////////////////////////////
18     //  The Program
19     ///////////////////////////////////////////////////////////////////////////
20     struct program
21     {
22         void op(int a);
23         void op(int a, int b);
24         void op(int a, int b, int c);
25 
operator []client::code_gen::program26         int& operator[](std::size_t i) { return code[i]; }
operator []client::code_gen::program27         int operator[](std::size_t i) const { return code[i]; }
clearclient::code_gen::program28         void clear() { code.clear(); variables.clear(); }
operator ()client::code_gen::program29         std::vector<int> const& operator()() const { return code; }
30 
nvarsclient::code_gen::program31         std::size_t nvars() const { return variables.size(); }
32         int const* find_var(std::string const& name) const;
33         void add_var(std::string const& name);
34 
35         void print_variables(std::vector<int> const& stack) const;
36         void print_assembler() const;
37 
38     private:
39 
40         std::map<std::string, int> variables;
41         std::vector<int> code;
42     };
43 
44     ////////////////////////////////////////////////////////////////////////////
45     //  The Compiler
46     ////////////////////////////////////////////////////////////////////////////
47     struct compiler
48     {
49         typedef bool result_type;
50         typedef std::function<
51             void(x3::position_tagged, std::string const&)>
52         error_handler_type;
53 
54         template <typename ErrorHandler>
compilerclient::code_gen::compiler55         compiler(
56             client::code_gen::program& program
57           , ErrorHandler const& error_handler)
58           : program(program)
59           , error_handler(
60                 [&](x3::position_tagged pos, std::string const& msg)
61                 { error_handler(pos, msg); }
62             )
63         {}
64 
operator ()client::code_gen::compiler65         bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
66         bool operator()(unsigned int x) const;
67         bool operator()(ast::variable const& x) const;
68         bool operator()(ast::operation const& x) const;
69         bool operator()(ast::signed_ const& x) const;
70         bool operator()(ast::expression const& x) const;
71         bool operator()(ast::assignment const& x) const;
72         bool operator()(ast::variable_declaration const& x) const;
73         bool operator()(ast::statement_list const& x) const;
74 
75         client::code_gen::program& program;
76         error_handler_type error_handler;
77     };
78 }}
79 
80 #endif
81