[DEV] add v1.66.0

This commit is contained in:
2018-01-12 21:47:58 +01:00
parent 87059bb1af
commit a97e9ae7d4
49032 changed files with 7668950 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_ANNOTATION_HPP)
#define BOOST_SPIRIT_CALC7_ANNOTATION_HPP
#include <map>
#include <boost/variant/apply_visitor.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/mpl/bool.hpp>
#include "ast.hpp"
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// The annotation handler links the AST to a map of iterator positions
// for the purpose of subsequent semantic error handling when the
// program is being compiled.
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct annotation
{
template <typename, typename>
struct result { typedef void type; };
std::vector<Iterator>& iters;
annotation(std::vector<Iterator>& iters)
: iters(iters) {}
struct set_id
{
typedef void result_type;
int id;
set_id(int id) : id(id) {}
template <typename T>
void operator()(T& x) const
{
this->dispatch(x, boost::is_base_of<ast::tagged, T>());
}
// This will catch all nodes except those inheriting from ast::tagged
template <typename T>
void dispatch(T& x, boost::mpl::false_) const
{
// (no-op) no need for tags
}
// This will catch all nodes inheriting from ast::tagged
template <typename T>
void dispatch(T& x, boost::mpl::true_) const
{
x.id = id;
}
};
void operator()(ast::operand& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
boost::apply_visitor(set_id(id), ast);
}
void operator()(ast::assignment& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
};
}
#endif

View File

@@ -0,0 +1,117 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_AST_HPP)
#define BOOST_SPIRIT_CALC7_AST_HPP
#include <boost/config/warning_disable.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <list>
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct tagged
{
int id; // Used to annotate the AST with the iterator position.
// This id is used as a key to a map<int, Iterator>
// (not really part of the AST.)
};
struct nil {};
struct signed_;
struct expression;
struct variable : tagged
{
variable(std::string const& name = "") : name(name) {}
std::string name;
};
typedef boost::variant<
nil
, unsigned int
, variable
, boost::recursive_wrapper<signed_>
, boost::recursive_wrapper<expression>
>
operand;
struct signed_
{
char sign;
operand operand_;
};
struct operation
{
char operator_;
operand operand_;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct assignment
{
variable lhs;
expression rhs;
};
struct variable_declaration
{
assignment assign;
};
typedef boost::variant<
variable_declaration
, assignment>
statement;
typedef std::list<statement> statement_list;
// print functions for debugging
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; return out; }
inline std::ostream& operator<<(std::ostream& out, variable const& var) { out << var.name; return out; }
}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast::signed_,
(char, sign)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(char, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::expression,
(client::ast::operand, first)
(std::list<client::ast::operation>, rest)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::variable_declaration,
(client::ast::assignment, assign)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::assignment,
(client::ast::variable, lhs)
(client::ast::expression, rhs)
)
#endif

View File

