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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*=============================================================================
Copyright (c) 2001-2010 Joel de Guzman
Copyright (c) 2001-2010 Hartmut Kaiser
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A Calculator example demonstrating generation of AST from which we generate
// a simple byte code representation being interpreted by a similar virtual
// machine.
//
// [ JDG April 28, 2008 ]
// [ HK May 05, 2008 ]
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <string>
#include "calc2_ast_vm.hpp"
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
using namespace boost::spirit;
using namespace boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// Our calculator parser grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator
: qi::grammar<Iterator, expression_ast(), space_type>
{
calculator() : calculator::base_type(expression)
{
expression =
term [_val = _1]
>> *( ('+' >> term [_val += _1])
| ('-' >> term [_val -= _1])
)
;
term =
factor [_val = _1]
>> *( ('*' >> factor [_val *= _1])
| ('/' >> factor [_val /= _1])
)
;
factor =
uint_ [_val = _1]
| '(' >> expression [_val = _1] >> ')'
| ('-' >> factor [_val = neg(_1)])
| ('+' >> factor [_val = pos(_1)])
;
}
qi::rule<Iterator, expression_ast(), space_type> expression, term, factor;
};
///////////////////////////////////////////////////////////////////////////////
// The Virtual Machine
///////////////////////////////////////////////////////////////////////////////
class vmachine
{
public:
union element {
int code;
char bytes[sizeof(int)];
};
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
, stack_ptr(stack.begin())
{
}
int top() const { return stack_ptr[-1]; };
void execute(std::vector<element> const& code);
private:
std::vector<int> stack;
std::vector<int>::iterator stack_ptr;
};
void vmachine::execute(std::vector<element> const& code)
{
std::vector<element>::const_iterator pc = code.begin();
stack_ptr = stack.begin();
while ((*pc).code && pc != code.end())
{
switch ((*pc++).code)
{
case op_neg:
stack_ptr[-1] = -stack_ptr[-1];
break;
case op_add:
--stack_ptr;
stack_ptr[-1] += stack_ptr[0];
break;
case op_sub:
--stack_ptr;
stack_ptr[-1] -= stack_ptr[0];
break;
case op_mul:
--stack_ptr;
stack_ptr[-1] *= stack_ptr[0];
break;
case op_div:
--stack_ptr;
stack_ptr[-1] /= stack_ptr[0];
break;
case op_int:
*stack_ptr++ = (*pc++).code;
break;
}
}
}
// We need to tell fusion about our binary_op and unary_op structs
// to make them a first-class fusion citizen
//
// Note: we register the members exactly in the same sequence as we need them
// in the grammar
BOOST_FUSION_ADAPT_STRUCT(
binary_op,
(expression_ast, left)
(expression_ast, right)
(int, op)
)
BOOST_FUSION_ADAPT_STRUCT(
unary_op,
(expression_ast, right)
(int, op)
)
///////////////////////////////////////////////////////////////////////////////
// Our AST grammar for the generator, this just dumps the AST as a expression
///////////////////////////////////////////////////////////////////////////////
template <typename OuputIterator, typename Delimiter>
struct generate_byte_code
: karma::grammar<OuputIterator, expression_ast(), Delimiter>
{
generate_byte_code() : generate_byte_code::base_type(ast_node)
{
ast_node %= int_node | binary_node | unary_node;
int_node %= dword(op_int) << dword;
binary_node %= ast_node << ast_node << byte_;
unary_node %= ast_node << byte_;
}
karma::rule<OuputIterator, expression_ast(), Delimiter> ast_node;
karma::rule<OuputIterator, int(), Delimiter> int_node;
karma::rule<OuputIterator, binary_op(), Delimiter> binary_node;
karma::rule<OuputIterator, unary_op(), Delimiter> unary_node;
};
///////////////////////////////////////////////////////////////////////////////
// helper function helping to deduce the delimiter type
template <typename Delimiter>
bool generate_vm_code(expression_ast const& ast,
std::vector<vmachine::element>& code, Delimiter const& d)
{
// Our generator grammar definitions
typedef char* output_iterator_type;
typedef generate_byte_code<output_iterator_type, Delimiter> generate_byte_code;
char* outbuffer = (*code.begin()).bytes;
generate_byte_code gen_vm;
return karma::generate_delimited(outbuffer, gen_vm, d, ast);
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Compile simple expressions to bytecode...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
// Our parser grammar definitions
typedef std::string::const_iterator iterator_type;
typedef calculator<iterator_type> calculator;
calculator calc;
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
expression_ast ast;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = qi::phrase_parse(iter, end, calc, space, ast);
if (r && iter == end)
{
// we assume a vm code size of 4096 is sufficient
std::vector<vmachine::element> code (4096);
r = generate_vm_code(ast, code, pad(4));
if (r)
{
vmachine vm;
vm.execute(code);
std::cout << "\nresult = " << vm.top() << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Generating failed\n";
std::cout << "-------------------------\n";
}
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \": " << rest << "\"\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
|