-
Notifications
You must be signed in to change notification settings - Fork 3
/
var_adder.hpp
75 lines (59 loc) · 2.04 KB
/
var_adder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*=============================================================================
Copyright (c) 2001-2009 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef VAR_ADDER_HPP_
#define VAR_ADDER_HPP_
#include <boost/spirit/include/qi.hpp>
struct symbol_type
{
bool sign;
int size;
};
struct symbol_info
{
int id;
symbol_type type;
};
///////////////////////////////////////////////////////////////////////////////
// A functor that adds variables to our (variables) symbol-table
///////////////////////////////////////////////////////////////////////////////
struct var_adder
{
template <typename>
struct result { typedef void type; };
var_adder(boost::spirit::qi::symbols<char, int>& vars, int& nvars)
: vars(vars), nvars(nvars)
{
}
void operator()(std::string const& var) const
{
vars.add(var.begin(), var.end(), nvars++);
};
boost::spirit::qi::symbols<char, int>& vars;
int& nvars;
};
///////////////////////////////////////////////////////////////////////////////
// A functor that adds variables to our (variables) symbol-table including a data type!
///////////////////////////////////////////////////////////////////////////////
struct var_adder2
{
template <typename, typename>
struct result { typedef void type; };
var_adder2(boost::spirit::qi::symbols<char, int>& oldVars,
boost::spirit::qi::symbols<char, symbol_info>& vars, int& nvars)
: oldVars(oldVars), vars(vars), nvars(nvars)
{
}
void operator()(std::string const& var, const symbol_type type) const
{
oldVars.add(var.begin(), var.end(), nvars);
symbol_info info = {nvars++, type};
vars.add(var.begin(), var.end(), info);
};
boost::spirit::qi::symbols<char, int>& oldVars;
boost::spirit::qi::symbols<char, symbol_info>& vars;
int& nvars;
};
#endif /* VAR_ADDER_HPP_ */