-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompile_op.hpp
67 lines (53 loc) · 1.57 KB
/
compile_op.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
/*=============================================================================
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 COMPILE_OP_HPP_
#define COMPILE_OP_HPP_
#include "byte_code.hpp"
#include <boost/spirit/include/qi.hpp>
#include <vector>
struct compile_op
{
template <typename A,
typename B = boost::spirit::qi::unused_type,
typename C = boost::spirit::qi::unused_type>
struct result { typedef void type; };
compile_op(std::vector<int>& code)
: code(code)
{
}
void operator()(int a) const
{
code.push_back(a);
}
void operator()(int a, int b) const
{
code.push_back(a);
code.push_back(b);
}
void operator()(int a, int b, int c) const
{
code.push_back(a);
code.push_back(b);
code.push_back(c);
}
// special overload for function calls
void operator()(function_info const& info, int got_nargs, bool& parse_result) const
{
if (got_nargs == info.arity)
{
code.push_back(op_call);
code.push_back(info.arity);
code.push_back(info.address);
}
else
{
parse_result = false; // fail the parse
std::cerr << "wrong number of arguments" << std::endl;
}
}
std::vector<int>& code;
};
#endif /* COMPILE_OP_HPP_ */