@@ -0,0 +1,222 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "compiler.hpp"
#include "vm.hpp"
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/assert.hpp>
namespace client { namespace code_gen
{
void program::op(int a)
{
code.push_back(a);
}
void program::op(int a, int b)
{
code.push_back(a);
code.push_back(b);
}
void program::op(int a, int b, int c)
{
code.push_back(a);
code.push_back(b);
code.push_back(c);
}
int const* program::find_var(std::string const& name) const
{
std::map<std::string, int>::const_iterator i = variables.find(name);
if (i == variables.end())
return 0;
return &i->second;
}
void program::add_var(std::string const& name)
{
std::size_t n = variables.size();
variables[name] = n;
}
void program::print_variables(std::vector<int> const& stack) const
{
typedef std::pair<std::string, int> pair;
BOOST_FOREACH(pair const& p, variables)
{
std::cout << " " << p.first << ": " << stack[p.second] << std::endl;
}
}
void program::print_assembler() const
{
std::vector<int>::const_iterator pc = code.begin();
std::vector<std::string> locals(variables.size());
typedef std::pair<std::string, int> pair;
BOOST_FOREACH(pair const& p, variables)
{
locals[p.second] = p.first;
std::cout << "local "
<< p.first << ", @" << p.second << std::endl;
}
while (pc != code.end())
{
switch (*pc++)
{
case op_neg:
std::cout << "op_neg" << std::endl;
break;
case op_add:
std::cout << "op_add" << std::endl;
break;
case op_sub:
std::cout << "op_sub" << std::endl;
break;
case op_mul:
std::cout << "op_mul" << std::endl;
break;
case op_div:
std::cout << "op_div" << std::endl;
break;
case op_load:
std::cout << "op_load " << locals[*pc++] << std::endl;
break;
case op_store:
std::cout << "op_store " << locals[*pc++] << std::endl;
break;
case op_int:
std::cout << "op_int " << *pc++ << std::endl;
break;
case op_stk_adj:
std::cout << "op_stk_adj " << *pc++ << std::endl;
break;
}
}
}
bool compiler::operator()(unsigned int x) const
{
program.op(op_int, x);
return true;
}
bool compiler::operator()(ast::variable const& x) const
{
int const* p = program.find_var(x.name);
if (p == 0)
{
std::cout << x.id << std::endl;
error_handler(x.id, "Undeclared variable: " + x.name);
return false;
}
program.op(op_load, *p);
return true;
}
bool compiler::operator()(ast::operation const& x) const
{
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.operator_)
{
case '+': program.op(op_add); break;
case '-': program.op(op_sub); break;
case '*': program.op(op_mul); break;
case '/': program.op(op_div); break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::signed_ const& x) const
{
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.sign)
{
case '-': program.op(op_neg); break;
case '+': break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::expression const& x) const
{
if (!boost::apply_visitor(*this, x.first))
return false;
BOOST_FOREACH(ast::operation const& oper, x.rest)
{
if (!(*this)(oper))
return false;
}
return true;
}
bool compiler::operator()(ast::assignment const& x) const
{
if (!(*this)(x.rhs))
return false;
int const* p = program.find_var(x.lhs.name);
if (p == 0)
{
std::cout << x.lhs.id << std::endl;
error_handler(x.lhs.id, "Undeclared variable: " + x.lhs.name);
return false;
}
program.op(op_store, *p);
return true;
}
bool compiler::operator()(ast::variable_declaration const& x) const
{
int const* p = program.find_var(x.assign.lhs.name);
if (p != 0)
{
std::cout << x.assign.lhs.id << std::endl;
error_handler(x.assign.lhs.id, "Duplicate variable: " + x.assign.lhs.name);
return false;
}
bool r = (*this)(x.assign.rhs);
if (r) // don't add the variable if the RHS fails
{
program.add_var(x.assign.lhs.name);
program.op(op_store, *program.find_var(x.assign.lhs.name));
}
return r;
}
bool compiler::operator()(ast::statement_list const& x) const
{
program.clear();
// op_stk_adj 0 for now. we'll know how many variables we'll have later
program.op(op_stk_adj, 0);
BOOST_FOREACH(ast::statement const& s, x)
{
if (!boost::apply_visitor(*this, s))
{
program.clear();
return false;
}
}
program[1] = program.nvars(); // now store the actual number of variables
return true;
}
}}

View File

