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

source code of boost/libs/spirit/example/qi/compiler_tutorial/calc7/ast.hpp