1
/*=============================================================================
2
Copyright (c) 2001-2010 Joel de Guzman
3
Copyright (c) 2001-2010 Hartmut Kaiser
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
///////////////////////////////////////////////////////////////////////////////
10
// A Calculator example demonstrating generation of AST from which we generate
11
// a simple byte code representation being interpreted by a similar virtual
14
// [ JDG April 28, 2008 ]
15
// [ HK May 05, 2008 ]
17
///////////////////////////////////////////////////////////////////////////////
18
#include <boost/config/warning_disable.hpp>
24
#include "calc2_ast_vm.hpp"
26
#include <boost/spirit/include/qi.hpp>
27
#include <boost/spirit/include/karma.hpp>
28
#include <boost/fusion/include/adapt_struct.hpp>
30
using namespace boost::spirit;
31
using namespace boost::spirit::ascii;
33
///////////////////////////////////////////////////////////////////////////////
34
// Our calculator parser grammar
35
///////////////////////////////////////////////////////////////////////////////
36
template <typename Iterator>
38
: qi::grammar<Iterator, expression_ast(), space_type>
40
calculator() : calculator::base_type(expression)
44
>> *( ('+' >> term [_val += _1])
45
| ('-' >> term [_val -= _1])
51
>> *( ('*' >> factor [_val *= _1])
52
| ('/' >> factor [_val /= _1])
58
| '(' >> expression [_val = _1] >> ')'
59
| ('-' >> factor [_val = neg(_1)])
60
| ('+' >> factor [_val = pos(_1)])
64
qi::rule<Iterator, expression_ast(), space_type> expression, term, factor;
67
///////////////////////////////////////////////////////////////////////////////
68
// The Virtual Machine
69
///////////////////////////////////////////////////////////////////////////////
75
char bytes[sizeof(int)];
78
vmachine(unsigned stackSize = 4096)
80
, stack_ptr(stack.begin())
84
int top() const { return stack_ptr[-1]; };
85
void execute(std::vector<element> const& code);
88
std::vector<int> stack;
89
std::vector<int>::iterator stack_ptr;
92
void vmachine::execute(std::vector<element> const& code)
94
std::vector<element>::const_iterator pc = code.begin();
95
stack_ptr = stack.begin();
97
while ((*pc).code && pc != code.end())
102
stack_ptr[-1] = -stack_ptr[-1];
107
stack_ptr[-1] += stack_ptr[0];
112
stack_ptr[-1] -= stack_ptr[0];
117
stack_ptr[-1] *= stack_ptr[0];
122
stack_ptr[-1] /= stack_ptr[0];
126
*stack_ptr++ = (*pc++).code;
132
// We need to tell fusion about our binary_op and unary_op structs
133
// to make them a first-class fusion citizen
135
// Note: we register the members exactly in the same sequence as we need them
137
BOOST_FUSION_ADAPT_STRUCT(
139
(expression_ast, left)
140
(expression_ast, right)
144
BOOST_FUSION_ADAPT_STRUCT(
146
(expression_ast, right)
150
///////////////////////////////////////////////////////////////////////////////
151
// Our AST grammar for the generator, this just dumps the AST as a expression
152
///////////////////////////////////////////////////////////////////////////////
153
template <typename OuputIterator, typename Delimiter>
154
struct generate_byte_code
155
: karma::grammar<OuputIterator, expression_ast(), Delimiter>
157
generate_byte_code() : generate_byte_code::base_type(ast_node)
159
ast_node %= int_node | binary_node | unary_node;
160
int_node %= dword(op_int) << dword;
161
binary_node %= ast_node << ast_node << byte_;
162
unary_node %= ast_node << byte_;
165
karma::rule<OuputIterator, expression_ast(), Delimiter> ast_node;
166
karma::rule<OuputIterator, int(), Delimiter> int_node;
167
karma::rule<OuputIterator, binary_op(), Delimiter> binary_node;
168
karma::rule<OuputIterator, unary_op(), Delimiter> unary_node;
171
///////////////////////////////////////////////////////////////////////////////
172
// helper function helping to deduce the delimiter type
173
template <typename Delimiter>
174
bool generate_vm_code(expression_ast const& ast,
175
std::vector<vmachine::element>& code, Delimiter const& d)
177
// Our generator grammar definitions
178
typedef char* output_iterator_type;
179
typedef generate_byte_code<output_iterator_type, Delimiter> generate_byte_code;
181
char* outbuffer = (*code.begin()).bytes;
182
generate_byte_code gen_vm;
183
return karma::generate_delimited(outbuffer, gen_vm, d, ast);
186
///////////////////////////////////////////////////////////////////////////////
188
///////////////////////////////////////////////////////////////////////////////
192
std::cout << "/////////////////////////////////////////////////////////\n\n";
193
std::cout << "Compile simple expressions to bytecode...\n\n";
194
std::cout << "/////////////////////////////////////////////////////////\n\n";
195
std::cout << "Type an expression...or [q or Q] to quit\n\n";
197
// Our parser grammar definitions
198
typedef std::string::const_iterator iterator_type;
199
typedef calculator<iterator_type> calculator;
204
while (std::getline(std::cin, str))
206
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
210
std::string::const_iterator iter = str.begin();
211
std::string::const_iterator end = str.end();
212
bool r = qi::phrase_parse(iter, end, calc, space, ast);
214
if (r && iter == end)
216
// we assume a vm code size of 4096 is sufficient
217
std::vector<vmachine::element> code (4096);
218
r = generate_vm_code(ast, code, pad(4));
224
std::cout << "\nresult = " << vm.top() << std::endl;
225
std::cout << "-------------------------\n";
229
std::cout << "-------------------------\n";
230
std::cout << "Generating failed\n";
231
std::cout << "-------------------------\n";
236
std::string rest(iter, end);
237
std::cout << "-------------------------\n";
238
std::cout << "Parsing failed\n";
239
std::cout << "stopped at: \": " << rest << "\"\n";
240
std::cout << "-------------------------\n";
244
std::cout << "Bye... :-) \n\n";