@@ -0,0 +1,85 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_COMPILER_HPP)
#define BOOST_SPIRIT_CALC7_COMPILER_HPP
#include "ast.hpp"
#include "error_handler.hpp"
#include <vector>
#include <map>
#include <boost/function.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
namespace client { namespace code_gen
{
///////////////////////////////////////////////////////////////////////////
// The Program
///////////////////////////////////////////////////////////////////////////
struct program
{
void op(int a);
void op(int a, int b);
void op(int a, int b, int c);
int& operator[](std::size_t i) { return code[i]; }
int const& operator[](std::size_t i) const { return code[i]; }
void clear() { code.clear(); variables.clear(); }
std::vector<int> const& operator()() const { return code; }
int nvars() const { return variables.size(); }
int const* find_var(std::string const& name) const;
void add_var(std::string const& name);
void print_variables(std::vector<int> const& stack) const;
void print_assembler() const;
private:
std::map<std::string, int> variables;
std::vector<int> code;
};
///////////////////////////////////////////////////////////////////////////
// The Compiler
///////////////////////////////////////////////////////////////////////////
struct compiler
{
typedef bool result_type;
template <typename ErrorHandler>
compiler(client::code_gen::program& program, ErrorHandler& error_handler_)
: program(program)
{
using namespace boost::phoenix::arg_names;
namespace phx = boost::phoenix;
using boost::phoenix::function;
error_handler = function<ErrorHandler>(error_handler_)(
"Error! ", _2, phx::cref(error_handler_.iters)[_1]);
}
bool operator()(ast::nil) const { BOOST_ASSERT(0); return false; }
bool operator()(unsigned int x) const;
bool operator()(ast::variable const& x) const;
bool operator()(ast::operation const& x) const;
bool operator()(ast::signed_ const& x) const;
bool operator()(ast::expression const& x) const;
bool operator()(ast::assignment const& x) const;
bool operator()(ast::variable_declaration const& x) const;
bool operator()(ast::statement_list const& x) const;
client::code_gen::program& program;
boost::function<
void(int tag, std::string const& what)>
error_handler;
};
}}
#endif

View File

@@ -0,0 +1,93 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_ERROR_HANDLER_HPP)
#define BOOST_SPIRIT_CALC7_ERROR_HANDLER_HPP
#include <iostream>
#include <string>
#include <vector>
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// The error handler
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct error_handler
{
template <typename, typename, typename>
struct result { typedef void type; };
error_handler(Iterator first, Iterator last)
: first(first), last(last) {}
template <typename Message, typename What>
void operator()(
Message const& message,
What const& what,
Iterator err_pos) const
{
int line;
Iterator line_start = get_pos(err_pos, line);
if (err_pos != last)
{
std::cout << message << what << " line " << line << ':' << std::endl;
std::cout << get_line(line_start) << std::endl;
for (; line_start != err_pos; ++line_start)
std::cout << ' ';
std::cout << '^' << std::endl;
}
else
{
std::cout << "Unexpected end of file. ";
std::cout << message << what << " line " << line << std::endl;
}
}
Iterator get_pos(Iterator err_pos, int& line) const
{
line = 1;
Iterator i = first;
Iterator line_start = first;
while (i != err_pos)
{
bool eol = false;
if (i != err_pos && *i == '\r') // CR
{
eol = true;
line_start = ++i;
}
if (i != err_pos && *i == '\n') // LF
{
eol = true;
line_start = ++i;
}
if (eol)
++line;
else
++i;
}
return line_start;
}
std::string get_line(Iterator err_pos) const
{
Iterator i = err_pos;
// position i to the next EOL
while (i != last && (*i != '\r' && *i != '\n'))
++i;
return std::string(err_pos, i);
}
Iterator first;
Iterator last;
std::vector<Iterator> iters;
};
}
#endif

View File

