mupp 1.1.0
Loading...
Searching...
No Matches
PAst.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PAst.hpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8 Based on Joel de Guzman example on calc7,
9 see https://github.com/boostorg/spirit
10
11***************************************************************************/
12
13/***************************************************************************
14 * Copyright (C) 2023 by Andreas Suter *
15 * andreas.suter@psi.ch *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
32
33#ifndef _PAST_HPP_
34#define _PAST_HPP_
35
36#include <boost/config/warning_disable.hpp>
37#include <boost/variant/recursive_variant.hpp>
38#include <boost/fusion/include/adapt_struct.hpp>
39#include <boost/fusion/include/io.hpp>
40#include <boost/optional.hpp>
41#include <list>
42
43namespace mupp { namespace ast
44{
46
63
71 struct tagged
72 {
73 int id;
74 };
75
91
117
123 struct nil {};
124
125 // Forward declarations for recursive AST structures
126 struct unary;
127 struct expression;
128 struct function;
129 struct power;
130
139 {
144 variable(std::string const& name = "") : name(name) {}
145 std::string name;
146 };
147
156 typedef boost::variant<
157 nil
158 , double
159 , variable
160 , boost::recursive_wrapper<function>
161 , boost::recursive_wrapper<power>
162 , boost::recursive_wrapper<unary>
163 , boost::recursive_wrapper<expression>
164 >
166
177
189
198 {
200 std::list<operation> rest;
201 };
202
213
225
238
247 {
249 boost::optional<expression> rhs;
250 };
251
257 typedef boost::variant<
259 , assignment>
261
268 typedef std::list<statement> statement_list;
269
275 inline std::ostream& operator<<(std::ostream& out, nil) { out << std::string("nil"); return out; }
276
283 inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
284}}
285
286BOOST_FUSION_ADAPT_STRUCT(
288 (mupp::ast::optoken, operator_)
289 (mupp::ast::operand, operand_)
290)
291
292BOOST_FUSION_ADAPT_STRUCT(
294 (mupp::ast::optoken, operator_)
295 (mupp::ast::operand, operand_)
296)
297
298BOOST_FUSION_ADAPT_STRUCT(
300 (mupp::ast::operand, first)
301 (std::list<mupp::ast::operation>, rest)
302)
303
304BOOST_FUSION_ADAPT_STRUCT(
306 (mupp::ast::funid, func_id)
308)
309
310BOOST_FUSION_ADAPT_STRUCT(
314)
315
316BOOST_FUSION_ADAPT_STRUCT(
319 (boost::optional<mupp::ast::expression>, rhs)
320)
321
322BOOST_FUSION_ADAPT_STRUCT(
326)
327
328#endif // _PAST_HPP_
std::list< statement > statement_list
Type alias for a list of statements forming a program.
Definition PAst.hpp:268
optoken
Enumeration of arithmetic and unary operators.
Definition PAst.hpp:83
@ op_minus
Subtraction operator (-)
Definition PAst.hpp:85
@ op_positive
Unary plus operator (+x)
Definition PAst.hpp:88
@ op_divide
Division operator (/)
Definition PAst.hpp:87
@ op_plus
Addition operator (+)
Definition PAst.hpp:84
@ op_times
Multiplication operator (*)
Definition PAst.hpp:86
@ op_negative
Unary minus operator (-x)
Definition PAst.hpp:89
boost::variant< variable_declaration, assignment > statement
Variant type representing a single statement.
Definition PAst.hpp:260
boost::variant< nil, double, variable, boost::recursive_wrapper< function >, boost::recursive_wrapper< power >, boost::recursive_wrapper< unary >, boost::recursive_wrapper< expression > > operand
Variant type representing any operand in an expression.
Definition PAst.hpp:165
funid
Enumeration of supported mathematical functions.
Definition PAst.hpp:99
@ fun_sqrt
Square root function.
Definition PAst.hpp:115
@ fun_atan
Arctangent function.
Definition PAst.hpp:111
@ fun_acos
Arccosine function.
Definition PAst.hpp:110
@ fun_exp
Exponential function.
Definition PAst.hpp:112
@ fun_min
Minimum value function.
Definition PAst.hpp:101
@ fun_sin
Sine function.
Definition PAst.hpp:103
@ fun_cos
Cosine function.
Definition PAst.hpp:104
@ fun_cosh
Hyperbolic cosine function.
Definition PAst.hpp:107
@ fun_max
Maximum value function.
Definition PAst.hpp:100
@ fun_ln
Natural logarithm function.
Definition PAst.hpp:114
@ fun_log
Base-10 logarithm function.
Definition PAst.hpp:113
@ fun_tanh
Hyperbolic tangent function.
Definition PAst.hpp:108
@ fun_asin
Arcsine function.
Definition PAst.hpp:109
@ fun_sinh
Hyperbolic sine function.
Definition PAst.hpp:106
@ fun_tan
Tangent function.
Definition PAst.hpp:105
@ fun_abs
Absolute value function.
Definition PAst.hpp:102
std::ostream & operator<<(std::ostream &out, nil)
Stream output operator for nil nodes (debugging support).
Definition PAst.hpp:275
Represents an assignment statement.
Definition PAst.hpp:234
variable lhs
The left-hand side variable being assigned to.
Definition PAst.hpp:235
expression rhs
The right-hand side expression to evaluate.
Definition PAst.hpp:236
Represents a complete expression with operator precedence.
Definition PAst.hpp:198
operand first
The first operand in the expression.
Definition PAst.hpp:199
std::list< operation > rest
Sequence of operations applied left-to-right.
Definition PAst.hpp:200
Represents a function call with a single argument.
Definition PAst.hpp:209
expression arg
The argument expression.
Definition PAst.hpp:211
funid func_id
The function identifier.
Definition PAst.hpp:210
Represents an empty/null AST node.
Definition PAst.hpp:123
Represents a binary operation with an operator and right operand.
Definition PAst.hpp:185
optoken operator_
The binary operator (+, -, *, /)
Definition PAst.hpp:186
operand operand_
The right-hand operand.
Definition PAst.hpp:187
Represents a power operation.
Definition PAst.hpp:221
expression base
The base expression.
Definition PAst.hpp:222
expression pow
The exponent expression.
Definition PAst.hpp:223
Abstract Syntax Tree (AST) definitions for the variable parser.
Definition PAst.hpp:72
int id
ID used as key to map to iterator position in source code (not part of the logical AST structure)
Definition PAst.hpp:73
Represents a unary operation applied to an operand.
Definition PAst.hpp:173
operand operand_
The operand the operator is applied to.
Definition PAst.hpp:175
optoken operator_
The unary operator (positive or negative)
Definition PAst.hpp:174
Represents a variable declaration with optional initialization.
Definition PAst.hpp:247
variable lhs
The variable being declared.
Definition PAst.hpp:248
boost::optional< expression > rhs
Optional initialization expression.
Definition PAst.hpp:249
Represents a variable in an expression.
Definition PAst.hpp:139
std::string name
Variable name without the '$' prefix.
Definition PAst.hpp:145
variable(std::string const &name="")
Constructor for variable node.
Definition PAst.hpp:144