]>
Commit | Line | Data |
---|---|---|
1 | /*============================================================================= | |
2 | Copyright (c) 2001-2011 Joel de Guzman | |
3 | Copyright (c) 2001-2011 Hartmut Kaiser | |
4 | ||
5 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
7 | =============================================================================*/ | |
8 | #if !defined(BOOST_SPIRIT_CONJURE_AST_HPP) | |
9 | #define BOOST_SPIRIT_CONJURE_AST_HPP | |
10 | ||
11 | #include <boost/variant/recursive_variant.hpp> | |
12 | #include <boost/fusion/include/adapt_struct.hpp> | |
13 | #include <boost/fusion/include/io.hpp> | |
14 | #include <boost/spirit/include/support_extended_variant.hpp> | |
15 | #include <boost/spirit/include/support_attributes.hpp> | |
16 | #include <boost/optional.hpp> | |
17 | #include <list> | |
18 | ||
19 | #include "ids.hpp" | |
20 | ||
21 | namespace client { namespace ast | |
22 | { | |
23 | /////////////////////////////////////////////////////////////////////////// | |
24 | // The AST | |
25 | /////////////////////////////////////////////////////////////////////////// | |
26 | struct tagged | |
27 | { | |
28 | int id; // Used to annotate the AST with the iterator position. | |
29 | // This id is used as a key to a map<int, Iterator> | |
30 | // (not really part of the AST.) | |
31 | }; | |
32 | ||
33 | struct nil {}; | |
34 | struct unary_expr; | |
35 | struct function_call; | |
36 | struct expression; | |
37 | ||
38 | struct identifier : tagged | |
39 | { | |
40 | identifier(std::string const& name = "") : name(name) {} | |
41 | std::string name; | |
42 | }; | |
43 | ||
44 | struct primary_expr : | |
45 | tagged, | |
46 | boost::spirit::extended_variant< | |
47 | nil | |
48 | , bool | |
49 | , unsigned int | |
50 | , identifier | |
51 | , boost::recursive_wrapper<expression> | |
52 | > | |
53 | { | |
54 | primary_expr() : base_type() {} | |
55 | primary_expr(bool val) : base_type(val) {} | |
56 | primary_expr(unsigned int val) : base_type(val) {} | |
57 | primary_expr(identifier const& val) : base_type(val) {} | |
58 | primary_expr(expression const& val) : base_type(val) {} | |
59 | primary_expr(primary_expr const& rhs) | |
60 | : base_type(rhs.get()) {} | |
61 | }; | |
62 | ||
63 | struct operand : | |
64 | tagged, | |
65 | boost::spirit::extended_variant< | |
66 | nil | |
67 | , primary_expr | |
68 | , boost::recursive_wrapper<unary_expr> | |
69 | , boost::recursive_wrapper<function_call> | |
70 | > | |
71 | { | |
72 | operand() : base_type() {} | |
73 | operand(primary_expr const& val) : base_type(val) {} | |
74 | operand(unary_expr const& val) : base_type(val) {} | |
75 | operand(function_call const& val) : base_type(val) {} | |
76 | operand(operand const& rhs) | |
77 | : base_type(rhs.get()) {} | |
78 | }; | |
79 | ||
80 | struct unary_expr : tagged | |
81 | { | |
82 | token_ids::type operator_; | |
83 | operand operand_; | |
84 | }; | |
85 | ||
86 | struct operation | |
87 | { | |
88 | token_ids::type operator_; | |
89 | operand operand_; | |
90 | }; | |
91 | ||
92 | struct function_call | |
93 | { | |
94 | identifier function_name; | |
95 | std::list<expression> args; | |
96 | }; | |
97 | ||
98 | struct expression | |
99 | { | |
100 | operand first; | |
101 | std::list<operation> rest; | |
102 | }; | |
103 | ||
104 | struct assignment | |
105 | { | |
106 | identifier lhs; | |
107 | token_ids::type operator_; | |
108 | expression rhs; | |
109 | }; | |
110 | ||
111 | struct variable_declaration | |
112 | { | |
113 | identifier lhs; | |
114 | boost::optional<expression> rhs; | |
115 | }; | |
116 | ||
117 | struct if_statement; | |
118 | struct while_statement; | |
119 | struct statement_list; | |
120 | struct return_statement; | |
121 | ||
122 | typedef boost::variant< | |
123 | nil | |
124 | , variable_declaration | |
125 | , assignment | |
126 | , boost::recursive_wrapper<if_statement> | |
127 | , boost::recursive_wrapper<while_statement> | |
128 | , boost::recursive_wrapper<return_statement> | |
129 | , boost::recursive_wrapper<statement_list> | |
130 | , boost::recursive_wrapper<expression> | |
131 | > | |
132 | statement; | |
133 | ||
134 | struct statement_list : std::list<statement> {}; | |
135 | ||
136 | struct if_statement | |
137 | { | |
138 | expression condition; | |
139 | statement then; | |
140 | boost::optional<statement> else_; | |
141 | }; | |
142 | ||
143 | struct while_statement | |
144 | { | |
145 | expression condition; | |
146 | statement body; | |
147 | }; | |
148 | ||
149 | struct return_statement : tagged | |
150 | { | |
151 | boost::optional<expression> expr; | |
152 | }; | |
153 | ||
154 | struct function | |
155 | { | |
156 | std::string return_type; | |
157 | identifier function_name; | |
158 | std::list<identifier> args; | |
159 | boost::optional<statement_list> body; | |
160 | }; | |
161 | ||
162 | typedef std::list<function> function_list; | |
163 | ||
164 | // print functions for debugging | |
165 | inline std::ostream& operator<<(std::ostream& out, nil) | |
166 | { | |
167 | out << "nil"; return out; | |
168 | } | |
169 | ||
170 | inline std::ostream& operator<<(std::ostream& out, identifier const& id) | |
171 | { | |
172 | out << id.name; return out; | |
173 | } | |
174 | }} | |
175 | ||
176 | BOOST_FUSION_ADAPT_STRUCT( | |
177 | client::ast::unary_expr, | |
178 | (client::token_ids::type, operator_) | |
179 | (client::ast::operand, operand_) | |
180 | ) | |
181 | ||
182 | BOOST_FUSION_ADAPT_STRUCT( | |
183 | client::ast::operation, | |
184 | (client::token_ids::type, operator_) | |
185 | (client::ast::operand, operand_) | |
186 | ) | |
187 | ||
188 | BOOST_FUSION_ADAPT_STRUCT( | |
189 | client::ast::function_call, | |
190 | (client::ast::identifier, function_name) | |
191 | (std::list<client::ast::expression>, args) | |
192 | ) | |
193 | ||
194 | BOOST_FUSION_ADAPT_STRUCT( | |
195 | client::ast::expression, | |
196 | (client::ast::operand, first) | |
197 | (std::list<client::ast::operation>, rest) | |
198 | ) | |
199 | ||
200 | BOOST_FUSION_ADAPT_STRUCT( | |
201 | client::ast::variable_declaration, | |
202 | (client::ast::identifier, lhs) | |
203 | (boost::optional<client::ast::expression>, rhs) | |
204 | ) | |
205 | ||
206 | BOOST_FUSION_ADAPT_STRUCT( | |
207 | client::ast::assignment, | |
208 | (client::ast::identifier, lhs) | |
209 | (client::token_ids::type, operator_) | |
210 | (client::ast::expression, rhs) | |
211 | ) | |
212 | ||
213 | BOOST_FUSION_ADAPT_STRUCT( | |
214 | client::ast::if_statement, | |
215 | (client::ast::expression, condition) | |
216 | (client::ast::statement, then) | |
217 | (boost::optional<client::ast::statement>, else_) | |
218 | ) | |
219 | ||
220 | BOOST_FUSION_ADAPT_STRUCT( | |
221 | client::ast::while_statement, | |
222 | (client::ast::expression, condition) | |
223 | (client::ast::statement, body) | |
224 | ) | |
225 | ||
226 | BOOST_FUSION_ADAPT_STRUCT( | |
227 | client::ast::return_statement, | |
228 | (boost::optional<client::ast::expression>, expr) | |
229 | ) | |
230 | ||
231 | BOOST_FUSION_ADAPT_STRUCT( | |
232 | client::ast::function, | |
233 | (std::string, return_type) | |
234 | (client::ast::identifier, function_name) | |
235 | (std::list<client::ast::identifier>, args) | |
236 | (boost::optional<client::ast::statement_list>, body) | |
237 | ) | |
238 | ||
239 | #endif |