@@ -0,0 +1,14 @@
/*=============================================================================
Copyright (c) 2001-2010 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "expression_def.hpp"
typedef std::string::const_iterator iterator_type;
template struct client::parser::expression<iterator_type>;

View File

@@ -0,0 +1,53 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_EXPRESSION_HPP)
#define BOOST_SPIRIT_CALC7_EXPRESSION_HPP
///////////////////////////////////////////////////////////////////////////////
// Spirit v2.5 allows you to suppress automatic generation
// of predefined terminals to speed up complation. With
// BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
// responsible in creating instances of the terminals that
// you need (e.g. see qi::uint_type uint_ below).
#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment this if you want to enable debugging
// #define BOOST_SPIRIT_QI_DEBUG
///////////////////////////////////////////////////////////////////////////////
#include <boost/spirit/include/qi.hpp>
#include "ast.hpp"
#include "error_handler.hpp"
#include <vector>
namespace client { namespace parser
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// The expression grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct expression : qi::grammar<Iterator, ast::expression(), ascii::space_type>
{
expression(error_handler<Iterator>& error_handler);
qi::rule<Iterator, ast::expression(), ascii::space_type> expr;
qi::rule<Iterator, ast::expression(), ascii::space_type> additive_expr;
qi::rule<Iterator, ast::expression(), ascii::space_type> multiplicative_expr;
qi::rule<Iterator, ast::operand(), ascii::space_type> unary_expr;
qi::rule<Iterator, ast::operand(), ascii::space_type> primary_expr;
qi::rule<Iterator, std::string(), ascii::space_type> identifier;
};
}}
#endif

View File

@@ -0,0 +1,94 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "expression.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
#include <boost/spirit/include/phoenix_function.hpp>
namespace client { namespace parser
{
template <typename Iterator>
expression<Iterator>::expression(error_handler<Iterator>& error_handler)
: expression::base_type(expr)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::char_type char_;
qi::uint_type uint_;
qi::_val_type _val;
qi::raw_type raw;
qi::lexeme_type lexeme;
qi::alpha_type alpha;
qi::alnum_type alnum;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef function<client::error_handler<Iterator> > error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
expr =
additive_expr.alias()
;
additive_expr =
multiplicative_expr
>> *( (char_('+') > multiplicative_expr)
| (char_('-') > multiplicative_expr)
)
;
multiplicative_expr =
unary_expr
>> *( (char_('*') > unary_expr)
| (char_('/') > unary_expr)
)
;
unary_expr =
primary_expr
| (char_('-') > primary_expr)
| (char_('+') > primary_expr)
;
primary_expr =
uint_
| identifier
| '(' > expr > ')'
;
identifier =
raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(additive_expr)
(multiplicative_expr)
(unary_expr)
(primary_expr)
(identifier)
);
// Error handling: on error in expr, call error_handler.
on_error<fail>(expr,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
// Annotation: on success in primary_expr, call annotation.
on_success(primary_expr,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,101 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Now we'll introduce variables and assignment. This time, we'll also
// be renaming some of the rules -- a strategy for a grander scheme
// to come ;-)
//
// This version also shows off grammar modularization. Here you will
// see how expressions and statements are built as modular grammars.
//
// [ JDG April 9, 2007 ] spirit2
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
//
///////////////////////////////////////////////////////////////////////////////
#include "statement.hpp"
#include "vm.hpp"
#include "compiler.hpp"
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Statement parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type some statements... ";
std::cout << "An empty line ends input, compiles, runs and prints results\n\n";
std::cout << "Example:\n\n";
std::cout << " var a = 123;\n";
std::cout << " var b = 456;\n";
std::cout << " var c = a + b * 2;\n\n";
std::cout << "-------------------------\n";
std::string str;
std::string source;
while (std::getline(std::cin, str))
{
if (str.empty())
break;
source += str + '\n';
}
typedef std::string::const_iterator iterator_type;
iterator_type iter = source.begin();
iterator_type end = source.end();
client::vmachine vm; // Our virtual machine
client::code_gen::program program; // Our VM program
client::ast::statement_list ast; // Our AST
client::error_handler<iterator_type>
error_handler(iter, end); // Our error handler
client::parser::statement<iterator_type>
parser(error_handler); // Our parser
client::code_gen::compiler
compile(program, error_handler); // Our compiler
boost::spirit::ascii::space_type space;
bool success = phrase_parse(iter, end, parser, space, ast);
std::cout << "-------------------------\n";
if (success && iter == end)
{
if (compile(ast))
{
std::cout << "Success\n";
std::cout << "-------------------------\n";
vm.execute(program());
std::cout << "-------------------------\n";
std::cout << "Assembler----------------\n\n";
program.print_assembler();
std::cout << "-------------------------\n";
std::cout << "Results------------------\n\n";
program.print_variables(vm.get_stack());
}
else
{
std::cout << "Compile failure\n";
}
}
else
{
std::cout << "Parse failure\n";
}
std::cout << "-------------------------\n\n";
return 0;
}

View File

@@ -0,0 +1,14 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "statement_def.hpp"
typedef std::string::const_iterator iterator_type;
template struct client::parser::statement<iterator_type>;

View File

@@ -0,0 +1,32 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_STATEMENT_HPP)
#define BOOST_SPIRIT_CALC7_STATEMENT_HPP
#include "expression.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The statement grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct statement : qi::grammar<Iterator, ast::statement_list(), ascii::space_type>
{
statement(error_handler<Iterator>& error_handler);
expression<Iterator> expr;
qi::rule<Iterator, ast::statement_list(), ascii::space_type> statement_list;
qi::rule<Iterator, ast::variable_declaration(), ascii::space_type> variable_declaration;
qi::rule<Iterator, ast::assignment(), ascii::space_type> assignment;
qi::rule<Iterator, std::string(), ascii::space_type> identifier;
};
}}
#endif

View File

@@ -0,0 +1,75 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "statement.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
namespace client { namespace parser
{
template <typename Iterator>
statement<Iterator>::statement(error_handler<Iterator>& error_handler)
: statement::base_type(statement_list), expr(error_handler)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
qi::raw_type raw;
qi::lexeme_type lexeme;
qi::alpha_type alpha;
qi::alnum_type alnum;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef function<client::error_handler<Iterator> > error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
statement_list =
+(variable_declaration | assignment)
;
identifier =
raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
variable_declaration =
lexeme["var" >> !(alnum | '_')] // make sure we have whole words
> assignment
;
assignment =
identifier
> '='
> expr
> ';'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(statement_list)
(identifier)
(variable_declaration)
(assignment)
);
// Error handling: on error in statement_list, call error_handler.
on_error<fail>(statement_list,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
// Annotation: on success in assignment, call annotation.
on_success(assignment,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,66 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "vm.hpp"
namespace client
{
void vmachine::execute(std::vector<int> const& code)
{
std::vector<int>::const_iterator pc = code.begin();
std::vector<int>::iterator locals = stack.begin();
stack_ptr = stack.begin();
while (pc != code.end())
{
switch (*pc++)
{
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_load:
*stack_ptr++ = locals[*pc++];
break;
case op_store:
--stack_ptr;
locals[*pc++] = stack_ptr[0];
break;
case op_int:
*stack_ptr++ = *pc++;
break;
case op_stk_adj:
stack_ptr = stack.begin() + *pc++;
break;
}
}
}
}

View File

@@ -0,0 +1,52 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CALC7_VM_HPP)
#define BOOST_SPIRIT_CALC7_VM_HPP
#include <vector>
namespace client
{
///////////////////////////////////////////////////////////////////////////
// The Virtual Machine
///////////////////////////////////////////////////////////////////////////
enum byte_code
{
op_neg, // negate the top stack entry
op_add, // add top two stack entries
op_sub, // subtract top two stack entries
op_mul, // multiply top two stack entries
op_div, // divide top two stack entries
op_load, // load a variable
op_store, // store a variable
op_int, // push constant integer into the stack
op_stk_adj // adjust the stack for local variables
};
class vmachine
{
public:
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
, stack_ptr(stack.begin())
{
}
void execute(std::vector<int> const& code);
std::vector<int> const& get_stack() const { return stack; };
private:
std::vector<int> stack;
std::vector<int>::iterator stack_ptr;
};
}
#endif