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_CALC8_AST_HPP) 8 #define BOOST_SPIRIT_X3_CALC8_AST_HPP 9 10 #include <boost/spirit/home/x3/support/ast/variant.hpp> 11 #include <boost/spirit/home/x3/support/ast/position_tagged.hpp> 12 #include <boost/fusion/include/io.hpp> 13 #include <list> 14 15 namespace client { namespace ast 16 { 17 /////////////////////////////////////////////////////////////////////////// 18 // The AST 19 /////////////////////////////////////////////////////////////////////////// 20 namespace x3 = boost::spirit::x3; 21 22 struct nil {}; 23 struct signed_; 24 struct expression; 25 26 struct variable : x3::position_tagged 27 { variableclient::ast::variable28 variable(std::string const& name = "") : name(name) {} 29 std::string name; 30 }; 31 32 struct operand : 33 x3::variant< 34 nil 35 , unsigned int 36 , variable 37 , x3::forward_ast<signed_> 38 , x3::forward_ast<expression> 39 > 40 { 41 using base_type::base_type; 42 using base_type::operator=; 43 }; 44 45 struct signed_ 46 { 47 char sign; 48 operand operand_; 49 }; 50 51 struct operation : x3::position_tagged 52 { 53 char operator_; 54 operand operand_; 55 }; 56 57 struct expression : x3::position_tagged 58 { 59 operand first; 60 std::list<operation> rest; 61 }; 62 63 struct assignment : x3::position_tagged 64 { 65 variable lhs; 66 expression rhs; 67 }; 68 69 struct variable_declaration 70 { 71 assignment assign; 72 }; 73 74 struct statement : 75 x3::variant< 76 variable_declaration 77 , assignment> 78 { 79 using base_type::base_type; 80 using base_type::operator=; 81 }; 82 83 typedef std::list<statement> statement_list; 84 85 // print functions for debugging operator <<(std::ostream & out,nil)86 inline std::ostream& operator<<(std::ostream& out, nil) 87 { 88 out << "nil"; 89 return out; 90 } 91 operator <<(std::ostream & out,variable const & var)92 inline std::ostream& operator<<(std::ostream& out, variable const& var) 93 { 94 out << var.name; return out; 95 } 96 }} 97 98 #endif 99