1 /*============================================================================= 2 Copyright (c) 2001-2014 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_CALC9_COMPILER_HPP) 8 #define BOOST_SPIRIT_X3_CALC9_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(); } sizeclient::code_gen::program29 std::size_t size() const { return code.size(); } operator ()client::code_gen::program30 std::vector<int> const& operator()() const { return code; } 31 nvarsclient::code_gen::program32 std::size_t nvars() const { return variables.size(); } 33 int const* find_var(std::string const& name) const; 34 void add_var(std::string const& name); 35 36 void print_variables(std::vector<int> const& stack) const; 37 void print_assembler() const; 38 39 private: 40 41 std::map<std::string, int> variables; 42 std::vector<int> code; 43 }; 44 45 //////////////////////////////////////////////////////////////////////////// 46 // The Compiler 47 //////////////////////////////////////////////////////////////////////////// 48 struct compiler 49 { 50 typedef bool result_type; 51 typedef std::function< 52 void(x3::position_tagged, std::string const&)> 53 error_handler_type; 54 55 template <typename ErrorHandler> compilerclient::code_gen::compiler56 compiler( 57 client::code_gen::program& program 58 , ErrorHandler const& error_handler) 59 : program(program) 60 , error_handler( 61 [&](x3::position_tagged pos, std::string const& msg) 62 { error_handler(pos, msg); } 63 ) 64 {} 65 operator ()client::code_gen::compiler66 bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; } 67 bool operator()(unsigned int x) const; 68 bool operator()(bool x) const; 69 bool operator()(ast::variable const& x) const; 70 bool operator()(ast::operation const& x) const; 71 bool operator()(ast::unary 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 bool operator()(ast::statement const& x) const; 77 bool operator()(ast::if_statement const& x) const; 78 bool operator()(ast::while_statement const& x) const; 79 80 bool start(ast::statement_list const& x) const; 81 82 client::code_gen::program& program; 83 error_handler_type error_handler; 84 }; 85 }} 86 87 #endif 88