mupp 1.1.0
Loading...
Searching...
No Matches
PAnnotation.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PAnnotation.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 _PANNOTATION_HPP_
34#define _PANNOTATION_HPP_
35
36#include <map>
37#include <boost/variant/apply_visitor.hpp>
38#include <boost/type_traits/is_base_of.hpp>
39#include <boost/mpl/bool.hpp>
40#include "PAst.hpp"
41
42namespace mupp
43{
45
59 template <typename Iterator>
61 {
62
63 std::vector<Iterator>& iters;
64
69 PAnnotation(std::vector<Iterator>& iters) : iters(iters) {}
70
77 struct set_id
78 {
79 int id;
80
85 set_id(int id) : id(id) {}
86
95 template <typename T>
96 void operator()(T& x) const
97 {
98 this->dispatch(x, boost::is_base_of<ast::tagged, T>());
99 }
100
109 template <typename T>
110 void dispatch(T& x, boost::mpl::false_) const
111 {
112 // (no-op) no need for tags
113 }
114
123 template <typename T>
124 void dispatch(T& x, boost::mpl::true_) const
125 {
126 x.id = id;
127 }
128 };
129
138 void operator()(ast::operand& ast, Iterator pos) const
139 {
140 int id = iters.size();
141 iters.push_back(pos);
142 boost::apply_visitor(set_id(id), ast);
143 }
144
153 void operator()(ast::assignment& ast, Iterator pos) const
154 {
155 int id = iters.size();
156 iters.push_back(pos);
157 ast.lhs.id = id;
158 }
159 };
160}
161
162#endif // _PANNOTATION_HPP_
163
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
Helper struct to set ID tags on AST nodes.
int id
The ID to assign to tagged AST nodes.
void dispatch(T &x, boost::mpl::false_) const
No-op handler for non-tagged AST nodes.
set_id(int id)
Constructor that stores the ID to be assigned.
void dispatch(T &x, boost::mpl::true_) const
Assigns ID to tagged AST nodes.
void operator()(T &x) const
Dispatches to the appropriate handler based on node type.
void operator()(ast::assignment &ast, Iterator pos) const
Annotates an assignment AST node with its source position.
std::vector< Iterator > & iters
Reference to vector storing iterator positions indexed by AST node IDs.
void operator()(ast::operand &ast, Iterator pos) const
Annotates an operand AST node with its source position.
PAnnotation(std::vector< Iterator > &iters)
Constructor that initializes the annotation handler.
Represents an assignment statement.
Definition PAst.hpp:234