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_AST_HPP) 8 #define BOOST_SPIRIT_CALC7_AST_HPP 9 10 #include <boost/config/warning_disable.hpp> 11 #include <boost/variant/recursive_variant.hpp> 12 #include <boost/fusion/include/adapt_struct.hpp> 13 #include <boost/fusion/include/io.hpp> 14 #include <list> 15 16 namespace client { namespace ast 17 { 18 /////////////////////////////////////////////////////////////////////////// 19 // The AST 20 /////////////////////////////////////////////////////////////////////////// 21 struct tagged 22 { 23 int id; // Used to annotate the AST with the iterator position. 24 // This id is used as a key to a map<int, Iterator> 25 // (not really part of the AST.) 26 }; 27 28 struct nil {}; 29 struct signed_; 30 struct expression; 31 32 struct variable : tagged 33 { variableclient::ast::variable34 variable(std::string const& name = "") : name(name) {} 35 std::string name; 36 }; 37 38 typedef boost::variant< 39 nil 40 , unsigned int 41 , variable 42 , boost::recursive_wrapper<signed_> 43 , boost::recursive_wrapper<expression> 44 > 45 operand; 46 47 struct signed_ 48 { 49 char sign; 50 operand operand_; 51 }; 52 53 struct operation 54 { 55 char operator_; 56 operand operand_; 57 }; 58 59 struct expression 60 { 61 operand first; 62 std::list<operation> rest; 63 }; 64 65 struct assignment 66 { 67 variable lhs; 68 expression rhs; 69 }; 70 71 struct variable_declaration 72 { 73 assignment assign; 74 }; 75 76 typedef boost::variant< 77 variable_declaration 78 , assignment> 79 statement; 80 81 typedef std::list<statement> statement_list; 82 83 // print functions for debugging operator <<(std::ostream & out,nil)84 inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; } operator <<(std::ostream & out,variable const & var)85 inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; } 86 }} 87 88 BOOST_FUSION_ADAPT_STRUCT( 89 client::ast::signed_, 90 (char, sign) 91 (client::ast::operand, operand_) 92 ) 93 94 BOOST_FUSION_ADAPT_STRUCT( 95 client::ast::operation, 96 (char, operator_) 97 (client::ast::operand, operand_) 98 ) 99 100 BOOST_FUSION_ADAPT_STRUCT( 101 client::ast::expression, 102 (client::ast::operand, first) 103 (std::list<client::ast::operation>, rest) 104 ) 105 106 BOOST_FUSION_ADAPT_STRUCT( 107 client::ast::variable_declaration, 108 (client::ast::assignment, assign) 109 ) 110 111 BOOST_FUSION_ADAPT_STRUCT( 112 client::ast::assignment, 113 (client::ast::variable, lhs) 114 (client::ast::expression, rhs) 115 ) 116 117 #endif 118