early-access version 3088

This commit is contained in:
pineappleEA
2022-11-05 15:35:56 +01:00
parent 4e4fc25ce3
commit b601909c6d
35519 changed files with 5996896 additions and 860 deletions

View File

@@ -0,0 +1,48 @@
#==============================================================================
# Copyright (c) 2001-2007 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)
#==============================================================================
project spirit-qi-example
: requirements
<c++-template-depth>300
:
:
;
exe actions_ : actions.cpp ;
exe sum : sum.cpp ;
exe complex_number : complex_number.cpp ;
exe employee : employee.cpp ;
exe roman : roman.cpp ;
exe reference : reference.cpp ;
exe mini_xml1 : mini_xml1.cpp ;
exe mini_xml2 : mini_xml2.cpp ;
exe mini_xml3 : mini_xml3.cpp ;
exe num_list1 : num_list1.cpp ;
exe num_list2 : num_list2.cpp ;
exe num_list3 : num_list3.cpp ;
exe num_list4 : num_list4.cpp ;
exe reorder_struct : reorder_struct.cpp ;
exe parse_date : parse_date.cpp ;
exe expect : expect.cpp ;
exe key_value_sequence : key_value_sequence.cpp ;
exe key_value_sequence_ordered : key_value_sequence_ordered.cpp ;
exe key_value_sequence_empty_value : key_value_sequence_empty_value.cpp ;
exe iter_pos_parser : iter_pos_parser.cpp ;
exe boost_array : boost_array.cpp ;
exe display_attribute_type : display_attribute_type.cpp ;
exe adapt_template_struct : adapt_template_struct.cpp ;
exe unescaped_string : unescaped_string.cpp ;
exe calc_utree_naive : calc_utree_naive.cpp ;
exe calc_utree_ast : calc_utree_ast.cpp ;
exe calc_utree : calc_utree.cpp ;
exe nabialek : nabialek.cpp ;
exe typeof : typeof.cpp ;

View File

@@ -0,0 +1,109 @@
/*=============================================================================
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)
=============================================================================*/
#include <boost/spirit/include/qi.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/bind/bind.hpp>
#include <iostream>
// Presented are various ways to attach semantic actions
// * Using plain function pointer
// * Using simple function object
// * Using boost.bind with a plain function
// * Using boost.bind with a member function
// * Using boost.lambda
//[tutorial_semantic_action_functions
namespace client
{
namespace qi = boost::spirit::qi;
// A plain function
void print(int const& i)
{
std::cout << i << std::endl;
}
// A member function
struct writer
{
void print(int const& i) const
{
std::cout << i << std::endl;
}
};
// A function object
struct print_action
{
void operator()(int const& i, qi::unused_type, qi::unused_type) const
{
std::cout << i << std::endl;
}
};
}
//]
int main()
{
using boost::spirit::qi::int_;
using boost::spirit::qi::parse;
using client::print;
using client::writer;
using client::print_action;
{ // example using plain function
char const *first = "{42}", *last = first + std::strlen(first);
//[tutorial_attach_actions1
parse(first, last, '{' >> int_[&print] >> '}');
//]
}
{ // example using simple function object
char const *first = "{43}", *last = first + std::strlen(first);
//[tutorial_attach_actions2
parse(first, last, '{' >> int_[print_action()] >> '}');
//]
}
{ // example using boost.bind with a plain function
char const *first = "{44}", *last = first + std::strlen(first);
using boost::placeholders::_1;
//[tutorial_attach_actions3
parse(first, last, '{' >> int_[boost::bind(&print, _1)] >> '}');
//]
}
{ // example using boost.bind with a member function
char const *first = "{44}", *last = first + std::strlen(first);
using boost::placeholders::_1;
//[tutorial_attach_actions4
writer w;
parse(first, last, '{' >> int_[boost::bind(&writer::print, &w, _1)] >> '}');
//]
}
{ // example using boost.lambda
namespace lambda = boost::lambda;
char const *first = "{45}", *last = first + std::strlen(first);
using lambda::_1;
//[tutorial_attach_actions5
parse(first, last, '{' >> int_[std::cout << _1 << '\n'] >> '}');
//]
}
return 0;
}

View File

@@ -0,0 +1,92 @@
// 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)
// This example demonstrates a trick allowing to adapt a template data
// structure as a Fusion sequence in order to use is for direct attribute
// propagation. For more information see
// http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
namespace client
{
template <typename A, typename B>
struct data
{
A a;
B b;
};
template <typename Iterator, typename A, typename B>
struct data_grammar : qi::grammar<Iterator, data<A, B>()>
{
data_grammar() : data_grammar::base_type(start)
{
start = real_start;
real_start = qi::auto_ >> ',' >> qi::auto_;
}
qi::rule<Iterator, data<A, B>()> start;
qi::rule<Iterator, fusion::vector<A&, B&>()> real_start;
};
}
namespace boost { namespace spirit { namespace traits
{
template <typename A, typename B>
struct transform_attribute<client::data<A, B>, fusion::vector<A&, B&>, qi::domain>
{
typedef fusion::vector<A&, B&> type;
static type pre(client::data<A, B>& val) { return type(val.a, val.b); }
static void post(client::data<A, B>&, fusion::vector<A&, B&> const&) {}
static void fail(client::data<A, B>&) {}
};
}}}
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "\t\tA parser for Spirit utilizing an adapted template ...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Give me two comma separated integers:\n";
std::cout << "Type [q or Q] to quit\n\n";
std::string str;
client::data_grammar<std::string::const_iterator, long, int> g; // Our grammar
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
client::data<long, int> d;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, g, qi::space, d);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "got: " << d.a << "," << d.b << std::endl;
std::cout << "\n-------------------------\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
return 0;
}

View File

@@ -0,0 +1,119 @@
// Copyright (c) 2009 Erik Bryan
// Copyright (c) 2007-2010 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <string>
#include <vector>
#include <boost/array.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// create a wrapper holding the boost::array and a current insertion point
namespace client
{
namespace detail
{
template <typename T>
struct adapt_array;
template <typename T, std::size_t N>
struct adapt_array<boost::array<T, N> >
{
typedef boost::array<T, N> array_type;
adapt_array(array_type& arr)
: arr_(arr), current_(0) {}
// expose a push_back function compatible with std containers
bool push_back(typename array_type::value_type const& val)
{
// if the array is full, we need to bail out
// returning false will fail the parse
if (current_ >= N)
return false;
arr_[current_++] = val;
return true;
}
array_type& arr_;
std::size_t current_;
};
}
namespace result_of
{
template <typename T>
struct adapt_array;
template <typename T, std::size_t N>
struct adapt_array<boost::array<T, N> >
{
typedef detail::adapt_array<boost::array<T, N> > type;
};
}
template <typename T, std::size_t N>
inline detail::adapt_array<boost::array<T, N> >
adapt_array(boost::array<T, N>& arr)
{
return detail::adapt_array<boost::array<T, N> >(arr);
}
}
///////////////////////////////////////////////////////////////////////////////
// specialize Spirit's container specific customization points for our adaptor
namespace boost { namespace spirit { namespace traits
{
template <typename T, std::size_t N>
struct is_container<client::detail::adapt_array<boost::array<T, N> > >
: boost::mpl::true_
{};
template <typename T, std::size_t N>
struct container_value<client::detail::adapt_array<boost::array<T, N> > >
{
typedef T type; // value type of container
};
template <typename T, std::size_t N>
struct push_back_container<
client::detail::adapt_array<boost::array<T, N> >, T>
{
static bool call(client::detail::adapt_array<boost::array<T, N> >& c
, T const& val)
{
return c.push_back(val);
}
};
}}}
int main()
{
typedef std::string::const_iterator iterator_type;
typedef boost::array<int, 2> array_type;
typedef client::result_of::adapt_array<array_type>::type adapted_type;
array_type arr;
std::string str = "1 2";
iterator_type iter = str.begin();
iterator_type end = str.end();
qi::rule<iterator_type, adapted_type(), ascii::space_type> r = *qi::int_;
adapted_type attr = client::adapt_array(arr);
bool result = qi::phrase_parse(iter, end, r, ascii::space, attr);
if (result)
std::cout << "Parsed: " << arr[0] << ", " << arr[1] << std::endl;
return 0;
}

View File

@@ -0,0 +1,165 @@
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Plain calculator example demonstrating the grammar. The parser is a
// syntax checker only and does not do any semantic evaluation.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 4, 2007 ] spirit2
// [ HK November 30, 2010 ] spirit2/utree
//
///////////////////////////////////////////////////////////////////////////////
// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/function.hpp>
#include <iostream>
#include <string>
#if BOOST_PHOENIX_VERSION == 0x2000
namespace boost { namespace phoenix
{
// There's a bug in the Phoenix V2 type deduction mechanism that prevents
// correct return type deduction for the math operations below. Newer
// versions of Phoenix will be switching to BOOST_TYPEOF. In the meantime,
// we will use the specializations helping with return type deduction
// below:
template <>
struct result_of_plus<spirit::utree&, spirit::utree&>
{
typedef spirit::utree type;
};
template <>
struct result_of_minus<spirit::utree&, spirit::utree&>
{
typedef spirit::utree type;
};
template <>
struct result_of_multiplies<spirit::utree&, spirit::utree&>
{
typedef spirit::utree type;
};
template <>
struct result_of_divides<spirit::utree&, spirit::utree&>
{
typedef spirit::utree type;
};
template <>
struct result_of_negate<spirit::utree&>
{
typedef spirit::utree type;
};
}}
#endif
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace spirit = boost::spirit;
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
{
calculator() : calculator::base_type(expression)
{
using qi::uint_;
using qi::_val;
using qi::_1;
expression =
term [_val = _1]
>> *( ('+' >> term [_val = _val + _1])
| ('-' >> term [_val = _val - _1])
)
;
term =
factor [_val = _1]
>> *( ('*' >> factor [_val = _val * _1])
| ('/' >> factor [_val = _val / _1])
)
;
factor =
uint_ [_val = _1]
| '(' >> expression [_val = _1] >> ')'
| ('-' >> factor [_val = -_1])
| ('+' >> factor [_val = _1])
;
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(term);
BOOST_SPIRIT_DEBUG_NODE(factor);
}
qi::rule<Iterator, ascii::space_type, spirit::utree()> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
using boost::spirit::utree;
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
utree ut;
bool r = phrase_parse(iter, end, calc, space, ut);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded: " << ut << "\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;
}

View File

@@ -0,0 +1,163 @@
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Plain calculator example demonstrating the grammar. The parser is a
// syntax checker only and does not do any semantic evaluation.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 4, 2007 ] spirit2
// [ HK November 30, 2010 ] spirit2/utree
//
///////////////////////////////////////////////////////////////////////////////
// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/function.hpp>
#include <iostream>
#include <string>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace spirit = boost::spirit;
struct expr
{
template <typename T1, typename T2 = void>
struct result { typedef void type; };
expr(char op) : op(op) {}
void operator()(spirit::utree& expr, spirit::utree const& rhs) const
{
spirit::utree lhs;
lhs.swap(expr);
expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
expr.push_back(lhs);
expr.push_back(rhs);
}
char const op;
};
boost::phoenix::function<expr> const plus = expr('+');
boost::phoenix::function<expr> const minus = expr('-');
boost::phoenix::function<expr> const times = expr('*');
boost::phoenix::function<expr> const divide = expr('/');
struct negate_expr
{
template <typename T1, typename T2 = void>
struct result { typedef void type; };
void operator()(spirit::utree& expr, spirit::utree const& rhs) const
{
char const op = '-';
expr.clear();
expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
expr.push_back(rhs);
}
};
boost::phoenix::function<negate_expr> neg;
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
{
calculator() : calculator::base_type(expression)
{
using qi::uint_;
using qi::_val;
using qi::_1;
expression =
term [_val = _1]
>> *( ('+' >> term [plus(_val, _1)])
| ('-' >> term [minus(_val, _1)])
)
;
term =
factor [_val = _1]
>> *( ('*' >> factor [times(_val, _1)])
| ('/' >> factor [divide(_val, _1)])
)
;
factor =
uint_ [_val = _1]
| '(' >> expression [_val = _1] >> ')'
| ('-' >> factor [neg(_val, _1)])
| ('+' >> factor [_val = _1])
;
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(term);
BOOST_SPIRIT_DEBUG_NODE(factor);
}
qi::rule<Iterator, ascii::space_type, spirit::utree()> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
using boost::spirit::utree;
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
utree ut;
bool r = phrase_parse(iter, end, calc, space, ut);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded: " << ut << "\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;
}

View File

@@ -0,0 +1,133 @@
/*=============================================================================
Copyright (c) 2001-2011 Hartmut Kaiser
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Plain calculator example demonstrating the grammar. The parser is a
// syntax checker only and does not do any semantic evaluation.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 4, 2007 ] spirit2
// [ HK November 30, 2010 ] spirit2/utree
//
///////////////////////////////////////////////////////////////////////////////
// This rather naive example demonstrates that you can pass an instance of a
// utree as the attribute for almost any grammar. As the result the utree will
// be filled with the parse tree as generated during the parsing. This is most
// of the time not what's desired, but is usually a good first step in order to
// prepare your grammar to generate a customized AST. See the calc_utree_ast
// example for a modified version of this grammar filling the attribute with a
// AST (abstract syntax tree) representing the math expression as matched from
// the input.
// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <iostream>
#include <string>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace spirit = boost::spirit;
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type, spirit::utree()>
{
calculator() : calculator::base_type(expression)
{
using qi::uint_;
using qi::char_;
expression =
term
>> *( (char_('+') >> term)
| (char_('-') >> term)
)
;
term =
factor
>> *( (char_('*') >> factor)
| (char_('/') >> factor)
)
;
factor =
uint_
| '(' >> expression >> ')'
| (char_('-') >> factor)
| (char_('+') >> factor)
;
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(term);
BOOST_SPIRIT_DEBUG_NODE(factor);
}
qi::rule<Iterator, ascii::space_type, spirit::utree()> expression;
qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> term;
qi::rule<Iterator, ascii::space_type, spirit::utree::list_type()> factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
using boost::spirit::utree;
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
utree ut;
bool r = phrase_parse(iter, end, calc, space, ut);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded: " << ut << "\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;
}

View File

@@ -0,0 +1,132 @@
#==============================================================================
# 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)
#==============================================================================
project spirit-qi-compiler_tutorial
: requirements
<c++-template-depth>300
:
;
import modules ;
exe calc1 : calc1.cpp ;
exe calc2 : calc2.cpp ;
exe calc3 : calc3.cpp ;
exe calc4 : calc4.cpp ;
exe calc5 : calc5.cpp ;
exe calc6 : calc6.cpp ;
exe calc7 :
calc7/vm.cpp
calc7/compiler.cpp
calc7/expression.cpp
calc7/statement.cpp
calc7/main.cpp
;
exe calc8 :
calc8/vm.cpp
calc8/compiler.cpp
calc8/expression.cpp
calc8/statement.cpp
calc8/main.cpp
;
exe mini_c :
mini_c/vm.cpp
mini_c/compiler.cpp
mini_c/expression.cpp
mini_c/statement.cpp
mini_c/function.cpp
mini_c/main.cpp
;
exe conjure1 :
conjure1/vm.cpp
conjure1/compiler.cpp
conjure1/expression.cpp
conjure1/statement.cpp
conjure1/function.cpp
conjure1/main.cpp
;
exe conjure2 :
conjure2/compiler.cpp
conjure2/expression.cpp
conjure2/function.cpp
conjure2/lexer.cpp
conjure2/main.cpp
conjure2/statement.cpp
conjure2/vm.cpp
;
#==============================================================================
# conjure3 and above require LLVM. Make sure you provide the
# LLVM_PATH in your bjam invocation. E.g.:
#
# bjam -sLLVM_PATH=C:/dev/llvm conjure3
#
#==============================================================================
if [ modules.peek : LLVM_PATH ]
{
LLVM_PATH = [ modules.peek : LLVM_PATH ] ;
}
if $(LLVM_PATH)
{
path-constant LLVM_LIB_DEBUG_PATH : $(LLVM_PATH)/lib/Debug ;
path-constant LLVM_LIB_RELEASE_PATH : $(LLVM_PATH)/lib/Release ;
llvm_linker_flags =
"advapi32.lib"
"shell32.lib"
;
llvm_debug_libs = [ glob $(LLVM_LIB_DEBUG_PATH)/LLVM*.lib ] ;
llvm_release_libs = [ glob $(LLVM_LIB_RELEASE_PATH)/LLVM*.lib ] ;
rule build_exe_1 ( target-name : sources + : requirements * )
{
local llvm_lib ;
if <variant>debug in $(requirements)
{
llvm_lib = $(llvm_debug_libs) ;
}
else
{
llvm_lib = $(llvm_release_libs) ;
}
exe $(target-name)
: $(sources)
$(llvm_lib)
: $(requirements)
<toolset>msvc
<include>$(LLVM_PATH)/include
<linkflags>$(llvm_linker_flags)
;
}
rule build_exe ( target-name : sources + )
{
build_exe_1 $(target-name) : $(sources) : <variant>debug ;
build_exe_1 $(target-name) : $(sources) : <variant>release ;
}
build_exe conjure3 :
conjure3/compiler.cpp
conjure3/expression.cpp
conjure3/function.cpp
conjure3/lexer.cpp
conjure3/main.cpp
conjure3/statement.cpp
conjure3/vm.cpp
;
}

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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Plain calculator example demonstrating the grammar. The parser is a
// syntax checker only and does not do any semantic evaluation.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 4, 2007 ] spirit2
// [ JDG February 21, 2011 ] spirit2.5
//
///////////////////////////////////////////////////////////////////////////////
// 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
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::uint_type uint_;
expression =
term
>> *( ('+' >> term)
| ('-' >> term)
)
;
term =
factor
>> *( ('*' >> factor)
| ('/' >> factor)
)
;
factor =
uint_
| '(' >> expression >> ')'
| ('-' >> factor)
| ('+' >> factor)
;
}
qi::rule<Iterator, ascii::space_type> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
boost::spirit::ascii::space_type space; // Our skipper
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, calc, space);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\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;
}

View File

@@ -0,0 +1,130 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A Calculator example demonstrating the grammar and semantic actions
// using plain functions. The parser prints code suitable for a stack
// based virtual machine.
//
// [ JDG May 10, 2002 ] spirit1
// [ JDG March 4, 2007 ] spirit2
// [ JDG February 21, 2011 ] spirit2.5
//
///////////////////////////////////////////////////////////////////////////////
// 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
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// Semantic actions
////////////////////////////////////////////////////////1///////////////////////
namespace
{
void do_int(int n) { std::cout << "push " << n << std::endl; }
void do_add() { std::cout << "add\n"; }
void do_subt() { std::cout << "subtract\n"; }
void do_mult() { std::cout << "mult\n"; }
void do_div() { std::cout << "divide\n"; }
void do_neg() { std::cout << "negate\n"; }
}
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::uint_type uint_;
expression =
term
>> *( ('+' >> term [&do_add])
| ('-' >> term [&do_subt])
)
;
term =
factor
>> *( ('*' >> factor [&do_mult])
| ('/' >> factor [&do_div])
)
;
factor =
uint_ [&do_int]
| '(' >> expression >> ')'
| ('-' >> factor [&do_neg])
| ('+' >> factor)
;
}
qi::rule<Iterator, ascii::space_type> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
boost::spirit::ascii::space_type space; // Our skipper
calculator calc; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, calc, space);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\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;
}

View File

@@ -0,0 +1,123 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A calculator example demonstrating the grammar and semantic actions
// using phoenix to do the actual expression evaluation. The parser is
// essentially an "interpreter" that evaluates expressions on the fly.
//
// [ JDG June 29, 2002 ] spirit1
// [ JDG March 5, 2007 ] spirit2
//
///////////////////////////////////////////////////////////////////////////////
// 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
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix/operator.hpp>
#include <iostream>
#include <string>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::_val_type _val;
qi::_1_type _1;
qi::uint_type uint_;
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 = -_1])
| ('+' >> factor [_val = _1])
;
}
qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
boost::spirit::ascii::space_type space; // Our skipper
calculator calc; // Our grammar
std::string str;
int result;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, calc, space, result);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "result = " << result << std::endl;
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;
}

View File

@@ -0,0 +1,284 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// A Calculator example demonstrating generation of AST. The AST,
// once created, is traversed, 1) To print its contents and
// 2) To evaluate the result.
//
// [ JDG April 28, 2008 ] For BoostCon 2008
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
//
///////////////////////////////////////////////////////////////////////////////
// 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
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include <boost/spirit/include/qi.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct nil {};
struct signed_;
struct program;
typedef boost::variant<
nil
, unsigned int
, boost::recursive_wrapper<signed_>
, boost::recursive_wrapper<program>
>
operand;
struct signed_
{
char sign;
operand operand_;
};
struct operation
{
char operator_;
operand operand_;
};
struct program
{
operand first;
std::list<operation> rest;
};
}}
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::program,
(client::ast::operand, first)
(std::list<client::ast::operation>, rest)
)
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST Printer
///////////////////////////////////////////////////////////////////////////
struct printer
{
typedef void result_type;
void operator()(nil) const {}
void operator()(unsigned int n) const { std::cout << n; }
void operator()(operation const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': std::cout << " add"; break;
case '-': std::cout << " subt"; break;
case '*': std::cout << " mult"; break;
case '/': std::cout << " div"; break;
}
}
void operator()(signed_ const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': std::cout << " neg"; break;
case '+': std::cout << " pos"; break;
}
}
void operator()(program const& x) const
{
boost::apply_visitor(*this, x.first);
BOOST_FOREACH(operation const& oper, x.rest)
{
std::cout << ' ';
(*this)(oper);
}
}
};
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
struct eval
{
typedef int result_type;
int operator()(nil) const { BOOST_ASSERT(0); return 0; }
int operator()(unsigned int n) const { return n; }
int operator()(operation const& x, int lhs) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(signed_ const& x) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': return -rhs;
case '+': return +rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(program const& x) const
{
int state = boost::apply_visitor(*this, x.first);
BOOST_FOREACH(operation const& oper, x.rest)
{
state = (*this)(oper, state);
}
return state;
}
};
}}
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// The calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ast::program(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::uint_type uint_;
qi::char_type char_;
expression =
term
>> *( (char_('+') >> term)
| (char_('-') >> term)
)
;
term =
factor
>> *( (char_('*') >> factor)
| (char_('/') >> factor)
)
;
factor =
uint_
| '(' >> expression >> ')'
| (char_('-') >> factor)
| (char_('+') >> factor)
;
}
qi::rule<Iterator, ast::program(), ascii::space_type> expression;
qi::rule<Iterator, ast::program(), ascii::space_type> term;
qi::rule<Iterator, ast::operand(), ascii::space_type> factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
typedef client::ast::program ast_program;
typedef client::ast::printer ast_print;
typedef client::ast::eval ast_eval;
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
calculator calc; // Our grammar
ast_program program; // Our program (AST)
ast_print print; // Prints the program
ast_eval eval; // Evaluates the program
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
boost::spirit::ascii::space_type space;
bool r = phrase_parse(iter, end, calc, space, program);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
print(program);
std::cout << "\nResult: " << eval(program) << std::endl;
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;
}

View File

@@ -0,0 +1,338 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Same as Calc4, this time, we'll incorporate debugging support,
// plus error handling and reporting.
//
// [ JDG April 28, 2008 ] For BoostCon 2008
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 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
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Define this to enable debugging
#define BOOST_SPIRIT_QI_DEBUG
///////////////////////////////////////////////////////////////////////////////
// Uncomment this if you want to enable debugging
//#define BOOST_SPIRIT_QI_DEBUG
///////////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include <boost/spirit/include/qi.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct nil {};
struct signed_;
struct program;
typedef boost::variant<
nil
, unsigned int
, boost::recursive_wrapper<signed_>
, boost::recursive_wrapper<program>
>
operand;
struct signed_
{
char sign;
operand operand_;
};
struct operation
{
char operator_;
operand operand_;
};
struct program
{
operand first;
std::list<operation> rest;
};
// print function for debugging
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; 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::program,
(client::ast::operand, first)
(std::list<client::ast::operation>, rest)
)
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST Printer
///////////////////////////////////////////////////////////////////////////
struct printer
{
typedef void result_type;
void operator()(nil) const {}
void operator()(unsigned int n) const { std::cout << n; }
void operator()(operation const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': std::cout << " add"; break;
case '-': std::cout << " subt"; break;
case '*': std::cout << " mult"; break;
case '/': std::cout << " div"; break;
}
}
void operator()(signed_ const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': std::cout << " neg"; break;
case '+': std::cout << " pos"; break;
}
}
void operator()(program const& x) const
{
boost::apply_visitor(*this, x.first);
BOOST_FOREACH(operation const& oper, x.rest)
{
std::cout << ' ';
(*this)(oper);
}
}
};
///////////////////////////////////////////////////////////////////////////
// The AST evaluator
///////////////////////////////////////////////////////////////////////////
struct eval
{
typedef int result_type;
int operator()(nil) const { BOOST_ASSERT(0); return 0; }
int operator()(unsigned int n) const { return n; }
int operator()(operation const& x, int lhs) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': return lhs + rhs;
case '-': return lhs - rhs;
case '*': return lhs * rhs;
case '/': return lhs / rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(signed_ const& x) const
{
int rhs = boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': return -rhs;
case '+': return +rhs;
}
BOOST_ASSERT(0);
return 0;
}
int operator()(program const& x) const
{
int state = boost::apply_visitor(*this, x.first);
BOOST_FOREACH(operation const& oper, x.rest)
{
state = (*this)(oper, state);
}
return state;
}
};
}}
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
using boost::phoenix::function;
///////////////////////////////////////////////////////////////////////////////
// Our error handler
///////////////////////////////////////////////////////////////////////////////
struct error_handler_
{
template <typename, typename, typename>
struct result { typedef void type; };
template <typename Iterator>
void operator()(
qi::info const& what
, Iterator err_pos, Iterator last) const
{
std::cout
<< "Error! Expecting "
<< what // what failed?
<< " here: \""
<< std::string(err_pos, last) // iterators to error-pos, end
<< "\""
<< std::endl
;
}
};
function<error_handler_> const error_handler = error_handler_();
///////////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ast::program(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::char_type char_;
qi::uint_type uint_;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
using qi::on_error;
using qi::fail;
expression =
term
>> *( (char_('+') > term)
| (char_('-') > term)
)
;
term =
factor
>> *( (char_('*') > factor)
| (char_('/') > factor)
)
;
factor =
uint_
| '(' > expression > ')'
| (char_('-') > factor)
| (char_('+') > factor)
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(term);
BOOST_SPIRIT_DEBUG_NODE(factor);
// Error handling
on_error<fail>(expression, error_handler(_4, _3, _2));
}
qi::rule<Iterator, ast::program(), ascii::space_type> expression;
qi::rule<Iterator, ast::program(), ascii::space_type> term;
qi::rule<Iterator, ast::operand(), ascii::space_type> factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
typedef client::ast::program ast_program;
typedef client::ast::printer ast_print;
typedef client::ast::eval ast_eval;
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
calculator calc; // Our grammar
ast_program program; // Our program (AST)
ast_print print; // Prints the program
ast_eval eval; // Evaluates the program
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
boost::spirit::ascii::space_type space;
bool r = phrase_parse(iter, end, calc, space, program);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
print(program);
std::cout << "\nResult: " << eval(program) << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}

View File

@@ -0,0 +1,372 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Yet another calculator example! This time, we will compile to a simple
// virtual machine. This is actually one of the very first Spirit example
// circa 2000. Now, it's ported to Spirit2.
//
// [ JDG Sometime 2000 ] pre-boost
// [ JDG September 18, 2002 ] spirit1
// [ JDG April 8, 2007 ] spirit2
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 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
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Define this to enable debugging
//#define BOOST_SPIRIT_QI_DEBUG
///////////////////////////////////////////////////////////////////////////////
// Uncomment this if you want to enable debugging
//#define BOOST_SPIRIT_QI_DEBUG
///////////////////////////////////////////////////////////////////////////////
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include <boost/spirit/include/qi.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
namespace client { namespace ast
{
///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
struct nil {};
struct signed_;
struct expression;
typedef boost::variant<
nil
, unsigned int
, 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;
};
// print function for debugging
inline std::ostream& operator<<(std::ostream& out, nil) { out << "nil"; 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)
)
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_int, // push constant integer into the stack
};
class vmachine
{
public:
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
, stack_ptr(stack.begin())
{
}
int top() const { return stack_ptr[-1]; };
void execute(std::vector<int> const& code);
private:
std::vector<int> stack;
std::vector<int>::iterator stack_ptr;
};
void vmachine::execute(std::vector<int> const& code)
{
std::vector<int>::const_iterator pc = code.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_int:
*stack_ptr++ = *pc++;
break;
}
}
}
///////////////////////////////////////////////////////////////////////////
// The Compiler
///////////////////////////////////////////////////////////////////////////
struct compiler
{
typedef void result_type;
std::vector<int>& code;
compiler(std::vector<int>& code)
: code(code) {}
void operator()(ast::nil) const { BOOST_ASSERT(0); }
void operator()(unsigned int n) const
{
code.push_back(op_int);
code.push_back(n);
}
void operator()(ast::operation const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.operator_)
{
case '+': code.push_back(op_add); break;
case '-': code.push_back(op_sub); break;
case '*': code.push_back(op_mul); break;
case '/': code.push_back(op_div); break;
default: BOOST_ASSERT(0); break;
}
}
void operator()(ast::signed_ const& x) const
{
boost::apply_visitor(*this, x.operand_);
switch (x.sign)
{
case '-': code.push_back(op_neg); break;
case '+': break;
default: BOOST_ASSERT(0); break;
}
}
void operator()(ast::expression const& x) const
{
boost::apply_visitor(*this, x.first);
BOOST_FOREACH(ast::operation const& oper, x.rest)
{
(*this)(oper);
}
}
};
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
using boost::phoenix::function;
///////////////////////////////////////////////////////////////////////////////
// The error handler
///////////////////////////////////////////////////////////////////////////////
struct error_handler_
{
template <typename, typename, typename>
struct result { typedef void type; };
template <typename Iterator>
void operator()(
qi::info const& what
, Iterator err_pos, Iterator last) const
{
std::cout
<< "Error! Expecting "
<< what // what failed?
<< " here: \""
<< std::string(err_pos, last) // iterators to error-pos, end
<< "\""
<< std::endl
;
}
};
function<error_handler_> const error_handler = error_handler_();
///////////////////////////////////////////////////////////////////////////////
// The calculator grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, ast::expression(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
qi::char_type char_;
qi::uint_type uint_;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
using qi::on_error;
using qi::fail;
expression =
term
>> *( (char_('+') > term)
| (char_('-') > term)
)
;
term =
factor
>> *( (char_('*') > factor)
| (char_('/') > factor)
)
;
factor =
uint_
| '(' > expression > ')'
| (char_('-') > factor)
| (char_('+') > factor)
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expression)(term)(factor));
// Error handling
on_error<fail>(expression, error_handler(_4, _3, _2));
}
qi::rule<Iterator, ast::expression(), ascii::space_type> expression;
qi::rule<Iterator, ast::expression(), ascii::space_type> term;
qi::rule<Iterator, ast::operand(), ascii::space_type> factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
typedef client::ast::expression ast_expression;
typedef client::compiler compiler;
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
client::vmachine mach; // Our virtual machine
std::vector<int> code; // Our VM code
calculator calc; // Our grammar
ast_expression expression; // Our program (AST)
compiler compile(code); // Compiles the program
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
boost::spirit::ascii::space_type space;
bool r = phrase_parse(iter, end, calc, space, expression);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
compile(expression);
mach.execute(code);
std::cout << "\nResult: " << mach.top() << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}

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,116 @@
/*=============================================================================
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/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/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/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/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

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_CALC8_ANNOTATION_HPP)
#define BOOST_SPIRIT_CALC8_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,171 @@
/*=============================================================================
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_CALC8_AST_HPP)
#define BOOST_SPIRIT_CALC8_AST_HPP
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/optional.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 unary;
struct expression;
struct variable : tagged
{
variable(std::string const& name = "") : name(name) {}
std::string name;
};
typedef boost::variant<
nil
, bool
, unsigned int
, variable
, boost::recursive_wrapper<unary>
, boost::recursive_wrapper<expression>
>
operand;
enum optoken
{
op_plus,
op_minus,
op_times,
op_divide,
op_positive,
op_negative,
op_not,
op_equal,
op_not_equal,
op_less,
op_less_equal,
op_greater,
op_greater_equal,
op_and,
op_or
};
struct unary
{
optoken operator_;
operand operand_;
};
struct operation
{
optoken operator_;
operand operand_;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct assignment
{
variable lhs;
expression rhs;
};
struct variable_declaration
{
assignment assign;
};
struct if_statement;
struct while_statement;
struct statement_list;
typedef boost::variant<
variable_declaration
, assignment
, boost::recursive_wrapper<if_statement>
, boost::recursive_wrapper<while_statement>
, boost::recursive_wrapper<statement_list>
>
statement;
struct statement_list : std::list<statement> {};
struct if_statement
{
expression condition;
statement then;
boost::optional<statement> else_;
};
struct while_statement
{
expression condition;
statement body;
};
// 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::unary,
(client::ast::optoken, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(client::ast::optoken, 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)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::if_statement,
(client::ast::expression, condition)
(client::ast::statement, then)
(boost::optional<client::ast::statement>, else_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::while_statement,
(client::ast::expression, condition)
(client::ast::statement, body)
)
#endif

View File

@@ -0,0 +1,382 @@
/*=============================================================================
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>
#include <boost/lexical_cast.hpp>
#include <set>
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;
}
std::map<std::size_t, std::string> lines;
std::set<std::size_t> jumps;
while (pc != code.end())
{
std::string line;
std::size_t address = pc - code.begin();
switch (*pc++)
{
case op_neg:
line += " op_neg";
break;
case op_not:
line += " op_not";
break;
case op_add:
line += " op_add";
break;
case op_sub:
line += " op_sub";
break;
case op_mul:
line += " op_mul";
break;
case op_div:
line += " op_div";
break;
case op_eq:
line += " op_eq";
break;
case op_neq:
line += " op_neq";
break;
case op_lt:
line += " op_lt";
break;
case op_lte:
line += " op_lte";
break;
case op_gt:
line += " op_gt";
break;
case op_gte:
line += " op_gte";
break;
case op_and:
line += " op_and";
break;
case op_or:
line += " op_or";
break;
case op_load:
line += " op_load ";
line += locals[*pc++];
break;
case op_store:
line += " op_store ";
line += locals[*pc++];
break;
case op_int:
line += " op_int ";
line += boost::lexical_cast<std::string>(*pc++);
break;
case op_true:
line += " op_true";
break;
case op_false:
line += " op_false";
break;
case op_jump:
{
line += " op_jump ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_jump_if:
{
line += " op_jump_if ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_stk_adj:
line += " op_stk_adj ";
line += boost::lexical_cast<std::string>(*pc++);
break;
}
lines[address] = line;
}
std::cout << "start:" << std::endl;
typedef std::pair<std::size_t, std::string> line_info;
BOOST_FOREACH(line_info const& l, lines)
{
std::size_t pos = l.first;
if (jumps.find(pos) != jumps.end())
std::cout << pos << ':' << std::endl;
std::cout << l.second << std::endl;
}
std::cout << "end:" << std::endl;
}
bool compiler::operator()(unsigned int x) const
{
program.op(op_int, x);
return true;
}
bool compiler::operator()(bool x) const
{
program.op(x ? op_true : op_false);
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 ast::op_plus: program.op(op_add); break;
case ast::op_minus: program.op(op_sub); break;
case ast::op_times: program.op(op_mul); break;
case ast::op_divide: program.op(op_div); break;
case ast::op_equal: program.op(op_eq); break;
case ast::op_not_equal: program.op(op_neq); break;
case ast::op_less: program.op(op_lt); break;
case ast::op_less_equal: program.op(op_lte); break;
case ast::op_greater: program.op(op_gt); break;
case ast::op_greater_equal: program.op(op_gte); break;
case ast::op_and: program.op(op_and); break;
case ast::op_or: program.op(op_or); break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::unary const& x) const
{
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.operator_)
{
case ast::op_negative: program.op(op_neg); break;
case ast::op_not: program.op(op_not); break;
case ast::op_positive: 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 const& x) const
{
return boost::apply_visitor(*this, x);
}
bool compiler::operator()(ast::statement_list const& x) const
{
BOOST_FOREACH(ast::statement const& s, x)
{
if (!(*this)(s))
return false;
}
return true;
}
bool compiler::operator()(ast::if_statement const& x) const
{
if (!(*this)(x.condition))
return false;
program.op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t skip = program.size()-1; // mark its position
if (!(*this)(x.then))
return false;
program[skip] = program.size()-skip; // now we know where to jump to (after the if branch)
if (x.else_) // We got an alse
{
program[skip] += 2; // adjust for the "else" jump
program.op(op_jump, 0); // we shall fill this (0) in later
std::size_t exit = program.size()-1; // mark its position
if (!(*this)(*x.else_))
return false;
program[exit] = program.size()-exit; // now we know where to jump to (after the else branch)
}
return true;
}
bool compiler::operator()(ast::while_statement const& x) const
{
std::size_t loop = program.size(); // mark our position
if (!(*this)(x.condition))
return false;
program.op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t exit = program.size()-1; // mark its position
if (!(*this)(x.body))
return false;
program.op(op_jump,
int(loop-1) - int(program.size())); // loop back
program[exit] = program.size()-exit; // now we know where to jump to (to exit the loop)
return true;
}
bool compiler::start(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);
if (!(*this)(x))
{
program.clear();
return false;
}
program[1] = program.nvars(); // now store the actual number of variables
return true;
}
}}

View File

@@ -0,0 +1,92 @@
/*=============================================================================
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_CALC8_COMPILER_HPP)
#define BOOST_SPIRIT_CALC8_COMPILER_HPP
#include "ast.hpp"
#include "error_handler.hpp"
#include <vector>
#include <map>
#include <boost/function.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/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::size_t size() const { return code.size(); }
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()(bool x) const;
bool operator()(ast::variable const& x) const;
bool operator()(ast::operation const& x) const;
bool operator()(ast::unary 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;
bool operator()(ast::statement const& x) const;
bool operator()(ast::if_statement const& x) const;
bool operator()(ast::while_statement const& x) const;
bool start(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_CALC8_ERROR_HANDLER_HPP)
#define BOOST_SPIRIT_CALC8_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,68 @@
/*=============================================================================
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_CALC8_EXPRESSION_HPP)
#define BOOST_SPIRIT_CALC8_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, equality_expr, relational_expr,
logical_expr, additive_expr, multiplicative_expr
;
qi::rule<Iterator, ast::operand(), ascii::space_type>
unary_expr, primary_expr
;
qi::rule<Iterator, std::string(), ascii::space_type>
identifier
;
qi::symbols<char, ast::optoken>
equality_op, relational_op, logical_op,
additive_op, multiplicative_op, unary_op
;
qi::symbols<char>
keywords
;
};
}}
#endif

View File

@@ -0,0 +1,159 @@
/*=============================================================================
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/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;
qi::bool_type bool_;
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;
///////////////////////////////////////////////////////////////////////
// Tokens
logical_op.add
("&&", ast::op_and)
("||", ast::op_or)
;
equality_op.add
("==", ast::op_equal)
("!=", ast::op_not_equal)
;
relational_op.add
("<", ast::op_less)
("<=", ast::op_less_equal)
(">", ast::op_greater)
(">=", ast::op_greater_equal)
;
additive_op.add
("+", ast::op_plus)
("-", ast::op_minus)
;
multiplicative_op.add
("*", ast::op_times)
("/", ast::op_divide)
;
unary_op.add
("+", ast::op_positive)
("-", ast::op_negative)
("!", ast::op_not)
;
keywords.add
("var")
("true")
("false")
("if")
("else")
("while")
;
///////////////////////////////////////////////////////////////////////
// Main expression grammar
expr =
logical_expr.alias()
;
logical_expr =
equality_expr
>> *(logical_op > equality_expr)
;
equality_expr =
relational_expr
>> *(equality_op > relational_expr)
;
relational_expr =
additive_expr
>> *(relational_op > additive_expr)
;
additive_expr =
multiplicative_expr
>> *(additive_op > multiplicative_expr)
;
multiplicative_expr =
unary_expr
>> *(multiplicative_op > unary_expr)
;
unary_expr =
primary_expr
| (unary_op > primary_expr)
;
primary_expr =
uint_
| identifier
| bool_
| '(' > expr > ')'
;
identifier =
!keywords
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
///////////////////////////////////////////////////////////////////////
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(equality_expr)
(relational_expr)
(logical_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,97 @@
/*=============================================================================
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 boolean expressions and control structures.
// Is it obvious now what we are up to? ;-)
//
// [ 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.start(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.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,37 @@
/*=============================================================================
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_CALC8_STATEMENT_HPP)
#define BOOST_SPIRIT_CALC8_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, compound_statement;
qi::rule<Iterator, ast::statement(), ascii::space_type> statement_;
qi::rule<Iterator, ast::variable_declaration(), ascii::space_type> variable_declaration;
qi::rule<Iterator, ast::assignment(), ascii::space_type> assignment;
qi::rule<Iterator, ast::if_statement(), ascii::space_type> if_statement;
qi::rule<Iterator, ast::while_statement(), ascii::space_type> while_statement;
qi::rule<Iterator, std::string(), ascii::space_type> identifier;
};
}}
#endif

View File

@@ -0,0 +1,111 @@
/*=============================================================================
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;
qi::lit_type lit;
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 =
+statement_
;
statement_ =
variable_declaration
| assignment
| compound_statement
| if_statement
| while_statement
;
identifier =
!expr.keywords
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
variable_declaration =
lexeme["var" >> !(alnum | '_')] // make sure we have whole words
> &identifier // expect an identifier
> assignment
;
assignment =
identifier
> '='
> expr
> ';'
;
if_statement =
lit("if")
> '('
> expr
> ')'
> statement_
>
-(
lexeme["else" >> !(alnum | '_')] // make sure we have whole words
> statement_
)
;
while_statement =
lit("while")
> '('
> expr
> ')'
> statement_
;
compound_statement =
'{' >> -statement_list >> '}'
;
// 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,163 @@
/*=============================================================================
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"
#include <boost/assert.hpp>
#if defined(_MSC_VER)
# pragma warning(disable: 4800) // forcing value to bool 'true' or 'false'
// (performance warning)
#endif
namespace client
{
int vmachine::execute(
std::vector<int> const& code
, std::vector<int>::const_iterator pc
, std::vector<int>::iterator frame_ptr
)
{
std::vector<int>::iterator stack_ptr = frame_ptr;
while (pc != code.end())
{
BOOST_ASSERT(pc != code.end());
switch (*pc++)
{
case op_neg:
stack_ptr[-1] = -stack_ptr[-1];
break;
case op_not:
stack_ptr[-1] = !bool(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_eq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] == stack_ptr[0]);
break;
case op_neq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] != stack_ptr[0]);
break;
case op_lt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] < stack_ptr[0]);
break;
case op_lte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] <= stack_ptr[0]);
break;
case op_gt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] > stack_ptr[0]);
break;
case op_gte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] >= stack_ptr[0]);
break;
case op_and:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) && bool(stack_ptr[0]);
break;
case op_or:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) || bool(stack_ptr[0]);
break;
case op_load:
*stack_ptr++ = frame_ptr[*pc++];
break;
case op_store:
--stack_ptr;
frame_ptr[*pc++] = stack_ptr[0];
break;
case op_int:
*stack_ptr++ = *pc++;
break;
case op_true:
*stack_ptr++ = true;
break;
case op_false:
*stack_ptr++ = false;
break;
case op_jump:
pc += *pc;
break;
case op_jump_if:
if (!bool(stack_ptr[-1]))
pc += *pc;
else
++pc;
--stack_ptr;
break;
case op_stk_adj:
stack_ptr = stack.begin() + *pc++;
break;
case op_call:
{
int nargs = *pc++;
int jump = *pc++;
// a function call is a recursive call to execute
int r = execute(
code
, code.begin() + jump
, stack_ptr - nargs
);
// cleanup after return from function
stack_ptr[-nargs] = r; // get return value
stack_ptr -= (nargs - 1); // the stack will now contain
// the return value
}
break;
case op_return:
return stack_ptr[-1];
}
}
return -1;
}
}

View File

@@ -0,0 +1,77 @@
/*=============================================================================
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_CALC8_VM_HPP)
#define BOOST_SPIRIT_CALC8_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_not, // boolean negate the top stack entry
op_eq, // compare the top two stack entries for ==
op_neq, // compare the top two stack entries for !=
op_lt, // compare the top two stack entries for <
op_lte, // compare the top two stack entries for <=
op_gt, // compare the top two stack entries for >
op_gte, // compare the top two stack entries for >=
op_and, // logical and top two stack entries
op_or, // logical or top two stack entries
op_load, // load a variable
op_store, // store a variable
op_int, // push constant integer into the stack
op_true, // push constant 0 into the stack
op_false, // push constant 1 into the stack
op_jump_if, // jump to a relative position in the code if top stack
// evaluates to false
op_jump, // jump to a relative position in the code
op_stk_adj, // adjust the stack (for args and locals)
op_call, // function call
op_return // return from function
};
class vmachine
{
public:
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
{
}
int execute(
std::vector<int> const& code // the program code
, std::vector<int>::const_iterator pc // program counter
, std::vector<int>::iterator frame_ptr // start of arguments and locals
);
int execute(std::vector<int> const& code)
{
return execute(code, code.begin(), stack.begin());
};
std::vector<int> stack;
};
}
#endif

View File

@@ -0,0 +1,95 @@
/*=============================================================================
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_CONJURE_ANNOTATION_HPP)
#define BOOST_SPIRIT_CONJURE_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) {}
void operator()(ast::function_call& x) const
{
x.function_name.id = id;
}
void operator()(ast::identifier& x) const
{
x.id = id;
}
template <typename T>
void operator()(T& x) const
{
// no-op
}
};
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::variable_declaration& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::assignment& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::return_statement& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::identifier& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
};
}
#endif

View File

@@ -0,0 +1,274 @@
/*=============================================================================
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_CONJURE_AST_HPP)
#define BOOST_SPIRIT_CONJURE_AST_HPP
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/optional.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 unary;
struct function_call;
struct expression;
struct identifier : tagged
{
identifier(std::string const& name = "") : name(name) {}
std::string name;
};
typedef boost::variant<
nil
, bool
, unsigned int
, identifier
, boost::recursive_wrapper<unary>
, boost::recursive_wrapper<function_call>
, boost::recursive_wrapper<expression>
>
operand;
enum optoken
{
// precedence 1
op_comma,
// precedence 2
op_assign,
op_plus_assign,
op_minus_assign,
op_times_assign,
op_divide_assign,
op_mod_assign,
op_bit_and_assign,
op_bit_xor_assign,
op_bitor_assign,
op_shift_left_assign,
op_shift_right_assign,
// precedence 3
op_logical_or,
// precedence 4
op_logical_and,
// precedence 5
op_bit_or,
// precedence 6
op_bit_xor,
// precedence 7
op_bit_and,
// precedence 8
op_equal,
op_not_equal,
// precedence 9
op_less,
op_less_equal,
op_greater,
op_greater_equal,
// precedence 10
op_shift_left,
op_shift_right,
// precedence 11
op_plus,
op_minus,
// precedence 12
op_times,
op_divide,
op_mod,
// precedence 13
op_positive,
op_negative,
op_pre_incr,
op_pre_decr,
op_compl,
op_not,
// precedence 14
op_post_incr,
op_post_decr,
};
struct unary
{
optoken operator_;
operand operand_;
};
struct operation
{
optoken operator_;
operand operand_;
};
struct function_call
{
identifier function_name;
std::list<expression> args;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct assignment
{
identifier lhs;
expression rhs;
};
struct variable_declaration
{
identifier lhs;
boost::optional<expression> rhs;
};
struct if_statement;
struct while_statement;
struct statement_list;
struct return_statement;
typedef boost::variant<
variable_declaration
, assignment
, boost::recursive_wrapper<if_statement>
, boost::recursive_wrapper<while_statement>
, boost::recursive_wrapper<return_statement>
, boost::recursive_wrapper<statement_list>
>
statement;
struct statement_list : std::list<statement> {};
struct if_statement
{
expression condition;
statement then;
boost::optional<statement> else_;
};
struct while_statement
{
expression condition;
statement body;
};
struct return_statement : tagged
{
boost::optional<expression> expr;
};
struct function
{
std::string return_type;
identifier function_name;
std::list<identifier> args;
statement_list body;
};
typedef std::list<function> function_list;
// print functions for debugging
inline std::ostream& operator<<(std::ostream& out, nil)
{
out << "nil"; return out;
}
inline std::ostream& operator<<(std::ostream& out, identifier const& id)
{
out << id.name; return out;
}
}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast::unary,
(client::ast::optoken, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(client::ast::optoken, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function_call,
(client::ast::identifier, function_name)
(std::list<client::ast::expression>, args)
)
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::identifier, lhs)
(boost::optional<client::ast::expression>, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::assignment,
(client::ast::identifier, lhs)
(client::ast::expression, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::if_statement,
(client::ast::expression, condition)
(client::ast::statement, then)
(boost::optional<client::ast::statement>, else_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::while_statement,
(client::ast::expression, condition)
(client::ast::statement, body)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::return_statement,
(boost::optional<client::ast::expression>, expr)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function,
(std::string, return_type)
(client::ast::identifier, function_name)
(std::list<client::ast::identifier>, args)
(client::ast::statement_list, body)
)
#endif

View File

@@ -0,0 +1,628 @@
/*=============================================================================
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>
#include <boost/lexical_cast.hpp>
#include <set>
namespace client { namespace code_gen
{
void function::op(int a)
{
code.push_back(a);
size_ += 1;
}
void function::op(int a, int b)
{
code.push_back(a);
code.push_back(b);
size_ += 2;
}
void function::op(int a, int b, int c)
{
code.push_back(a);
code.push_back(b);
code.push_back(c);
size_ += 3;
}
int const* function::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 function::add_var(std::string const& name)
{
std::size_t n = variables.size();
variables[name] = n;
}
void function::link_to(std::string const& name, std::size_t address)
{
function_calls[address] = name;
}
void function::print_assembler() const
{
std::vector<int>::const_iterator pc = code.begin() + address;
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;
}
std::map<std::size_t, std::string> lines;
std::set<std::size_t> jumps;
while (pc != (code.begin() + address + size_))
{
std::string line;
std::size_t address = pc - code.begin();
switch (*pc++)
{
case op_neg:
line += " op_neg";
break;
case op_not:
line += " op_not";
break;
case op_add:
line += " op_add";
break;
case op_sub:
line += " op_sub";
break;
case op_mul:
line += " op_mul";
break;
case op_div:
line += " op_div";
break;
case op_eq:
line += " op_eq";
break;
case op_neq:
line += " op_neq";
break;
case op_lt:
line += " op_lt";
break;
case op_lte:
line += " op_lte";
break;
case op_gt:
line += " op_gt";
break;
case op_gte:
line += " op_gte";
break;
case op_and:
line += " op_and";
break;
case op_or:
line += " op_or";
break;
case op_load:
line += " op_load ";
line += locals[*pc++];
break;
case op_store:
line += " op_store ";
line += locals[*pc++];
break;
case op_int:
line += " op_int ";
line += boost::lexical_cast<std::string>(*pc++);
break;
case op_true:
line += " op_true";
break;
case op_false:
line += " op_false";
break;
case op_jump:
{
line += " op_jump ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_jump_if:
{
line += " op_jump_if ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_call:
{
line += " op_call ";
int nargs = *pc++;
std::size_t jump = *pc++;
line += boost::lexical_cast<std::string>(nargs) + ", ";
BOOST_ASSERT(function_calls.find(jump) != function_calls.end());
line += function_calls.find(jump)->second;
}
break;
case op_stk_adj:
line += " op_stk_adj ";
line += boost::lexical_cast<std::string>(*pc++);
break;
case op_return:
line += " op_return";
break;
}
lines[address] = line;
}
std::cout << "start:" << std::endl;
typedef std::pair<std::size_t, std::string> line_info;
BOOST_FOREACH(line_info const& l, lines)
{
std::size_t pos = l.first;
if (jumps.find(pos) != jumps.end())
std::cout << pos << ':' << std::endl;
std::cout << l.second << std::endl;
}
std::cout << "end:" << std::endl << std::endl;
}
bool compiler::operator()(unsigned int x)
{
BOOST_ASSERT(current != 0);
current->op(op_int, x);
return true;
}
bool compiler::operator()(bool x)
{
BOOST_ASSERT(current != 0);
current->op(x ? op_true : op_false);
return true;
}
bool compiler::operator()(ast::identifier const& x)
{
BOOST_ASSERT(current != 0);
int const* p = current->find_var(x.name);
if (p == 0)
{
error_handler(x.id, "Undeclared variable: " + x.name);
return false;
}
current->op(op_load, *p);
return true;
}
bool compiler::operator()(ast::optoken const& x)
{
BOOST_ASSERT(current != 0);
switch (x)
{
case ast::op_plus: current->op(op_add); break;
case ast::op_minus: current->op(op_sub); break;
case ast::op_times: current->op(op_mul); break;
case ast::op_divide: current->op(op_div); break;
case ast::op_equal: current->op(op_eq); break;
case ast::op_not_equal: current->op(op_neq); break;
case ast::op_less: current->op(op_lt); break;
case ast::op_less_equal: current->op(op_lte); break;
case ast::op_greater: current->op(op_gt); break;
case ast::op_greater_equal: current->op(op_gte); break;
case ast::op_logical_or: current->op(op_or); break;
case ast::op_logical_and: current->op(op_and); break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::unary const& x)
{
BOOST_ASSERT(current != 0);
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.operator_)
{
case ast::op_negative: current->op(op_neg); break;
case ast::op_not: current->op(op_not); break;
case ast::op_positive: break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::function_call const& x)
{
BOOST_ASSERT(current != 0);
if (functions.find(x.function_name.name) == functions.end())
{
error_handler(x.function_name.id, "Function not found: " + x.function_name.name);
return false;
}
boost::shared_ptr<code_gen::function> p = functions[x.function_name.name];
if (p->nargs() != x.args.size())
{
error_handler(x.function_name.id, "Wrong number of arguments: " + x.function_name.name);
return false;
}
BOOST_FOREACH(ast::expression const& expr, x.args)
{
if (!(*this)(expr))
return false;
}
current->op(
op_call,
p->nargs(),
p->get_address());
current->link_to(x.function_name.name, p->get_address());
return true;
}
namespace
{
int precedence[] = {
// precedence 1
1, // op_comma
// precedence 2
2, // op_assign
2, // op_plus_assign
2, // op_minus_assign
2, // op_times_assign
2, // op_divide_assign
2, // op_mod_assign
2, // op_bit_and_assign
2, // op_bit_xor_assign
2, // op_bitor_assign
2, // op_shift_left_assign
2, // op_shift_right_assign
// precedence 3
3, // op_logical_or
// precedence 4
4, // op_logical_and
// precedence 5
5, // op_bit_or
// precedence 6
6, // op_bit_xor
// precedence 7
7, // op_bit_and
// precedence 8
8, // op_equal
8, // op_not_equal
// precedence 9
9, // op_less
9, // op_less_equal
9, // op_greater
9, // op_greater_equal
// precedence 10
10, // op_shift_left
10, // op_shift_right
// precedence 11
11, // op_plus
11, // op_minus
// precedence 12
12, // op_times
12, // op_divide
12, // op_mod
// precedence 13
13, // op_positive
13, // op_negative
13, // op_pre_incr
13, // op_pre_decr
13, // op_compl
13, // op_not
// precedence 14
14, // op_post_incr
14 // op_post_decr
};
}
// The Shunting-yard algorithm
bool compiler::compile_expression(
int min_precedence,
std::list<ast::operation>::const_iterator& rbegin,
std::list<ast::operation>::const_iterator rend)
{
while ((rbegin != rend) && (precedence[rbegin->operator_] >= min_precedence))
{
ast::optoken op = rbegin->operator_;
if (!boost::apply_visitor(*this, rbegin->operand_))
return false;
++rbegin;
while ((rbegin != rend) && (precedence[rbegin->operator_] > precedence[op]))
{
ast::optoken next_op = rbegin->operator_;
compile_expression(precedence[next_op], rbegin, rend);
}
(*this)(op);
}
return true;
}
bool compiler::operator()(ast::expression const& x)
{
BOOST_ASSERT(current != 0);
if (!boost::apply_visitor(*this, x.first))
return false;
std::list<ast::operation>::const_iterator rbegin = x.rest.begin();
if (!compile_expression(0, rbegin, x.rest.end()))
return false;
return true;
}
bool compiler::operator()(ast::assignment const& x)
{
BOOST_ASSERT(current != 0);
if (!(*this)(x.rhs))
return false;
int const* p = current->find_var(x.lhs.name);
if (p == 0)
{
error_handler(x.lhs.id, "Undeclared variable: " + x.lhs.name);
return false;
}
current->op(op_store, *p);
return true;
}
bool compiler::operator()(ast::variable_declaration const& x)
{
BOOST_ASSERT(current != 0);
int const* p = current->find_var(x.lhs.name);
if (p != 0)
{
error_handler(x.lhs.id, "Duplicate variable: " + x.lhs.name);
return false;
}
if (x.rhs) // if there's an RHS initializer
{
bool r = (*this)(*x.rhs);
if (r) // don't add the variable if the RHS fails
{
current->add_var(x.lhs.name);
current->op(op_store, *current->find_var(x.lhs.name));
}
return r;
}
else
{
current->add_var(x.lhs.name);
}
return true;
}
bool compiler::operator()(ast::statement const& x)
{
BOOST_ASSERT(current != 0);
return boost::apply_visitor(*this, x);
}
bool compiler::operator()(ast::statement_list const& x)
{
BOOST_ASSERT(current != 0);
BOOST_FOREACH(ast::statement const& s, x)
{
if (!(*this)(s))
return false;
}
return true;
}
bool compiler::operator()(ast::if_statement const& x)
{
BOOST_ASSERT(current != 0);
if (!(*this)(x.condition))
return false;
current->op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t skip = current->size()-1; // mark its position
if (!(*this)(x.then))
return false;
(*current)[skip] = current->size()-skip; // now we know where to jump to (after the if branch)
if (x.else_) // We got an alse
{
(*current)[skip] += 2; // adjust for the "else" jump
current->op(op_jump, 0); // we shall fill this (0) in later
std::size_t exit = current->size()-1; // mark its position
if (!(*this)(*x.else_))
return false;
(*current)[exit] = current->size()-exit;// now we know where to jump to (after the else branch)
}
return true;
}
bool compiler::operator()(ast::while_statement const& x)
{
BOOST_ASSERT(current != 0);
std::size_t loop = current->size(); // mark our position
if (!(*this)(x.condition))
return false;
current->op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t exit = current->size()-1; // mark its position
if (!(*this)(x.body))
return false;
current->op(op_jump,
int(loop-1) - int(current->size())); // loop back
(*current)[exit] = current->size()-exit; // now we know where to jump to (to exit the loop)
return true;
}
bool compiler::operator()(ast::return_statement const& x)
{
if (void_return)
{
if (x.expr)
{
error_handler(x.id, "'void' function returning a value: ");
return false;
}
}
else
{
if (!x.expr)
{
error_handler(x.id, current_function_name + " function must return a value: ");
return false;
}
}
if (x.expr)
{
if (!(*this)(*x.expr))
return false;
}
current->op(op_return);
return true;
}
bool compiler::operator()(ast::function const& x)
{
void_return = x.return_type == "void";
if (functions.find(x.function_name.name) != functions.end())
{
error_handler(x.function_name.id, "Duplicate function: " + x.function_name.name);
return false;
}
boost::shared_ptr<code_gen::function>& p = functions[x.function_name.name];
p.reset(new code_gen::function(code, x.args.size()));
current = p.get();
current_function_name = x.function_name.name;
// op_stk_adj 0 for now. we'll know how many variables
// we'll have later and add them
current->op(op_stk_adj, 0);
BOOST_FOREACH(ast::identifier const& arg, x.args)
{
current->add_var(arg.name);
}
if (!(*this)(x.body))
return false;
(*current)[1] = current->nvars(); // now store the actual number of variables
// this includes the arguments
return true;
}
bool compiler::operator()(ast::function_list const& x)
{
// Jump to the main function
code.push_back(op_jump);
code.push_back(0); // we will fill this in later when we finish compiling
// and we know where the main function is
BOOST_FOREACH(ast::function const& f, x)
{
if (!(*this)(f))
{
code.clear();
return false;
}
}
// find the main function
boost::shared_ptr<code_gen::function> p =
find_function("main");
if (!p) // main function not found
{
std::cerr << "Error: main function not defined" << std::endl;
return false;
}
code[1] = p->get_address()-1; // jump to this (main function) address
return true;
}
void compiler::print_assembler() const
{
typedef std::pair<std::string, boost::shared_ptr<code_gen::function> > pair;
BOOST_FOREACH(pair const& p, functions)
{
std::cout << ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" << std::endl;
std::cout << p.second->get_address() << ": function " << p.first << std::endl;
p.second->print_assembler();
}
}
boost::shared_ptr<code_gen::function>
compiler::find_function(std::string const& name) const
{
function_table::const_iterator i = functions.find(name);
if (i == functions.end())
return boost::shared_ptr<code_gen::function>();
else
return i->second;
}
}}

View File

@@ -0,0 +1,123 @@
/*=============================================================================
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_CONJURE_COMPILER_HPP)
#define BOOST_SPIRIT_CONJURE_COMPILER_HPP
#include "ast.hpp"
#include "error_handler.hpp"
#include <vector>
#include <map>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
namespace client { namespace code_gen
{
///////////////////////////////////////////////////////////////////////////
// The Function
///////////////////////////////////////////////////////////////////////////
struct function
{
function(std::vector<int>& code, int nargs)
: code(code), address(code.size()), size_(0), nargs_(nargs) {}
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[address+i]; }
int const& operator[](std::size_t i) const { return code[address+i]; }
std::size_t size() const { return size_; }
std::size_t get_address() const { return address; }
int nargs() const { return nargs_; }
int nvars() const { return variables.size(); }
int const* find_var(std::string const& name) const;
void add_var(std::string const& name);
void link_to(std::string const& name, std::size_t address);
void print_assembler() const;
private:
std::map<std::string, int> variables;
std::map<std::size_t, std::string> function_calls;
std::vector<int>& code;
std::size_t address;
std::size_t size_;
std::size_t nargs_;
};
///////////////////////////////////////////////////////////////////////////
// The Compiler
///////////////////////////////////////////////////////////////////////////
struct compiler
{
typedef bool result_type;
template <typename ErrorHandler>
compiler(ErrorHandler& error_handler_)
: current(0)
{
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) { BOOST_ASSERT(0); return false; }
bool operator()(unsigned int x);
bool operator()(bool x);
bool operator()(ast::identifier const& x);
bool operator()(ast::optoken const& x);
bool operator()(ast::unary const& x);
bool operator()(ast::function_call const& x);
bool operator()(ast::expression const& x);
bool operator()(ast::assignment const& x);
bool operator()(ast::variable_declaration const& x);
bool operator()(ast::statement_list const& x);
bool operator()(ast::statement const& x);
bool operator()(ast::if_statement const& x);
bool operator()(ast::while_statement const& x);
bool operator()(ast::return_statement const& x);
bool operator()(ast::function const& x);
bool operator()(ast::function_list const& x);
void print_assembler() const;
boost::shared_ptr<code_gen::function>
find_function(std::string const& name) const;
std::vector<int>& get_code() { return code; }
std::vector<int> const& get_code() const { return code; }
private:
bool compile_expression(
int min_precedence,
std::list<ast::operation>::const_iterator& rbegin,
std::list<ast::operation>::const_iterator rend);
typedef std::map<std::string, boost::shared_ptr<code_gen::function> > function_table;
std::vector<int> code;
code_gen::function* current;
std::string current_function_name;
function_table functions;
bool void_return;
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_CONJURE_ERROR_HANDLER_HPP)
#define BOOST_SPIRIT_CONJURE_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,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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_EXPRESSION_HPP)
#define BOOST_SPIRIT_CONJURE_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 "skipper.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(), skipper<Iterator> >
{
expression(error_handler<Iterator>& error_handler);
qi::rule<Iterator, ast::expression(), skipper<Iterator> >
expr
;
qi::rule<Iterator, ast::operand(), skipper<Iterator> >
unary_expr, primary_expr
;
qi::rule<Iterator, ast::function_call(), skipper<Iterator> >
function_call
;
qi::rule<Iterator, std::list<ast::expression>(), skipper<Iterator> >
argument_list
;
qi::rule<Iterator, std::string(), skipper<Iterator> >
identifier
;
qi::symbols<char, ast::optoken>
unary_op, binary_op
;
qi::symbols<char>
keywords
;
};
}}
#endif

View File

@@ -0,0 +1,131 @@
/*=============================================================================
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/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;
qi::bool_type bool_;
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;
///////////////////////////////////////////////////////////////////////
// Tokens
binary_op.add
("||", ast::op_logical_or)
("&&", ast::op_logical_and)
("==", ast::op_equal)
("!=", ast::op_not_equal)
("<", ast::op_less)
("<=", ast::op_less_equal)
(">", ast::op_greater)
(">=", ast::op_greater_equal)
("+", ast::op_plus)
("-", ast::op_minus)
("*", ast::op_times)
("/", ast::op_divide)
;
unary_op.add
("+", ast::op_positive)
("-", ast::op_negative)
("!", ast::op_not)
;
keywords.add
("true")
("false")
("if")
("else")
("while")
("int")
("void")
("return")
;
///////////////////////////////////////////////////////////////////////
// Main expression grammar
expr =
unary_expr
>> *(binary_op > unary_expr)
;
unary_expr =
primary_expr
| (unary_op > unary_expr)
;
primary_expr =
uint_
| function_call
| identifier
| bool_
| '(' > expr > ')'
;
function_call =
(identifier >> '(')
> argument_list
> ')'
;
argument_list = -(expr % ',');
identifier =
!lexeme[keywords >> !(alnum | '_')]
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
///////////////////////////////////////////////////////////////////////
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(unary_expr)
(primary_expr)
(function_call)
(argument_list)
(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,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 "function_def.hpp"
typedef std::string::const_iterator iterator_type;
template struct client::parser::function<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_CONJURE_FUNCTION_HPP)
#define BOOST_SPIRIT_CONJURE_FUNCTION_HPP
#include "statement.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The function grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct function : qi::grammar<Iterator, ast::function(), skipper<Iterator> >
{
function(error_handler<Iterator>& error_handler);
statement<Iterator> body;
qi::rule<Iterator, std::string(), skipper<Iterator> > name;
qi::rule<Iterator, ast::identifier(), skipper<Iterator> > identifier;
qi::rule<Iterator, std::list<ast::identifier>(), skipper<Iterator> > argument_list;
qi::rule<Iterator, ast::function(), skipper<Iterator> > start;
};
}}
#endif

View File

@@ -0,0 +1,71 @@
/*=============================================================================
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 "function.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
namespace client { namespace parser
{
template <typename Iterator>
function<Iterator>::function(error_handler<Iterator>& error_handler)
: function::base_type(start), body(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;
qi::string_type string;
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;
name =
!body.expr.keywords
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
identifier = name;
argument_list = -(identifier % ',');
start =
lexeme[(string("void") | string("int"))
>> !(alnum | '_')] // make sure we have whole words
> identifier
> '(' > argument_list > ')'
> '{' > body > '}'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(identifier)
(argument_list)
(start)
);
// Error handling: on error in start, call error_handler.
on_error<fail>(start,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
// Annotation: on success in start, call annotation.
on_success(identifier,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,121 @@
/*=============================================================================
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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Not a calculator anymore, right? :-)
//
// [ JDG April 10, 2007 ] spirit2
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
//
///////////////////////////////////////////////////////////////////////////////
#include "function.hpp"
#include "skipper.hpp"
#include "vm.hpp"
#include "compiler.hpp"
#include <boost/lexical_cast.hpp>
#include <fstream>
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
char const* filename;
if (argc > 1)
{
filename = argv[1];
}
else
{
std::cerr << "Error: No input file provided." << std::endl;
return 1;
}
std::ifstream in(filename, std::ios_base::in);
if (!in)
{
std::cerr << "Error: Could not open input file: "
<< filename << std::endl;
return 1;
}
std::string source_code; // We will read the contents here.
in.unsetf(std::ios::skipws); // No white space skipping!
std::copy(
std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::back_inserter(source_code));
typedef std::string::const_iterator iterator_type;
iterator_type iter = source_code.begin();
iterator_type end = source_code.end();
client::vmachine vm; // Our virtual machine
client::ast::function_list ast; // Our AST
client::error_handler<iterator_type>
error_handler(iter, end); // Our error handler
client::parser::function<iterator_type>
function(error_handler); // Our parser
client::parser::skipper<iterator_type>
skipper; // Our skipper
client::code_gen::compiler
compiler(error_handler); // Our compiler
bool success = phrase_parse(iter, end, +function, skipper, ast);
std::cout << "-------------------------\n";
if (success && iter == end)
{
if (compiler(ast))
{
boost::shared_ptr<client::code_gen::function>
p = compiler.find_function("main");
if (!p)
return 1;
int nargs = argc-2;
if (p->nargs() != nargs)
{
std::cerr << "Error: main function requires " << p->nargs() << " arguments." << std::endl;
std::cerr << nargs << " supplied." << std::endl;
return 1;
}
std::cout << "Success\n";
std::cout << "-------------------------\n";
std::cout << "Assembler----------------\n\n";
compiler.print_assembler();
// Push the arguments into our stack
for (int i = 0; i < nargs; ++i)
vm.get_stack()[i] = boost::lexical_cast<int>(argv[i+2]);
// Call the interpreter
int r = vm.execute(compiler.get_code());
std::cout << "-------------------------\n";
std::cout << "Result: " << r << std::endl;
std::cout << "-------------------------\n\n";
}
else
{
std::cout << "Compile failure\n";
}
}
else
{
std::cout << "Parse failure\n";
}
return 0;
}

View File

@@ -0,0 +1,40 @@
/*=============================================================================
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_CONJURE_SKIPPER_HPP)
#define BOOST_SPIRIT_CONJURE_SKIPPER_HPP
#include <boost/spirit/include/qi.hpp>
namespace client { namespace parser
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////////
// The skipper grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct skipper : qi::grammar<Iterator>
{
skipper() : skipper::base_type(start)
{
qi::char_type char_;
ascii::space_type space;
start =
space // tab/space/cr/lf
| "/*" >> *(char_ - "*/") >> "*/" // C-style comments
;
}
qi::rule<Iterator> start;
};
}}
#endif

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,38 @@
/*=============================================================================
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_CONJURE_STATEMENT_HPP)
#define BOOST_SPIRIT_CONJURE_STATEMENT_HPP
#include "expression.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The statement grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct statement : qi::grammar<Iterator, ast::statement_list(), skipper<Iterator> >
{
statement(error_handler<Iterator>& error_handler);
expression<Iterator> expr;
qi::rule<Iterator, ast::statement_list(), skipper<Iterator> >
statement_list, compound_statement;
qi::rule<Iterator, ast::statement(), skipper<Iterator> > statement_;
qi::rule<Iterator, ast::variable_declaration(), skipper<Iterator> > variable_declaration;
qi::rule<Iterator, ast::assignment(), skipper<Iterator> > assignment;
qi::rule<Iterator, ast::if_statement(), skipper<Iterator> > if_statement;
qi::rule<Iterator, ast::while_statement(), skipper<Iterator> > while_statement;
qi::rule<Iterator, ast::return_statement(), skipper<Iterator> > return_statement;
qi::rule<Iterator, std::string(), skipper<Iterator> > identifier;
};
}}
#endif

View File

@@ -0,0 +1,128 @@
/*=============================================================================
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;
qi::lit_type lit;
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 =
+statement_
;
statement_ =
variable_declaration
| assignment
| compound_statement
| if_statement
| while_statement
| return_statement
;
identifier =
!expr.keywords
>> raw[lexeme[(alpha | '_') >> *(alnum | '_')]]
;
variable_declaration =
lexeme["int" >> !(alnum | '_')] // make sure we have whole words
> identifier
> -('=' > expr)
> ';'
;
assignment =
identifier
> '='
> expr
> ';'
;
if_statement =
lit("if")
> '('
> expr
> ')'
> statement_
>
-(
lexeme["else" >> !(alnum | '_')] // make sure we have whole words
> statement_
)
;
while_statement =
lit("while")
> '('
> expr
> ')'
> statement_
;
compound_statement =
'{' >> -statement_list >> '}'
;
return_statement =
lexeme["return" >> !(alnum | '_')] // make sure we have whole words
> -expr
> ';'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(statement_list)
(statement_)
(variable_declaration)
(assignment)
(if_statement)
(while_statement)
(compound_statement)
(return_statement)
);
// 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 variable_declaration,
// assignment and return_statement, call annotation.
on_success(variable_declaration,
annotation_function(error_handler.iters)(_val, _1));
on_success(assignment,
annotation_function(error_handler.iters)(_val, _1));
on_success(return_statement,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,159 @@
/*=============================================================================
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"
#if defined(_MSC_VER)
# pragma warning(disable: 4800) // forcing value to bool 'true' or 'false'
// (performance warning)
#endif
namespace client
{
int vmachine::execute(
std::vector<int> const& code
, std::vector<int>::const_iterator pc
, std::vector<int>::iterator frame_ptr
)
{
std::vector<int>::iterator stack_ptr = frame_ptr;
while (true)
{
switch (*pc++)
{
case op_neg:
stack_ptr[-1] = -stack_ptr[-1];
break;
case op_not:
stack_ptr[-1] = !bool(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_eq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] == stack_ptr[0]);
break;
case op_neq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] != stack_ptr[0]);
break;
case op_lt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] < stack_ptr[0]);
break;
case op_lte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] <= stack_ptr[0]);
break;
case op_gt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] > stack_ptr[0]);
break;
case op_gte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] >= stack_ptr[0]);
break;
case op_and:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) && bool(stack_ptr[0]);
break;
case op_or:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) || bool(stack_ptr[0]);
break;
case op_load:
*stack_ptr++ = frame_ptr[*pc++];
break;
case op_store:
--stack_ptr;
frame_ptr[*pc++] = stack_ptr[0];
break;
case op_int:
*stack_ptr++ = *pc++;
break;
case op_true:
*stack_ptr++ = true;
break;
case op_false:
*stack_ptr++ = false;
break;
case op_jump:
pc += *pc;
break;
case op_jump_if:
if (!bool(stack_ptr[-1]))
pc += *pc;
else
++pc;
--stack_ptr;
break;
case op_stk_adj:
stack_ptr += *pc++;
break;
case op_call:
{
int nargs = *pc++;
int jump = *pc++;
// a function call is a recursive call to execute
int r = execute(
code
, code.begin() + jump
, stack_ptr - nargs
);
// cleanup after return from function
stack_ptr[-nargs] = r; // get return value
stack_ptr -= (nargs - 1); // the stack will now contain
// the return value
}
break;
case op_return:
return stack_ptr[-1];
}
}
}
}

View File

@@ -0,0 +1,82 @@
/*=============================================================================
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_CONJURE_VM_HPP)
#define BOOST_SPIRIT_CONJURE_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_not, // boolean negate the top stack entry
op_eq, // compare the top two stack entries for ==
op_neq, // compare the top two stack entries for !=
op_lt, // compare the top two stack entries for <
op_lte, // compare the top two stack entries for <=
op_gt, // compare the top two stack entries for >
op_gte, // compare the top two stack entries for >=
op_and, // logical and top two stack entries
op_or, // logical or top two stack entries
op_load, // load a variable
op_store, // store a variable
op_int, // push constant integer into the stack
op_true, // push constant 0 into the stack
op_false, // push constant 1 into the stack
op_jump_if, // jump to a relative position in the code if top stack
// evaluates to false
op_jump, // jump to a relative position in the code
op_stk_adj, // adjust the stack (for args and locals)
op_call, // function call
op_return // return from function
};
class vmachine
{
public:
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
{
}
int execute(std::vector<int> const& code)
{
return execute(code, code.begin(), stack.begin());
}
std::vector<int> const& get_stack() const { return stack; };
std::vector<int>& get_stack() { return stack; };
private:
int execute(
std::vector<int> const& code // the program code
, std::vector<int>::const_iterator pc // program counter
, std::vector<int>::iterator frame_ptr // start of arguments and locals
);
std::vector<int> stack;
};
}
#endif

View File

@@ -0,0 +1,95 @@
/*=============================================================================
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_CONJURE_ANNOTATION_HPP)
#define BOOST_SPIRIT_CONJURE_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) {}
void operator()(ast::function_call& x) const
{
x.function_name.id = id;
}
void operator()(ast::identifier& x) const
{
x.id = id;
}
template <typename T>
void operator()(T& x) const
{
// no-op
}
};
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::variable_declaration& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::assignment& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::return_statement& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::identifier& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
};
}
#endif

View File

@@ -0,0 +1,208 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_AST_HPP)
#define BOOST_SPIRIT_CONJURE_AST_HPP
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/optional.hpp>
#include <list>
#include "ids.hpp"
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 unary;
struct function_call;
struct expression;
struct identifier : tagged
{
identifier(std::string const& name = "") : name(name) {}
std::string name;
};
typedef boost::variant<
nil
, bool
, unsigned int
, identifier
, boost::recursive_wrapper<unary>
, boost::recursive_wrapper<function_call>
, boost::recursive_wrapper<expression>
>
operand;
struct unary
{
token_ids::type operator_;
operand operand_;
};
struct operation
{
token_ids::type operator_;
operand operand_;
};
struct function_call
{
identifier function_name;
std::list<expression> args;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct assignment
{
identifier lhs;
expression rhs;
};
struct variable_declaration
{
identifier lhs;
boost::optional<expression> rhs;
};
struct if_statement;
struct while_statement;
struct statement_list;
struct return_statement;
typedef boost::variant<
variable_declaration
, assignment
, boost::recursive_wrapper<if_statement>
, boost::recursive_wrapper<while_statement>
, boost::recursive_wrapper<return_statement>
, boost::recursive_wrapper<statement_list>
>
statement;
struct statement_list : std::list<statement> {};
struct if_statement
{
expression condition;
statement then;
boost::optional<statement> else_;
};
struct while_statement
{
expression condition;
statement body;
};
struct return_statement : tagged
{
boost::optional<expression> expr;
};
struct function
{
std::string return_type;
identifier function_name;
std::list<identifier> args;
statement_list body;
};
typedef std::list<function> function_list;
// print functions for debugging
inline std::ostream& operator<<(std::ostream& out, nil)
{
out << "nil"; return out;
}
inline std::ostream& operator<<(std::ostream& out, identifier const& id)
{
out << id.name; return out;
}
}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast::unary,
(client::token_ids::type, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(client::token_ids::type, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function_call,
(client::ast::identifier, function_name)
(std::list<client::ast::expression>, args)
)
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::identifier, lhs)
(boost::optional<client::ast::expression>, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::assignment,
(client::ast::identifier, lhs)
(client::ast::expression, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::if_statement,
(client::ast::expression, condition)
(client::ast::statement, then)
(boost::optional<client::ast::statement>, else_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::while_statement,
(client::ast::expression, condition)
(client::ast::statement, body)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::return_statement,
(boost::optional<client::ast::expression>, expr)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function,
(std::string, return_type)
(client::ast::identifier, function_name)
(std::list<client::ast::identifier>, args)
(client::ast::statement_list, body)
)
#endif

View File

@@ -0,0 +1,622 @@
/*=============================================================================
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 "config.hpp"
#include "compiler.hpp"
#include "vm.hpp"
#include <boost/foreach.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/assert.hpp>
#include <boost/lexical_cast.hpp>
#include <set>
namespace client { namespace code_gen
{
void function::op(int a)
{
code.push_back(a);
size_ += 1;
}
void function::op(int a, int b)
{
code.push_back(a);
code.push_back(b);
size_ += 2;
}
void function::op(int a, int b, int c)
{
code.push_back(a);
code.push_back(b);
code.push_back(c);
size_ += 3;
}
int const* function::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 function::add_var(std::string const& name)
{
std::size_t n = variables.size();
variables[name] = n;
}
void function::link_to(std::string const& name, std::size_t address)
{
function_calls[address] = name;
}
void function::print_assembler() const
{
std::vector<int>::const_iterator pc = code.begin() + address;
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;
}
std::map<std::size_t, std::string> lines;
std::set<std::size_t> jumps;
while (pc != (code.begin() + address + size_))
{
std::string line;
std::size_t address = pc - code.begin();
switch (*pc++)
{
case op_neg:
line += " op_neg";
break;
case op_not:
line += " op_not";
break;
case op_add:
line += " op_add";
break;
case op_sub:
line += " op_sub";
break;
case op_mul:
line += " op_mul";
break;
case op_div:
line += " op_div";
break;
case op_eq:
line += " op_eq";
break;
case op_neq:
line += " op_neq";
break;
case op_lt:
line += " op_lt";
break;
case op_lte:
line += " op_lte";
break;
case op_gt:
line += " op_gt";
break;
case op_gte:
line += " op_gte";
break;
case op_and:
line += " op_and";
break;
case op_or:
line += " op_or";
break;
case op_load:
line += " op_load ";
line += locals[*pc++];
break;
case op_store:
line += " op_store ";
line += locals[*pc++];
break;
case op_int:
line += " op_int ";
line += boost::lexical_cast<std::string>(*pc++);
break;
case op_true:
line += " op_true";
break;
case op_false:
line += " op_false";
break;
case op_jump:
{
line += " op_jump ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_jump_if:
{
line += " op_jump_if ";
std::size_t pos = (pc - code.begin()) + *pc++;
if (pos == code.size())
line += "end";
else
line += boost::lexical_cast<std::string>(pos);
jumps.insert(pos);
}
break;
case op_call:
{
line += " op_call ";
int nargs = *pc++;
std::size_t jump = *pc++;
line += boost::lexical_cast<std::string>(nargs) + ", ";
BOOST_ASSERT(function_calls.find(jump) != function_calls.end());
line += function_calls.find(jump)->second;
}
break;
case op_stk_adj:
line += " op_stk_adj ";
line += boost::lexical_cast<std::string>(*pc++);
break;
case op_return:
line += " op_return";
break;
}
lines[address] = line;
}
std::cout << "start:" << std::endl;
typedef std::pair<std::size_t, std::string> line_info;
BOOST_FOREACH(line_info const& l, lines)
{
std::size_t pos = l.first;
if (jumps.find(pos) != jumps.end())
std::cout << pos << ':' << std::endl;
std::cout << l.second << std::endl;
}
std::cout << "end:" << std::endl << std::endl;
}
bool compiler::operator()(unsigned int x)
{
BOOST_ASSERT(current != 0);
current->op(op_int, x);
return true;
}
bool compiler::operator()(bool x)
{
BOOST_ASSERT(current != 0);
current->op(x ? op_true : op_false);
return true;
}
bool compiler::operator()(ast::identifier const& x)
{
BOOST_ASSERT(current != 0);
int const* p = current->find_var(x.name);
if (p == 0)
{
error_handler(x.id, "Undeclared variable: " + x.name);
return false;
}
current->op(op_load, *p);
return true;
}
bool compiler::operator()(token_ids::type const& x)
{
BOOST_ASSERT(current != 0);
switch (x)
{
case token_ids::plus: current->op(op_add); break;
case token_ids::minus: current->op(op_sub); break;
case token_ids::times: current->op(op_mul); break;
case token_ids::divide: current->op(op_div); break;
case token_ids::equal: current->op(op_eq); break;
case token_ids::not_equal: current->op(op_neq); break;
case token_ids::less: current->op(op_lt); break;
case token_ids::less_equal: current->op(op_lte); break;
case token_ids::greater: current->op(op_gt); break;
case token_ids::greater_equal: current->op(op_gte); break;
case token_ids::logical_or: current->op(op_or); break;
case token_ids::logical_and: current->op(op_and); break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::unary const& x)
{
BOOST_ASSERT(current != 0);
if (!boost::apply_visitor(*this, x.operand_))
return false;
switch (x.operator_)
{
case token_ids::minus: current->op(op_neg); break;
case token_ids::not_: current->op(op_not); break;
case token_ids::plus: break;
default: BOOST_ASSERT(0); return false;
}
return true;
}
bool compiler::operator()(ast::function_call const& x)
{
BOOST_ASSERT(current != 0);
if (functions.find(x.function_name.name) == functions.end())
{
error_handler(x.function_name.id, "Function not found: " + x.function_name.name);
return false;
}
boost::shared_ptr<code_gen::function> p = functions[x.function_name.name];
if (p->nargs() != x.args.size())
{
error_handler(x.function_name.id, "Wrong number of arguments: " + x.function_name.name);
return false;
}
BOOST_FOREACH(ast::expression const& expr, x.args)
{
if (!(*this)(expr))
return false;
}
current->op(
op_call,
p->nargs(),
p->get_address());
current->link_to(x.function_name.name, p->get_address());
return true;
}
namespace
{
int precedence[] = {
// precedence 1
1, // op_comma
// precedence 2
2, // op_assign
2, // op_plus_assign
2, // op_minus_assign
2, // op_times_assign
2, // op_divide_assign
2, // op_mod_assign
2, // op_bit_and_assign
2, // op_bit_xor_assign
2, // op_bitor_assign
2, // op_shift_left_assign
2, // op_shift_right_assign
// precedence 3
3, // op_logical_or
// precedence 4
4, // op_logical_and
// precedence 5
5, // op_bit_or
// precedence 6
6, // op_bit_xor
// precedence 7
7, // op_bit_and
// precedence 8
8, // op_equal
8, // op_not_equal
// precedence 9
9, // op_less
9, // op_less_equal
9, // op_greater
9, // op_greater_equal
// precedence 10
10, // op_shift_left
10, // op_shift_right
// precedence 11
11, // op_plus
11, // op_minus
// precedence 12
12, // op_times
12, // op_divide
12 // op_mod
};
}
inline int precedence_of(token_ids::type op)
{
return precedence[op & 0xFF];
}
// The Shunting-yard algorithm
bool compiler::compile_expression(
int min_precedence,
std::list<ast::operation>::const_iterator& rbegin,
std::list<ast::operation>::const_iterator rend)
{
while ((rbegin != rend) && (precedence_of(rbegin->operator_) >= min_precedence))
{
token_ids::type op = rbegin->operator_;
if (!boost::apply_visitor(*this, rbegin->operand_))
return false;
++rbegin;
while ((rbegin != rend) && (precedence_of(rbegin->operator_) > precedence_of(op)))
{
token_ids::type next_op = rbegin->operator_;
compile_expression(precedence_of(next_op), rbegin, rend);
}
(*this)(op);
}
return true;
}
bool compiler::operator()(ast::expression const& x)
{
BOOST_ASSERT(current != 0);
if (!boost::apply_visitor(*this, x.first))
return false;
std::list<ast::operation>::const_iterator rbegin = x.rest.begin();
if (!compile_expression(0, rbegin, x.rest.end()))
return false;
return true;
}
bool compiler::operator()(ast::assignment const& x)
{
BOOST_ASSERT(current != 0);
if (!(*this)(x.rhs))
return false;
int const* p = current->find_var(x.lhs.name);
if (p == 0)
{
error_handler(x.lhs.id, "Undeclared variable: " + x.lhs.name);
return false;
}
current->op(op_store, *p);
return true;
}
bool compiler::operator()(ast::variable_declaration const& x)
{
BOOST_ASSERT(current != 0);
int const* p = current->find_var(x.lhs.name);
if (p != 0)
{
error_handler(x.lhs.id, "Duplicate variable: " + x.lhs.name);
return false;
}
if (x.rhs) // if there's an RHS initializer
{
bool r = (*this)(*x.rhs);
if (r) // don't add the variable if the RHS fails
{
current->add_var(x.lhs.name);
current->op(op_store, *current->find_var(x.lhs.name));
}
return r;
}
else
{
current->add_var(x.lhs.name);
}
return true;
}
bool compiler::operator()(ast::statement const& x)
{
BOOST_ASSERT(current != 0);
return boost::apply_visitor(*this, x);
}
bool compiler::operator()(ast::statement_list const& x)
{
BOOST_ASSERT(current != 0);
BOOST_FOREACH(ast::statement const& s, x)
{
if (!(*this)(s))
return false;
}
return true;
}
bool compiler::operator()(ast::if_statement const& x)
{
BOOST_ASSERT(current != 0);
if (!(*this)(x.condition))
return false;
current->op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t skip = current->size()-1; // mark its position
if (!(*this)(x.then))
return false;
(*current)[skip] = current->size()-skip; // now we know where to jump to (after the if branch)
if (x.else_) // We got an alse
{
(*current)[skip] += 2; // adjust for the "else" jump
current->op(op_jump, 0); // we shall fill this (0) in later
std::size_t exit = current->size()-1; // mark its position
if (!(*this)(*x.else_))
return false;
(*current)[exit] = current->size()-exit;// now we know where to jump to (after the else branch)
}
return true;
}
bool compiler::operator()(ast::while_statement const& x)
{
BOOST_ASSERT(current != 0);
std::size_t loop = current->size(); // mark our position
if (!(*this)(x.condition))
return false;
current->op(op_jump_if, 0); // we shall fill this (0) in later
std::size_t exit = current->size()-1; // mark its position
if (!(*this)(x.body))
return false;
current->op(op_jump,
int(loop-1) - int(current->size())); // loop back
(*current)[exit] = current->size()-exit; // now we know where to jump to (to exit the loop)
return true;
}
bool compiler::operator()(ast::return_statement const& x)
{
if (void_return)
{
if (x.expr)
{
error_handler(x.id, "'void' function returning a value: ");
return false;
}
}
else
{
if (!x.expr)
{
error_handler(x.id, current_function_name + " function must return a value: ");
return false;
}
}
if (x.expr)
{
if (!(*this)(*x.expr))
return false;
}
current->op(op_return);
return true;
}
bool compiler::operator()(ast::function const& x)
{
void_return = x.return_type == "void";
if (functions.find(x.function_name.name) != functions.end())
{
error_handler(x.function_name.id, "Duplicate function: " + x.function_name.name);
return false;
}
boost::shared_ptr<code_gen::function>& p = functions[x.function_name.name];
p.reset(new code_gen::function(code, x.args.size()));
current = p.get();
current_function_name = x.function_name.name;
// op_stk_adj 0 for now. we'll know how many variables
// we'll have later and add them
current->op(op_stk_adj, 0);
BOOST_FOREACH(ast::identifier const& arg, x.args)
{
current->add_var(arg.name);
}
if (!(*this)(x.body))
return false;
(*current)[1] = current->nvars(); // now store the actual number of variables
// this includes the arguments
return true;
}
bool compiler::operator()(ast::function_list const& x)
{
// Jump to the main function
code.push_back(op_jump);
code.push_back(0); // we will fill this in later when we finish compiling
// and we know where the main function is
BOOST_FOREACH(ast::function const& f, x)
{
if (!(*this)(f))
{
code.clear();
return false;
}
}
// find the main function
boost::shared_ptr<code_gen::function> p =
find_function("main");
if (!p) // main function not found
{
std::cerr << "Error: main function not defined" << std::endl;
return false;
}
code[1] = p->get_address()-1; // jump to this (main function) address
return true;
}
void compiler::print_assembler() const
{
typedef std::pair<std::string, boost::shared_ptr<code_gen::function> > pair;
BOOST_FOREACH(pair const& p, functions)
{
std::cout << ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;" << std::endl;
std::cout << p.second->get_address() << ": function " << p.first << std::endl;
p.second->print_assembler();
}
}
boost::shared_ptr<code_gen::function>
compiler::find_function(std::string const& name) const
{
function_table::const_iterator i = functions.find(name);
if (i == functions.end())
return boost::shared_ptr<code_gen::function>();
else
return i->second;
}
}}

View File

@@ -0,0 +1,123 @@
/*=============================================================================
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_CONJURE_COMPILER_HPP)
#define BOOST_SPIRIT_CONJURE_COMPILER_HPP
#include "ast.hpp"
#include "error_handler.hpp"
#include <vector>
#include <map>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
namespace client { namespace code_gen
{
///////////////////////////////////////////////////////////////////////////
// The Function
///////////////////////////////////////////////////////////////////////////
struct function
{
function(std::vector<int>& code, int nargs)
: code(code), address(code.size()), size_(0), nargs_(nargs) {}
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[address+i]; }
int const& operator[](std::size_t i) const { return code[address+i]; }
std::size_t size() const { return size_; }
std::size_t get_address() const { return address; }
int nargs() const { return nargs_; }
int nvars() const { return variables.size(); }
int const* find_var(std::string const& name) const;
void add_var(std::string const& name);
void link_to(std::string const& name, std::size_t address);
void print_assembler() const;
private:
std::map<std::string, int> variables;
std::map<std::size_t, std::string> function_calls;
std::vector<int>& code;
std::size_t address;
std::size_t size_;
std::size_t nargs_;
};
///////////////////////////////////////////////////////////////////////////
// The Compiler
///////////////////////////////////////////////////////////////////////////
struct compiler
{
typedef bool result_type;
template <typename ErrorHandler>
compiler(ErrorHandler& error_handler_)
: current(0)
{
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) { BOOST_ASSERT(0); return false; }
bool operator()(unsigned int x);
bool operator()(bool x);
bool operator()(ast::identifier const& x);
bool operator()(token_ids::type const& x);
bool operator()(ast::unary const& x);
bool operator()(ast::function_call const& x);
bool operator()(ast::expression const& x);
bool operator()(ast::assignment const& x);
bool operator()(ast::variable_declaration const& x);
bool operator()(ast::statement_list const& x);
bool operator()(ast::statement const& x);
bool operator()(ast::if_statement const& x);
bool operator()(ast::while_statement const& x);
bool operator()(ast::return_statement const& x);
bool operator()(ast::function const& x);
bool operator()(ast::function_list const& x);
void print_assembler() const;
boost::shared_ptr<code_gen::function>
find_function(std::string const& name) const;
std::vector<int>& get_code() { return code; }
std::vector<int> const& get_code() const { return code; }
private:
bool compile_expression(
int min_precedence,
std::list<ast::operation>::const_iterator& rbegin,
std::list<ast::operation>::const_iterator rend);
typedef std::map<std::string, boost::shared_ptr<code_gen::function> > function_table;
std::vector<int> code;
code_gen::function* current;
std::string current_function_name;
function_table functions;
bool void_return;
boost::function<
void(int tag, std::string const& what)>
error_handler;
};
}}
#endif

View File

@@ -0,0 +1,51 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_LEXER_CONFIG_HPP)
#define BOOST_SPIRIT_CONJURE_LEXER_CONFIG_HPP
///////////////////////////////////////////////////////////////////////////////
// The conjure lexer example can be built in 3 different variations:
//
// - With a lexer using runtime generated DFA tables
// - With a lexer using pre-generated (static) DFA tables
// - With a lexer using a pre-generated custom switch based state machine
//
// Use one of the following preprocessor constants to define, which of those
// will be built:
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on runtime generated DFA tables
// #define CONJURE_LEXER_DYNAMIC_TABLES 1
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on pre-generated static DFA tables
// #define CONJURE_LEXER_STATIC_TABLES 1
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on runtime generated DFA tables
// #define CONJURE_LEXER_STATIC_SWITCH 1
///////////////////////////////////////////////////////////////////////////////
// The default is to use the dynamic table driven lexer
#if CONJURE_LEXER_DYNAMIC_TABLES == 0 && \
CONJURE_LEXER_STATIC_TABLES == 0 && \
CONJURE_LEXER_STATIC_SWITCH == 0
#define CONJURE_LEXER_DYNAMIC_TABLES 1
#endif
///////////////////////////////////////////////////////////////////////////////
// Make sure we have only one lexer type selected
#if (CONJURE_LEXER_DYNAMIC_TABLES != 0 && CONJURE_LEXER_STATIC_TABLES != 0) || \
(CONJURE_LEXER_DYNAMIC_TABLES != 0 && CONJURE_LEXER_STATIC_SWITCH != 0) || \
(CONJURE_LEXER_STATIC_TABLES != 0 && CONJURE_LEXER_STATIC_SWITCH != 0)
#error "Configuration problem: please select exactly one type of lexer to build"
#endif
#endif

View File

@@ -0,0 +1,483 @@
// Copyright (c) 2008-2009 Ben Hanson
// Copyright (c) 2008-2011 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Auto-generated by boost::lexer, do not edit
#if !defined(BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_JUL_25_2011_07_03_08)
#define BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_JUL_25_2011_07_03_08
#include <boost/spirit/home/support/detail/lexer/char_traits.hpp>
////////////////////////////////////////////////////////////////////////////////
// the generated table of state names and the tokenizer have to be
// defined in the boost::spirit::lex::lexertl::static_ namespace
namespace boost { namespace spirit { namespace lex { namespace lexertl { namespace static_ {
////////////////////////////////////////////////////////////////////////////////
// this table defines the names of the lexer states
char const* const lexer_state_names_conjure_static[1] =
{
"INITIAL"
};
////////////////////////////////////////////////////////////////////////////////
// this variable defines the number of lexer states
std::size_t const lexer_state_count_conjure_static = 1;
////////////////////////////////////////////////////////////////////////////////
// this function returns the next matched token
template<typename Iterator>
std::size_t next_token_conjure_static (std::size_t& /*start_state_*/, bool& /*bol_*/,
Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
enum {end_state_index, id_index, unique_id_index, state_index, bol_index,
eol_index, dead_state_index, dfa_offset};
static std::size_t const npos = static_cast<std::size_t>(~0);
static std::size_t const lookup_[256] = {
41, 41, 41, 41, 41, 41, 41, 41,
41, 7, 7, 41, 41, 7, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
7, 8, 41, 41, 41, 41, 9, 41,
10, 11, 12, 13, 14, 15, 41, 16,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 41, 19, 20, 21, 22, 41,
41, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 41, 41, 41, 41, 18,
41, 23, 18, 18, 24, 25, 26, 18,
27, 28, 18, 18, 29, 18, 30, 31,
18, 18, 32, 33, 34, 35, 36, 37,
18, 18, 18, 38, 39, 40, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41 };
static std::size_t const dfa_alphabet_ = 42;
static std::size_t const dfa_[2604] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 27, 13, 11, 20, 21, 18, 16,
24, 17, 19, 2, 26, 25, 14, 12,
15, 26, 26, 7, 4, 26, 6, 26,
26, 26, 9, 26, 3, 26, 5, 8,
22, 10, 23, 0, 1, 35, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 28, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 29,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 30, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 32, 26,
26, 26, 31, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 33, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 34, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 35, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 36, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 37,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 61, 26, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 38, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 262177, 20, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 39,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131091, 12, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 40, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131093,
14, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 41, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 393241, 16, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 393242, 17, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131099, 18, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131100,
19, 0, 0, 0, 0, 0, 0, 0,
0, 0, 42, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 40, 21, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 41, 22, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 123, 23, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 125,
24, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 44, 25, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 59, 27, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 34,
30, 0, 0, 0, 0, 27, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 43, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 44,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
45, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
46, 26, 26, 26, 0, 0, 0, 0,
1, 65538, 4, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 47, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
48, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
49, 26, 26, 26, 0, 0, 0, 0,
1, 131084, 8, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 131085, 9, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131089, 10, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131090,
11, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 131092, 13, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 131094, 15, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 42, 42, 42, 42, 42,
50, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 51,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 43, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 52, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 65537, 3, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 53,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 54, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 55, 26, 26,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 56, 56, 56, 56, 56,
50, 56, 56, 56, 57, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 1, 36,
1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 65536, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 65539, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 58, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 59, 26,
26, 26, 26, 26, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 56,
56, 56, 56, 56, 60, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 1, 33, 29, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 65540, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 61, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 56,
56, 56, 56, 56, 60, 56, 56, 56,
57, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 1, 65541, 7, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0 };
if (start_token_ == end_)
{
unique_id_ = npos;
return 0;
}
std::size_t const* ptr_ = dfa_ + dfa_alphabet_;
Iterator curr_ = start_token_;
bool end_state_ = *ptr_ != 0;
std::size_t id_ = *(ptr_ + id_index);
std::size_t uid_ = *(ptr_ + unique_id_index);
Iterator end_token_ = start_token_;
while (curr_ != end_)
{
std::size_t const state_ =
ptr_[lookup_[static_cast<unsigned char>(*curr_++)]];
if (state_ == 0) break;
ptr_ = &dfa_[state_ * dfa_alphabet_];
if (*ptr_)
{
end_state_ = true;
id_ = *(ptr_ + id_index);
uid_ = *(ptr_ + unique_id_index);
end_token_ = curr_;
}
}
if (end_state_)
{
// return longest match
start_token_ = end_token_;
}
else
{
id_ = npos;
uid_ = npos;
}
unique_id_ = uid_;
return id_;
}
////////////////////////////////////////////////////////////////////////////////
// this defines a generic accessors for the information above
struct lexer_conjure_static
{
// version number and feature-set of compatible static lexer engine
enum
{
static_version = 65536,
supports_bol = false,
supports_eol = false
};
// return the number of lexer states
static std::size_t state_count()
{
return lexer_state_count_conjure_static;
}
// return the name of the lexer state as given by 'idx'
static char const* state_name(std::size_t idx)
{
return lexer_state_names_conjure_static[idx];
}
// return the next matched token
template<typename Iterator>
static std::size_t next(std::size_t &start_state_, bool& bol_
, Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
return next_token_conjure_static(start_state_, bol_, start_token_, end_, unique_id_);
}
};
}}}}} // namespace boost::spirit::lex::lexertl::static_
#endif

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2001-2011 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)
// This small utility program generates the 2 static lexers, the static table
// driven and the static switch based lexer.
#include <fstream>
#include <iostream>
#include "lexer_def.hpp"
#include <boost/spirit/include/lex_generate_static_lexertl.hpp>
int main()
{
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
lexer_type lexer;
// first generate the static switch based lexer
std::ofstream out_static("conjure_static_switch_lexer.hpp");
bool result = boost::spirit::lex::lexertl::generate_static_switch(
lexer, out_static, "conjure_static_switch");
if (!result) {
std::cerr << "Failed to generate static switch based lexer\n";
return -1;
}
// now generate the static table based lexer
std::ofstream out("conjure_static_lexer.hpp");
result = boost::spirit::lex::lexertl::generate_static(
lexer, out, "conjure_static");
if (!result) {
std::cerr << "Failed to generate static table based lexer\n";
return -1;
}
return 0;
}

View File

@@ -0,0 +1,873 @@
// Copyright (c) 2008-2009 Ben Hanson
// Copyright (c) 2008-2011 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Auto-generated by boost::lexer, do not edit
#if !defined(BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_SWITCH_JUL_25_2011_07_03_08)
#define BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_SWITCH_JUL_25_2011_07_03_08
#include <boost/spirit/home/support/detail/lexer/char_traits.hpp>
////////////////////////////////////////////////////////////////////////////////
// the generated table of state names and the tokenizer have to be
// defined in the boost::spirit::lex::lexertl::static_ namespace
namespace boost { namespace spirit { namespace lex { namespace lexertl { namespace static_ {
////////////////////////////////////////////////////////////////////////////////
// this table defines the names of the lexer states
char const* const lexer_state_names_conjure_static_switch[1] =
{
"INITIAL"
};
////////////////////////////////////////////////////////////////////////////////
// this variable defines the number of lexer states
std::size_t const lexer_state_count_conjure_static_switch = 1;
////////////////////////////////////////////////////////////////////////////////
// this function returns the next matched token
template<typename Iterator>
std::size_t next_token_conjure_static_switch (std::size_t& /*start_state_*/, bool& /*bol_*/,
Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
static std::size_t const npos = static_cast<std::size_t>(~0);
if (start_token_ == end_)
{
unique_id_ = npos;
return 0;
}
Iterator curr_ = start_token_;
bool end_state_ = false;
std::size_t id_ = npos;
std::size_t uid_ = npos;
Iterator end_token_ = start_token_;
char ch_ = 0;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9')) goto state0_1;
if (ch_ == 't') goto state0_2;
if (ch_ == 'f') goto state0_3;
if (ch_ == 'v') goto state0_4;
if (ch_ == 'i') goto state0_5;
if (ch_ == 'e') goto state0_6;
if (ch_ == 'w') goto state0_7;
if (ch_ == 'r') goto state0_8;
if (ch_ == '|') goto state0_9;
if (ch_ == '&') goto state0_10;
if (ch_ == '=') goto state0_11;
if (ch_ == '!') goto state0_12;
if (ch_ == '<') goto state0_13;
if (ch_ == '>') goto state0_14;
if (ch_ == '+') goto state0_15;
if (ch_ == '-') goto state0_16;
if (ch_ == '*') goto state0_17;
if (ch_ == '/') goto state0_18;
if (ch_ == '(') goto state0_19;
if (ch_ == ')') goto state0_20;
if (ch_ == '{') goto state0_21;
if (ch_ == '}') goto state0_22;
if (ch_ == ',') goto state0_23;
if (ch_ == ';') goto state0_24;
if ((ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'g' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'q') || ch_ == 's' || ch_ == 'u' || (ch_ >= 'x' && ch_ <= 'z')) goto state0_25;
if ((ch_ >= '\t' && ch_ <= '\n') || ch_ == '\r' || ch_ == ' ') goto state0_26;
goto end;
state0_1:
end_state_ = true;
id_ = 35;
uid_ = 0;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9')) goto state0_1;
goto end;
state0_2:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'q') || (ch_ >= 's' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'r') goto state0_27;
goto end;
state0_3:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'b' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'a') goto state0_28;
goto end;
state0_4:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'n') || (ch_ >= 'p' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'o') goto state0_29;
goto end;
state0_5:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'e') || (ch_ >= 'g' && ch_ <= 'm') || (ch_ >= 'o' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'n') goto state0_30;
if (ch_ == 'f') goto state0_31;
goto end;
state0_6:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_32;
goto end;
state0_7:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'g') || (ch_ >= 'i' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'h') goto state0_33;
goto end;
state0_8:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_34;
goto end;
state0_9:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '|') goto state0_35;
goto end;
state0_10:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '&') goto state0_36;
goto end;
state0_11:
end_state_ = true;
id_ = 61;
uid_ = 26;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_37;
goto end;
state0_12:
end_state_ = true;
id_ = 262177;
uid_ = 20;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_38;
goto end;
state0_13:
end_state_ = true;
id_ = 131091;
uid_ = 12;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_39;
goto end;
state0_14:
end_state_ = true;
id_ = 131093;
uid_ = 14;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_40;
goto end;
state0_15:
end_state_ = true;
id_ = 393241;
uid_ = 16;
end_token_ = curr_;
goto end;
state0_16:
end_state_ = true;
id_ = 393242;
uid_ = 17;
end_token_ = curr_;
goto end;
state0_17:
end_state_ = true;
id_ = 131099;
uid_ = 18;
end_token_ = curr_;
goto end;
state0_18:
end_state_ = true;
id_ = 131100;
uid_ = 19;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '*') goto state0_41;
goto end;
state0_19:
end_state_ = true;
id_ = 40;
uid_ = 21;
end_token_ = curr_;
goto end;
state0_20:
end_state_ = true;
id_ = 41;
uid_ = 22;
end_token_ = curr_;
goto end;
state0_21:
end_state_ = true;
id_ = 123;
uid_ = 23;
end_token_ = curr_;
goto end;
state0_22:
end_state_ = true;
id_ = 125;
uid_ = 24;
end_token_ = curr_;
goto end;
state0_23:
end_state_ = true;
id_ = 44;
uid_ = 25;
end_token_ = curr_;
goto end;
state0_24:
end_state_ = true;
id_ = 59;
uid_ = 27;
end_token_ = curr_;
goto end;
state0_25:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_26:
end_state_ = true;
id_ = 34;
uid_ = 30;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '\t' && ch_ <= '\n') || ch_ == '\r' || ch_ == ' ') goto state0_26;
goto end;
state0_27:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 't') || (ch_ >= 'v' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'u') goto state0_42;
goto end;
state0_28:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_43;
goto end;
state0_29:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'i') goto state0_44;
goto end;
state0_30:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 's') || (ch_ >= 'u' && ch_ <= 'z')) goto state0_25;
if (ch_ == 't') goto state0_45;
goto end;
state0_31:
end_state_ = true;
id_ = 65538;
uid_ = 4;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_32:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'r') || (ch_ >= 't' && ch_ <= 'z')) goto state0_25;
if (ch_ == 's') goto state0_46;
goto end;
state0_33:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'i') goto state0_47;
goto end;
state0_34:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 's') || (ch_ >= 'u' && ch_ <= 'z')) goto state0_25;
if (ch_ == 't') goto state0_48;
goto end;
state0_35:
end_state_ = true;
id_ = 131084;
uid_ = 8;
end_token_ = curr_;
goto end;
state0_36:
end_state_ = true;
id_ = 131085;
uid_ = 9;
end_token_ = curr_;
goto end;
state0_37:
end_state_ = true;
id_ = 131089;
uid_ = 10;
end_token_ = curr_;
goto end;
state0_38:
end_state_ = true;
id_ = 131090;
uid_ = 11;
end_token_ = curr_;
goto end;
state0_39:
end_state_ = true;
id_ = 131092;
uid_ = 13;
end_token_ = curr_;
goto end;
state0_40:
end_state_ = true;
id_ = 131094;
uid_ = 15;
end_token_ = curr_;
goto end;
state0_41:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*') goto state0_41;
if (ch_ == '*') goto state0_49;
goto end;
state0_42:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_50;
goto end;
state0_43:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'r') || (ch_ >= 't' && ch_ <= 'z')) goto state0_25;
if (ch_ == 's') goto state0_42;
goto end;
state0_44:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'c') || (ch_ >= 'e' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'd') goto state0_51;
goto end;
state0_45:
end_state_ = true;
id_ = 65537;
uid_ = 3;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_46:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_52;
goto end;
state0_47:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_53;
goto end;
state0_48:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 't') || (ch_ >= 'v' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'u') goto state0_54;
goto end;
state0_49:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '*') goto state0_49;
if (ch_ != '*' && ch_ != '/') goto state0_55;
if (ch_ == '/') goto state0_56;
goto end;
state0_50:
end_state_ = true;
id_ = 36;
uid_ = 1;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_51:
end_state_ = true;
id_ = 65536;
uid_ = 2;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_52:
end_state_ = true;
id_ = 65539;
uid_ = 5;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_53:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_57;
goto end;
state0_54:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'q') || (ch_ >= 's' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'r') goto state0_58;
goto end;
state0_55:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*') goto state0_55;
if (ch_ == '*') goto state0_59;
goto end;
state0_56:
end_state_ = true;
id_ = 33;
uid_ = 29;
end_token_ = curr_;
goto end;
state0_57:
end_state_ = true;
id_ = 65540;
uid_ = 6;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_58:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'm') || (ch_ >= 'o' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'n') goto state0_60;
goto end;
state0_59:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*' && ch_ != '/') goto state0_55;
if (ch_ == '/') goto state0_56;
if (ch_ == '*') goto state0_59;
goto end;
state0_60:
end_state_ = true;
id_ = 65541;
uid_ = 7;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
end:
if (end_state_)
{
// return longest match
start_token_ = end_token_;
}
else
{
id_ = npos;
uid_ = npos;
}
unique_id_ = uid_;
return id_;
}
////////////////////////////////////////////////////////////////////////////////
// this defines a generic accessors for the information above
struct lexer_conjure_static_switch
{
// version number and feature-set of compatible static lexer engine
enum
{
static_version = 65536,
supports_bol = false,
supports_eol = false
};
// return the number of lexer states
static std::size_t state_count()
{
return lexer_state_count_conjure_static_switch;
}
// return the name of the lexer state as given by 'idx'
static char const* state_name(std::size_t idx)
{
return lexer_state_names_conjure_static_switch[idx];
}
// return the next matched token
template<typename Iterator>
static std::size_t next(std::size_t &start_state_, bool& bol_
, Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
return next_token_conjure_static_switch(start_state_, bol_, start_token_, end_, unique_id_);
}
};
}}}}} // namespace boost::spirit::lex::lexertl::static_
#endif

View File

@@ -0,0 +1,105 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_ERROR_HANDLER_HPP)
#define BOOST_SPIRIT_CONJURE_ERROR_HANDLER_HPP
#include <iostream>
#include <string>
#include <vector>
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// The error handler
///////////////////////////////////////////////////////////////////////////////
template <typename BaseIterator, typename Iterator>
struct error_handler
{
template <typename, typename, typename>
struct result { typedef void type; };
error_handler(BaseIterator first, BaseIterator last)
: first(first), last(last) {}
template <typename Message, typename What>
void operator()(
Message const& message,
What const& what,
Iterator err_pos) const
{
// retrieve underlying iterator from current token, err_pos points
// to the last validly matched token, so we use its end iterator
// as the error position
BaseIterator err_pos_base = err_pos->matched().end();
std::cout << message << what << std::endl;
if (err_pos_base != BaseIterator())
dump_error_line(err_pos_base);
}
void dump_error_line(BaseIterator err_pos_base) const
{
int line;
BaseIterator line_start = get_pos(err_pos_base, line);
if (err_pos_base != last)
{
std::cout << " line " << line << ':' << std::endl;
std::cout << get_line(line_start) << std::endl;
for (; line_start != err_pos_base; ++line_start)
std::cout << ' ';
std::cout << '^' << std::endl;
}
else
{
std::cout << "Unexpected end of file.\n";
}
}
BaseIterator get_pos(BaseIterator err_pos, int& line) const
{
line = 1;
BaseIterator i = first;
BaseIterator 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(BaseIterator err_pos) const
{
BaseIterator i = err_pos;
// position i to the next EOL
while (i != last && (*i != '\r' && *i != '\n'))
++i;
return std::string(err_pos, i);
}
BaseIterator first;
BaseIterator last;
std::vector<Iterator> iters;
};
}
#endif

View File

@@ -0,0 +1,20 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "config.hpp"
#include "lexer.hpp"
#include "expression_def.hpp"
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
typedef lexer_type::iterator_type iterator_type;
template struct client::parser::expression<iterator_type, lexer_type>;

View File

@@ -0,0 +1,58 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_EXPRESSION_HPP)
#define BOOST_SPIRIT_CONJURE_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, typename Lexer>
struct expression : qi::grammar<Iterator, ast::expression()>
{
typedef error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
expression(error_handler_type& error_handler, Lexer const& l);
Lexer const& lexer;
qi::rule<Iterator, ast::expression()> expr;
qi::rule<Iterator, ast::operand()> unary_expr, primary_expr;
qi::rule<Iterator, ast::function_call()> function_call;
qi::rule<Iterator, std::list<ast::expression>()> argument_list;
qi::rule<Iterator, std::string()> identifier;
};
}}
#endif

View File

@@ -0,0 +1,94 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "expression.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
#include <boost/phoenix/function.hpp>
#include <boost/spirit/include/lex_plain_token.hpp>
namespace client { namespace parser
{
template <typename Iterator, typename Lexer>
expression<Iterator, Lexer>::expression(
error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
, Lexer const& l)
: expression::base_type(expr), lexer(l)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
qi::tokenid_mask_type tokenid_mask;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
typedef function<error_handler_type> error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
///////////////////////////////////////////////////////////////////////
// Main expression grammar
expr =
unary_expr
>> *(tokenid_mask(token_ids::op_binary) > unary_expr)
;
unary_expr =
primary_expr
| (tokenid_mask(token_ids::op_unary) > unary_expr)
;
primary_expr =
lexer.lit_uint
| function_call
| identifier
| lexer.true_or_false
| '(' > expr > ')'
;
function_call =
(identifier >> '(')
> argument_list
> ')'
;
argument_list = -(expr % ',');
identifier = lexer.identifier;
///////////////////////////////////////////////////////////////////////
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(unary_expr)
(primary_expr)
(function_call)
(argument_list)
(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,20 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "config.hpp"
#include "lexer.hpp"
#include "function_def.hpp"
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
typedef lexer_type::iterator_type iterator_type;
template struct client::parser::function<iterator_type, lexer_type>;

View File

@@ -0,0 +1,36 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_FUNCTION_HPP)
#define BOOST_SPIRIT_CONJURE_FUNCTION_HPP
#include "statement.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The function grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator, typename Lexer>
struct function : qi::grammar<Iterator, ast::function()>
{
typedef error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
function(error_handler_type& error_handler, Lexer const& l);
statement<Iterator, Lexer> body;
qi::rule<Iterator, ast::identifier()> identifier;
qi::rule<Iterator, std::list<ast::identifier>()> argument_list;
qi::rule<Iterator, ast::function()> start;
};
}}
#endif

View File

@@ -0,0 +1,64 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "function.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
namespace client { namespace parser
{
template <typename Iterator, typename Lexer>
function<Iterator, Lexer>::function(
error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
, Lexer const& l)
: function::base_type(start), body(error_handler, l)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
typedef function<error_handler_type> error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
identifier = body.expr.identifier;
argument_list = -(identifier % ',');
start = (l.token("void") | l.token("int"))
> identifier
> '(' > argument_list > ')'
> '{' > body > '}'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(identifier)
(argument_list)
(start)
);
// Error handling: on error in start, call error_handler.
on_error<fail>(start,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
// Annotation: on success in start, call annotation.
on_success(identifier,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,154 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_IDS_HPP)
#define BOOST_SPIRIT_CONJURE_IDS_HPP
namespace client
{
struct op_type
{
enum type
{
binary = 0x20000,
unary = 0x40000,
assign = 0x80000
};
};
struct op
{
enum type
{
// binary
comma,
assign,
plus_assign,
minus_assign,
times_assign,
divide_assign,
mod_assign,
bit_and_assign,
bit_xor_assign,
bit_or_assign,
shift_left_assign,
shift_right_assign,
logical_or,
logical_and,
bit_or,
bit_xor,
bit_and,
equal,
not_equal,
less,
less_equal,
greater,
greater_equal,
shift_left,
shift_right,
plus,
minus,
times,
divide,
mod,
// unary
plus_plus,
minus_minus,
compl_,
not_,
};
};
template <int type, int op>
struct make_op
{
static int const value = type + op;
};
template <op::type op>
struct unary_op : make_op<op_type::unary, op> {};
template <op::type op>
struct binary_op
: make_op<op_type::binary, op> {};
template <op::type op>
struct assign_op
: make_op<op_type::assign, op> {};
template <op::type op>
struct binary_or_unary_op
: make_op<op_type::unary | op_type::binary, op> {};
struct token_ids
{
enum type
{
// pseudo tags
invalid = -1,
op_binary = op_type::binary,
op_unary = op_type::unary,
op_assign = op_type::assign,
// binary / unary operators with common tokens
// '+' and '-' can be binary or unary
// (the lexer cannot distinguish which)
plus = binary_or_unary_op<op::plus>::value,
minus = binary_or_unary_op<op::minus>::value,
// binary operators
comma = binary_op<op::comma>::value,
assign = assign_op<op::assign>::value,
plus_assign = assign_op<op::plus_assign>::value,
minus_assign = assign_op<op::minus_assign>::value,
times_assign = assign_op<op::times_assign>::value,
divide_assign = assign_op<op::divide_assign>::value,
mod_assign = assign_op<op::mod_assign>::value,
bit_and_assign = assign_op<op::bit_and_assign>::value,
bit_xor_assign = assign_op<op::bit_xor_assign>::value,
bit_or_assign = assign_op<op::bit_or_assign>::value,
shift_left_assign = assign_op<op::shift_left_assign>::value,
shift_right_assign = assign_op<op::shift_right_assign>::value,
logical_or = binary_op<op::logical_or>::value,
logical_and = binary_op<op::logical_and>::value,
bit_or = binary_op<op::bit_or>::value,
bit_xor = binary_op<op::bit_xor>::value,
bit_and = binary_op<op::bit_and>::value,
equal = binary_op<op::equal>::value,
not_equal = binary_op<op::not_equal>::value,
less = binary_op<op::less>::value,
less_equal = binary_op<op::less_equal>::value,
greater = binary_op<op::greater>::value,
greater_equal = binary_op<op::greater_equal>::value,
shift_left = binary_op<op::shift_left>::value,
shift_right = binary_op<op::shift_right>::value,
times = binary_op<op::times>::value,
divide = binary_op<op::divide>::value,
mod = binary_op<op::mod>::value,
// unary operators with overlaps
// '++' and '--' can be prefix or postfix
// (the lexer cannot distinguish which)
plus_plus = unary_op<op::plus_plus>::value,
minus_minus = unary_op<op::minus_minus>::value,
// unary operators
compl_ = unary_op<op::compl_>::value,
not_ = unary_op<op::not_>::value,
// misc tags
identifier = op::not_ + 1,
comment,
whitespace,
lit_uint,
true_or_false
};
};
}
#endif

View File

@@ -0,0 +1,17 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "config.hpp"
#include "lexer_def.hpp"
typedef std::string::const_iterator base_iterator_type;
template client::lexer::conjure_tokens<base_iterator_type>::conjure_tokens();
template bool client::lexer::conjure_tokens<base_iterator_type>::add_keyword(
std::string const&);

View File

@@ -0,0 +1,128 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_LEXER_HPP)
#define BOOST_SPIRIT_CONJURE_LEXER_HPP
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/lex_lexertl_position_token.hpp>
#include "config.hpp"
#include "ids.hpp"
#if CONJURE_LEXER_STATIC_TABLES != 0
#include <boost/spirit/include/lex_static_lexertl.hpp>
#include "conjure_static_lexer.hpp"
#elif CONJURE_LEXER_STATIC_SWITCH != 0
#include <boost/spirit/include/lex_static_lexertl.hpp>
#include "conjure_static_switch_lexer.hpp"
#endif
#include <boost/assert.hpp>
namespace client { namespace lexer
{
namespace lex = boost::spirit::lex;
///////////////////////////////////////////////////////////////////////////
namespace detail
{
namespace lex = boost::spirit::lex;
template <typename BaseIterator>
struct get_lexer_type
{
// Our token needs to be able to carry several token values:
// std::string, unsigned int, and bool
typedef boost::mpl::vector<std::string, unsigned int, bool>
token_value_types;
// Using the position_token class as the token type to be returned
// from the lexer iterators allows to retain positional information
// as every token instance stores an iterator pair pointing to the
// matched input sequence.
typedef lex::lexertl::position_token<
BaseIterator, token_value_types, boost::mpl::false_
> token_type;
#if CONJURE_LEXER_DYNAMIC_TABLES != 0
// use the lexer based on runtime generated DFA tables
typedef lex::lexertl::actor_lexer<token_type> type;
#elif CONJURE_LEXER_STATIC_TABLES != 0
// use the lexer based on pre-generated static DFA tables
typedef lex::lexertl::static_actor_lexer<
token_type
, boost::spirit::lex::lexertl::static_::lexer_conjure_static
> type;
#elif CONJURE_LEXER_STATIC_SWITCH != 0
// use the lexer based on pre-generated static code
typedef lex::lexertl::static_actor_lexer<
token_type
, boost::spirit::lex::lexertl::static_::lexer_conjure_static_switch
> type;
#else
#error "Configuration problem: please select exactly one type of lexer to build"
#endif
};
}
///////////////////////////////////////////////////////////////////////////
template <typename BaseIterator>
struct conjure_tokens
: lex::lexer<typename detail::get_lexer_type<BaseIterator>::type>
{
private:
// get the type of any qi::raw_token(...) and qi::token(...) constructs
typedef typename boost::spirit::result_of::terminal<
boost::spirit::tag::raw_token(token_ids::type)
>::type raw_token_spec;
typedef typename boost::spirit::result_of::terminal<
boost::spirit::tag::token(token_ids::type)
>::type token_spec;
typedef std::map<std::string, token_ids::type> keyword_map_type;
protected:
// add a keyword to the mapping table
bool add_keyword(std::string const& keyword);
public:
typedef BaseIterator base_iterator_type;
conjure_tokens();
// extract a raw_token(id) for the given registered keyword
raw_token_spec operator()(std::string const& kwd) const
{
namespace qi = boost::spirit::qi;
qi::raw_token_type raw_token;
typename keyword_map_type::const_iterator it = keywords_.find(kwd);
BOOST_ASSERT(it != keywords_.end());
return raw_token((it != keywords_.end()) ? (*it).second : token_ids::invalid);
}
// extract a token(id) for the given registered keyword
token_spec token(std::string const& kwd) const
{
namespace qi = boost::spirit::qi;
qi::token_type token;
typename keyword_map_type::const_iterator it = keywords_.find(kwd);
BOOST_ASSERT(it != keywords_.end());
return token((it != keywords_.end()) ? (*it).second : token_ids::invalid);
}
lex::token_def<std::string> identifier;
lex::token_def<unsigned int> lit_uint;
lex::token_def<bool> true_or_false;
keyword_map_type keywords_;
};
}}
#endif

View File

@@ -0,0 +1,76 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "lexer.hpp"
#include <boost/phoenix/operator/self.hpp>
namespace client { namespace lexer
{
template <typename BaseIterator>
conjure_tokens<BaseIterator>::conjure_tokens()
: identifier("[a-zA-Z_][a-zA-Z_0-9]*", token_ids::identifier)
, lit_uint("[0-9]+", token_ids::lit_uint)
, true_or_false("true|false", token_ids::true_or_false)
{
lex::_pass_type _pass;
this->self = lit_uint | true_or_false;
add_keyword("void");
add_keyword("int");
add_keyword("if");
add_keyword("else");
add_keyword("while");
add_keyword("return");
this->self.add
("\\|\\|", token_ids::logical_or)
("&&", token_ids::logical_and)
("==", token_ids::equal)
("!=", token_ids::not_equal)
("<", token_ids::less)
("<=", token_ids::less_equal)
(">", token_ids::greater)
(">=", token_ids::greater_equal)
("\\+", token_ids::plus)
("\\-", token_ids::minus)
("\\*", token_ids::times)
("\\/", token_ids::divide)
("!", token_ids::not_)
;
this->self += lex::char_('(') | ')' | '{' | '}' | ',' | '=' | ';';
this->self +=
identifier
| lex::string("\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/", token_ids::comment)
[
lex::_pass = lex::pass_flags::pass_ignore
]
| lex::string("[ \t\n\r]+", token_ids::whitespace)
[
lex::_pass = lex::pass_flags::pass_ignore
]
;
}
template <typename BaseIterator>
bool conjure_tokens<BaseIterator>::add_keyword(std::string const& keyword)
{
// add the token to the lexer
token_ids::type id = token_ids::type(this->get_next_id());
this->self.add(keyword, id);
// store the mapping for later retrieval
std::pair<typename keyword_map_type::iterator, bool> p =
keywords_.insert(typename keyword_map_type::value_type(keyword, id));
return p.second;
}
}}

View File

@@ -0,0 +1,131 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
//
// Not a calculator anymore, right? :-)
//
// [ JDG April 10, 2007 ] spirit2
// [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
// [ HK June 3, 2011 ] Adding lexer
//
///////////////////////////////////////////////////////////////////////////////
#include "config.hpp"
#include "function.hpp"
#include "vm.hpp"
#include "compiler.hpp"
#include "lexer.hpp"
#include <boost/lexical_cast.hpp>
#include <fstream>
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
char const* filename;
if (argc > 1)
{
filename = argv[1];
}
else
{
std::cerr << "Error: No input file provided." << std::endl;
return 1;
}
std::ifstream in(filename, std::ios_base::in);
if (!in)
{
std::cerr << "Error: Could not open input file: "
<< filename << std::endl;
return 1;
}
std::string source_code; // We will read the contents here.
in.unsetf(std::ios::skipws); // No white space skipping!
std::copy(
std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::back_inserter(source_code));
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
typedef lexer_type::iterator_type iterator_type;
lexer_type lexer; // Our lexer
base_iterator_type first = source_code.begin();
base_iterator_type last = source_code.end();
iterator_type iter = lexer.begin(first, last);
iterator_type end = lexer.end();
client::vmachine vm; // Our virtual machine
client::ast::function_list ast; // Our AST
client::error_handler<base_iterator_type, iterator_type>
error_handler(first, last); // Our error handler
client::parser::function<iterator_type, lexer_type>
function(error_handler, lexer); // Our parser
client::code_gen::compiler
compiler(error_handler); // Our compiler
// note: we don't need a skipper
bool success = parse(iter, end, +function, ast);
std::cout << "-------------------------\n";
if (success && iter == end)
{
if (compiler(ast))
{
boost::shared_ptr<client::code_gen::function>
p = compiler.find_function("main");
if (!p)
return 1;
int nargs = argc-2;
if (p->nargs() != nargs)
{
std::cerr << "Error: main function requires " << p->nargs() << " arguments." << std::endl;
std::cerr << nargs << " supplied." << std::endl;
return 1;
}
std::cout << "Success\n";
std::cout << "-------------------------\n";
std::cout << "Assembler----------------\n\n";
compiler.print_assembler();
// Push the arguments into our stack
for (int i = 0; i < nargs; ++i)
vm.get_stack()[i] = boost::lexical_cast<int>(argv[i+2]);
// Call the interpreter
int r = vm.execute(compiler.get_code());
std::cout << "-------------------------\n";
std::cout << "Result: " << r << std::endl;
std::cout << "-------------------------\n\n";
}
else
{
std::cout << "Compile failure\n";
}
}
else
{
error_handler.dump_error_line(first);
std::cout << "Parse failure\n";
}
return 0;
}

View File

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

View File

@@ -0,0 +1,42 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_STATEMENT_HPP)
#define BOOST_SPIRIT_CONJURE_STATEMENT_HPP
#include "expression.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The statement grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator, typename Lexer>
struct statement : qi::grammar<Iterator, ast::statement_list()>
{
typedef error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
statement(error_handler_type& error_handler, Lexer const& l);
expression<Iterator, Lexer> expr;
qi::rule<Iterator, ast::statement_list()>
statement_list, compound_statement;
qi::rule<Iterator, ast::statement()> statement_;
qi::rule<Iterator, ast::variable_declaration()> variable_declaration;
qi::rule<Iterator, ast::assignment()> assignment;
qi::rule<Iterator, ast::if_statement()> if_statement;
qi::rule<Iterator, ast::while_statement()> while_statement;
qi::rule<Iterator, ast::return_statement()> return_statement;
};
}}
#endif

View File

@@ -0,0 +1,123 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "statement.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
namespace client { namespace parser
{
template <typename Iterator, typename Lexer>
statement<Iterator, Lexer>::statement(
error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
, Lexer const& l)
: statement::base_type(statement_list), expr(error_handler, l)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
typedef function<error_handler_type> error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
statement_list =
+statement_
;
statement_ =
variable_declaration
| assignment
| compound_statement
| if_statement
| while_statement
| return_statement
;
variable_declaration =
l("int")
> expr.identifier
> -('=' > expr)
> ';'
;
assignment =
expr.identifier
> '='
> expr
> ';'
;
if_statement =
l("if")
> '('
> expr
> ')'
> statement_
>
-(
l("else")
> statement_
)
;
while_statement =
l("while")
> '('
> expr
> ')'
> statement_
;
compound_statement =
'{' >> -statement_list >> '}'
;
return_statement =
l("return")
> -expr
> ';'
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(statement_list)
(statement_)
(variable_declaration)
(assignment)
(if_statement)
(while_statement)
(compound_statement)
(return_statement)
);
// 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 variable_declaration,
// assignment and return_statement, call annotation.
on_success(variable_declaration,
annotation_function(error_handler.iters)(_val, _1));
on_success(assignment,
annotation_function(error_handler.iters)(_val, _1));
on_success(return_statement,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,160 @@
/*=============================================================================
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 "config.hpp"
#include "vm.hpp"
#if defined(_MSC_VER)
# pragma warning(disable: 4800) // forcing value to bool 'true' or 'false'
// (performance warning)
#endif
namespace client
{
int vmachine::execute(
std::vector<int> const& code
, std::vector<int>::const_iterator pc
, std::vector<int>::iterator frame_ptr
)
{
std::vector<int>::iterator stack_ptr = frame_ptr;
while (true)
{
switch (*pc++)
{
case op_neg:
stack_ptr[-1] = -stack_ptr[-1];
break;
case op_not:
stack_ptr[-1] = !bool(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_eq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] == stack_ptr[0]);
break;
case op_neq:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] != stack_ptr[0]);
break;
case op_lt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] < stack_ptr[0]);
break;
case op_lte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] <= stack_ptr[0]);
break;
case op_gt:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] > stack_ptr[0]);
break;
case op_gte:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1] >= stack_ptr[0]);
break;
case op_and:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) && bool(stack_ptr[0]);
break;
case op_or:
--stack_ptr;
stack_ptr[-1] = bool(stack_ptr[-1]) || bool(stack_ptr[0]);
break;
case op_load:
*stack_ptr++ = frame_ptr[*pc++];
break;
case op_store:
--stack_ptr;
frame_ptr[*pc++] = stack_ptr[0];
break;
case op_int:
*stack_ptr++ = *pc++;
break;
case op_true:
*stack_ptr++ = true;
break;
case op_false:
*stack_ptr++ = false;
break;
case op_jump:
pc += *pc;
break;
case op_jump_if:
if (!bool(stack_ptr[-1]))
pc += *pc;
else
++pc;
--stack_ptr;
break;
case op_stk_adj:
stack_ptr += *pc++;
break;
case op_call:
{
int nargs = *pc++;
int jump = *pc++;
// a function call is a recursive call to execute
int r = execute(
code
, code.begin() + jump
, stack_ptr - nargs
);
// cleanup after return from function
stack_ptr[-nargs] = r; // get return value
stack_ptr -= (nargs - 1); // the stack will now contain
// the return value
}
break;
case op_return:
return stack_ptr[-1];
}
}
}
}

View File

@@ -0,0 +1,82 @@
/*=============================================================================
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_CONJURE_VM_HPP)
#define BOOST_SPIRIT_CONJURE_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_not, // boolean negate the top stack entry
op_eq, // compare the top two stack entries for ==
op_neq, // compare the top two stack entries for !=
op_lt, // compare the top two stack entries for <
op_lte, // compare the top two stack entries for <=
op_gt, // compare the top two stack entries for >
op_gte, // compare the top two stack entries for >=
op_and, // logical and top two stack entries
op_or, // logical or top two stack entries
op_load, // load a variable
op_store, // store a variable
op_int, // push constant integer into the stack
op_true, // push constant 0 into the stack
op_false, // push constant 1 into the stack
op_jump_if, // jump to a relative position in the code if top stack
// evaluates to false
op_jump, // jump to a relative position in the code
op_stk_adj, // adjust the stack (for args and locals)
op_call, // function call
op_return // return from function
};
class vmachine
{
public:
vmachine(unsigned stackSize = 4096)
: stack(stackSize)
{
}
int execute(std::vector<int> const& code)
{
return execute(code, code.begin(), stack.begin());
}
std::vector<int> const& get_stack() const { return stack; };
std::vector<int>& get_stack() { return stack; };
private:
int execute(
std::vector<int> const& code // the program code
, std::vector<int>::const_iterator pc // program counter
, std::vector<int>::iterator frame_ptr // start of arguments and locals
);
std::vector<int> stack;
};
}
#endif

View File

@@ -0,0 +1,141 @@
/*=============================================================================
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_CONJURE_ANNOTATION_HPP)
#define BOOST_SPIRIT_CONJURE_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.
///////////////////////////////////////////////////////////////////////////////
struct set_annotation_id
{
typedef void result_type;
int id;
set_annotation_id(int id) : id(id) {}
void operator()(ast::function_call& x) const
{
x.function_name.id = id;
}
template <typename T>
void dispatch(T& x, boost::mpl::true_) const
{
x.id = id;
}
template <typename T>
void dispatch(T& x, boost::mpl::false_) const
{
// no-op
}
template <typename T>
void operator()(T& x) const
{
typename boost::is_base_of<ast::tagged, T> is_tagged;
dispatch(x, is_tagged);
}
};
struct get_annotation_id
{
typedef int result_type;
int operator()(ast::function_call& x) const
{
return x.function_name.id;
}
template <typename T>
int dispatch(T& x, boost::mpl::true_) const
{
return x.id;
}
template <typename T>
int dispatch(T& x, boost::mpl::false_) const
{
return -1;
}
template <typename T>
int operator()(T& x) const
{
typename boost::is_base_of<ast::tagged, T> is_tagged;
return dispatch(x, is_tagged);
}
};
template <typename Iterator>
struct annotation
{
template <typename, typename>
struct result { typedef void type; };
std::vector<Iterator>& iters;
annotation(std::vector<Iterator>& iters)
: iters(iters) {}
void operator()(ast::operand& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
boost::apply_visitor(set_annotation_id(id), ast);
ast.id = id;
}
void operator()(ast::primary_expr& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
boost::apply_visitor(set_annotation_id(id), ast);
ast.id = id;
}
void operator()(ast::variable_declaration& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::assignment& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.lhs.id = id;
}
void operator()(ast::return_statement& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::identifier& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
};
}
#endif

View File

@@ -0,0 +1,239 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_AST_HPP)
#define BOOST_SPIRIT_CONJURE_AST_HPP
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/support_extended_variant.hpp>
#include <boost/spirit/include/support_attributes.hpp>
#include <boost/optional.hpp>
#include <list>
#include "ids.hpp"
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 unary_expr;
struct function_call;
struct expression;
struct identifier : tagged
{
identifier(std::string const& name = "") : name(name) {}
std::string name;
};
struct primary_expr :
tagged,
boost::spirit::extended_variant<
nil
, bool
, unsigned int
, identifier
, boost::recursive_wrapper<expression>
>
{
primary_expr() : base_type() {}
primary_expr(bool val) : base_type(val) {}
primary_expr(unsigned int val) : base_type(val) {}
primary_expr(identifier const& val) : base_type(val) {}
primary_expr(expression const& val) : base_type(val) {}
primary_expr(primary_expr const& rhs)
: base_type(rhs.get()) {}
};
struct operand :
tagged,
boost::spirit::extended_variant<
nil
, primary_expr
, boost::recursive_wrapper<unary_expr>
, boost::recursive_wrapper<function_call>
>
{
operand() : base_type() {}
operand(primary_expr const& val) : base_type(val) {}
operand(unary_expr const& val) : base_type(val) {}
operand(function_call const& val) : base_type(val) {}
operand(operand const& rhs)
: base_type(rhs.get()) {}
};
struct unary_expr : tagged
{
token_ids::type operator_;
operand operand_;
};
struct operation
{
token_ids::type operator_;
operand operand_;
};
struct function_call
{
identifier function_name;
std::list<expression> args;
};
struct expression
{
operand first;
std::list<operation> rest;
};
struct assignment
{
identifier lhs;
token_ids::type operator_;
expression rhs;
};
struct variable_declaration
{
identifier lhs;
boost::optional<expression> rhs;
};
struct if_statement;
struct while_statement;
struct statement_list;
struct return_statement;
typedef boost::variant<
nil
, variable_declaration
, assignment
, boost::recursive_wrapper<if_statement>
, boost::recursive_wrapper<while_statement>
, boost::recursive_wrapper<return_statement>
, boost::recursive_wrapper<statement_list>
, boost::recursive_wrapper<expression>
>
statement;
struct statement_list : std::list<statement> {};
struct if_statement
{
expression condition;
statement then;
boost::optional<statement> else_;
};
struct while_statement
{
expression condition;
statement body;
};
struct return_statement : tagged
{
boost::optional<expression> expr;
};
struct function
{
std::string return_type;
identifier function_name;
std::list<identifier> args;
boost::optional<statement_list> body;
};
typedef std::list<function> function_list;
// print functions for debugging
inline std::ostream& operator<<(std::ostream& out, nil)
{
out << "nil"; return out;
}
inline std::ostream& operator<<(std::ostream& out, identifier const& id)
{
out << id.name; return out;
}
}}
BOOST_FUSION_ADAPT_STRUCT(
client::ast::unary_expr,
(client::token_ids::type, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::operation,
(client::token_ids::type, operator_)
(client::ast::operand, operand_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function_call,
(client::ast::identifier, function_name)
(std::list<client::ast::expression>, args)
)
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::identifier, lhs)
(boost::optional<client::ast::expression>, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::assignment,
(client::ast::identifier, lhs)
(client::token_ids::type, operator_)
(client::ast::expression, rhs)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::if_statement,
(client::ast::expression, condition)
(client::ast::statement, then)
(boost::optional<client::ast::statement>, else_)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::while_statement,
(client::ast::expression, condition)
(client::ast::statement, body)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::return_statement,
(boost::optional<client::ast::expression>, expr)
)
BOOST_FUSION_ADAPT_STRUCT(
client::ast::function,
(std::string, return_type)
(client::ast::identifier, function_name)
(std::list<client::ast::identifier>, args)
(boost::optional<client::ast::statement_list>, body)
)
#endif

View File

@@ -0,0 +1,311 @@
/*=============================================================================
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_CONJURE_COMPILER_HPP)
#define BOOST_SPIRIT_CONJURE_COMPILER_HPP
#include "ast.hpp"
#include "error_handler.hpp"
#include "vm.hpp"
#include <map>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <llvm/DerivedTypes.h>
#include <llvm/Constants.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/PassManager.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Support/IRBuilder.h>
namespace client { namespace code_gen
{
unsigned const int_size = 32;
struct compiler;
struct llvm_compiler;
///////////////////////////////////////////////////////////////////////////
// The Value (light abstraction of an LLVM::Value)
///////////////////////////////////////////////////////////////////////////
struct value
{
value();
value(value const& rhs);
value& operator=(value const& rhs);
bool is_lvalue() const;
bool is_valid() const;
operator bool() const;
value& assign(value const& rhs);
void name(char const* id);
void name(std::string const& id);
friend value operator-(value a);
friend value operator!(value a);
friend value operator+(value a, value b);
friend value operator-(value a, value b);
friend value operator*(value a, value b);
friend value operator/(value a, value b);
friend value operator%(value a, value b);
friend value operator&(value a, value b);
friend value operator|(value a, value b);
friend value operator^(value a, value b);
friend value operator<<(value a, value b);
friend value operator>>(value a, value b);
friend value operator==(value a, value b);
friend value operator!=(value a, value b);
friend value operator<(value a, value b);
friend value operator<=(value a, value b);
friend value operator>(value a, value b);
friend value operator>=(value a, value b);
private:
struct to_llvm_value;
friend struct to_llvm_value;
friend struct llvm_compiler;
value(
llvm::Value* v,
bool is_lvalue_,
llvm::IRBuilder<>* builder);
llvm::LLVMContext& context() const
{ return llvm::getGlobalContext(); }
operator llvm::Value*() const;
llvm::Value* v;
bool is_lvalue_;
llvm::IRBuilder<>* builder;
};
///////////////////////////////////////////////////////////////////////////
// The Basic Block (light abstraction of an LLVM::BasicBlock)
///////////////////////////////////////////////////////////////////////////
struct function;
struct basic_block
{
basic_block()
: b(0) {}
bool has_terminator() const;
bool is_valid() const;
private:
basic_block(llvm::BasicBlock* b)
: b(b) {}
operator llvm::BasicBlock*() const
{ return b; }
friend struct llvm_compiler;
friend struct function;
llvm::BasicBlock* b;
};
///////////////////////////////////////////////////////////////////////////
// The Function (light abstraction of an LLVM::Function)
///////////////////////////////////////////////////////////////////////////
struct llvm_compiler;
struct function
{
private:
struct to_value;
typedef llvm::Function::arg_iterator arg_iterator;
typedef boost::transform_iterator<
to_value, arg_iterator>
arg_val_iterator;
public:
typedef boost::iterator_range<arg_val_iterator> arg_range;
function()
: f(0), c(c) {}
std::string name() const;
std::size_t arg_size() const;
arg_range args() const;
void add(basic_block const& b);
void erase_from_parent();
basic_block last_block();
bool empty() const;
bool is_valid() const;
void verify() const;
private:
function(llvm::Function* f, llvm_compiler* c)
: f(f), c(c) {}
operator llvm::Function*() const;
friend struct llvm_compiler;
llvm::Function* f;
llvm_compiler* c;
};
///////////////////////////////////////////////////////////////////////////
// The LLVM Compiler. Lower level compiler (does not deal with ASTs)
///////////////////////////////////////////////////////////////////////////
struct llvm_compiler
{
llvm_compiler(vmachine& vm)
: llvm_builder(context())
, vm(vm)
, fpm(vm.module())
{ init_fpm(); }
value val() { return value(); }
value val(unsigned int x);
value val(int x);
value val(bool x);
value var(char const* name);
value var(std::string const& name);
template <typename Container>
value call(function callee, Container const& args);
function get_function(char const* name);
function get_function(std::string const& name);
function get_current_function();
function declare_function(
bool void_return
, std::string const& name
, std::size_t nargs);
basic_block make_basic_block(
char const* name
, function parent = function()
, basic_block before = basic_block());
basic_block get_insert_block();
void set_insert_point(basic_block b);
void conditional_branch(
value cond, basic_block true_br, basic_block false_br);
void branch(basic_block b);
void return_();
void return_(value v);
void optimize_function(function f);
protected:
llvm::LLVMContext& context() const
{ return llvm::getGlobalContext(); }
llvm::IRBuilder<>& builder()
{ return llvm_builder; }
private:
friend struct function::to_value;
value val(llvm::Value* v);
template <typename C>
llvm::Value* call_impl(
function callee,
C const& args);
void init_fpm();
llvm::IRBuilder<> llvm_builder;
vmachine& vm;
llvm::FunctionPassManager fpm;
};
///////////////////////////////////////////////////////////////////////////
// The main compiler. Generates code from our AST.
///////////////////////////////////////////////////////////////////////////
struct compiler : llvm_compiler
{
typedef value result_type;
template <typename ErrorHandler>
compiler(vmachine& vm, ErrorHandler& error_handler_)
: llvm_compiler(vm)
{
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]);
}
value operator()(ast::nil) { BOOST_ASSERT(0); return val(); }
value operator()(unsigned int x);
value operator()(bool x);
value operator()(ast::primary_expr const& x);
value operator()(ast::identifier const& x);
value operator()(ast::unary_expr const& x);
value operator()(ast::function_call const& x);
value operator()(ast::expression const& x);
value operator()(ast::assignment const& x);
bool operator()(ast::variable_declaration const& x);
bool operator()(ast::statement_list const& x);
bool operator()(ast::statement const& x);
bool operator()(ast::if_statement const& x);
bool operator()(ast::while_statement const& x);
bool operator()(ast::return_statement const& x);
bool operator()(ast::function const& x);
bool operator()(ast::function_list const& x);
private:
value compile_binary_expression(
value lhs, value rhs, token_ids::type op);
value compile_expression(
int min_precedence,
value lhs,
std::list<ast::operation>::const_iterator& rest_begin,
std::list<ast::operation>::const_iterator rest_end);
struct statement_compiler;
statement_compiler& as_statement();
function function_decl(ast::function const& x);
void function_allocas(ast::function const& x, function function);
boost::function<
void(int tag, std::string const& what)>
error_handler;
bool void_return;
std::string current_function_name;
std::map<std::string, value> locals;
basic_block return_block;
value return_var;
};
}}
#endif

View File

@@ -0,0 +1,51 @@
/*=============================================================================
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_LEXER_CONFIG_HPP)
#define BOOST_SPIRIT_CONJURE_LEXER_CONFIG_HPP
///////////////////////////////////////////////////////////////////////////////
// The conjure lexer example can be built in 3 different variations:
//
// - With a lexer using runtime generated DFA tables
// - With a lexer using pre-generated (static) DFA tables
// - With a lexer using a pre-generated custom switch based state machine
//
// Use one of the following preprocessor constants to define, which of those
// will be built:
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on runtime generated DFA tables
// #define CONJURE_LEXER_DYNAMIC_TABLES 1
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on pre-generated static DFA tables
// #define CONJURE_LEXER_STATIC_TABLES 1
///////////////////////////////////////////////////////////////////////////////
// Use the lexer based on runtime generated DFA tables
// #define CONJURE_LEXER_STATIC_SWITCH 1
///////////////////////////////////////////////////////////////////////////////
// The default is to use the dynamic table driven lexer
#if CONJURE_LEXER_DYNAMIC_TABLES == 0 && \
CONJURE_LEXER_STATIC_TABLES == 0 && \
CONJURE_LEXER_STATIC_SWITCH == 0
#define CONJURE_LEXER_DYNAMIC_TABLES 1
#endif
///////////////////////////////////////////////////////////////////////////////
// Make sure we have only one lexer type selected
#if (CONJURE_LEXER_DYNAMIC_TABLES != 0 && CONJURE_LEXER_STATIC_TABLES != 0) || \
(CONJURE_LEXER_DYNAMIC_TABLES != 0 && CONJURE_LEXER_STATIC_SWITCH != 0) || \
(CONJURE_LEXER_STATIC_TABLES != 0 && CONJURE_LEXER_STATIC_SWITCH != 0)
#error "Configuration problem: please select exactly one type of lexer to build"
#endif
#endif

View File

@@ -0,0 +1,483 @@
// Copyright (c) 2008-2009 Ben Hanson
// Copyright (c) 2008-2011 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Auto-generated by boost::lexer, do not edit
#if !defined(BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_JUL_25_2011_07_25_53)
#define BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_JUL_25_2011_07_25_53
#include <boost/spirit/home/support/detail/lexer/char_traits.hpp>
////////////////////////////////////////////////////////////////////////////////
// the generated table of state names and the tokenizer have to be
// defined in the boost::spirit::lex::lexertl::static_ namespace
namespace boost { namespace spirit { namespace lex { namespace lexertl { namespace static_ {
////////////////////////////////////////////////////////////////////////////////
// this table defines the names of the lexer states
char const* const lexer_state_names_conjure_static[1] =
{
"INITIAL"
};
////////////////////////////////////////////////////////////////////////////////
// this variable defines the number of lexer states
std::size_t const lexer_state_count_conjure_static = 1;
////////////////////////////////////////////////////////////////////////////////
// this function returns the next matched token
template<typename Iterator>
std::size_t next_token_conjure_static (std::size_t& /*start_state_*/, bool& /*bol_*/,
Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
enum {end_state_index, id_index, unique_id_index, state_index, bol_index,
eol_index, dead_state_index, dfa_offset};
static std::size_t const npos = static_cast<std::size_t>(~0);
static std::size_t const lookup_[256] = {
41, 41, 41, 41, 41, 41, 41, 41,
41, 7, 7, 41, 41, 7, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
7, 8, 41, 41, 41, 41, 9, 41,
10, 11, 12, 13, 14, 15, 41, 16,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 41, 19, 20, 21, 22, 41,
41, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 41, 41, 41, 41, 18,
41, 23, 18, 18, 24, 25, 26, 18,
27, 28, 18, 18, 29, 18, 30, 31,
18, 18, 32, 33, 34, 35, 36, 37,
18, 18, 18, 38, 39, 40, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41,
41, 41, 41, 41, 41, 41, 41, 41 };
static std::size_t const dfa_alphabet_ = 42;
static std::size_t const dfa_[2604] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 27, 13, 11, 20, 21, 18, 16,
24, 17, 19, 2, 26, 25, 14, 12,
15, 26, 26, 7, 4, 26, 6, 26,
26, 26, 9, 26, 3, 26, 5, 8,
22, 10, 23, 0, 1, 35, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 28, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 29,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 30, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 32, 26,
26, 26, 31, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 33, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 34, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 35, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 36, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 37,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 61, 26, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 38, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 262177, 20, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 39,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131091, 12, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 40, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131093,
14, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 41, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 393241, 16, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 393242, 17, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131099, 18, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131100,
19, 0, 0, 0, 0, 0, 0, 0,
0, 0, 42, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 40, 21, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 41, 22, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 123, 23, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 125,
24, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 44, 25, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 59, 27, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 34,
30, 0, 0, 0, 0, 27, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 43, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 44,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
45, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
46, 26, 26, 26, 0, 0, 0, 0,
1, 65538, 4, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 47, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
48, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
49, 26, 26, 26, 0, 0, 0, 0,
1, 131084, 8, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 131085, 9, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 131089, 10, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 131090,
11, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 131092, 13, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 131094, 15, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 42, 42, 42, 42, 42,
50, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 51,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 43, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 52, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 65537, 3, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 53,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 32, 28, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 54, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 32, 28, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 55, 26, 26,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 56, 56, 56, 56, 56,
50, 56, 56, 56, 57, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 1, 36,
1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
1, 65536, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 26, 26, 0, 0, 0, 0, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 0, 0,
0, 0, 1, 65539, 5, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0, 1, 32, 28, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 58, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 26, 26, 59, 26,
26, 26, 26, 26, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 56,
56, 56, 56, 56, 60, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 1, 33, 29, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 65540, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 26, 26, 0,
0, 0, 0, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 0, 0, 0, 0, 1, 32,
28, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 26,
26, 0, 0, 0, 0, 26, 26, 26,
26, 26, 26, 26, 61, 26, 26, 26,
26, 26, 26, 26, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 56,
56, 56, 56, 56, 60, 56, 56, 56,
57, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 56, 56,
56, 56, 1, 65541, 7, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26, 26, 0, 0, 0,
0, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26,
0, 0, 0, 0 };
if (start_token_ == end_)
{
unique_id_ = npos;
return 0;
}
std::size_t const* ptr_ = dfa_ + dfa_alphabet_;
Iterator curr_ = start_token_;
bool end_state_ = *ptr_ != 0;
std::size_t id_ = *(ptr_ + id_index);
std::size_t uid_ = *(ptr_ + unique_id_index);
Iterator end_token_ = start_token_;
while (curr_ != end_)
{
std::size_t const state_ =
ptr_[lookup_[static_cast<unsigned char>(*curr_++)]];
if (state_ == 0) break;
ptr_ = &dfa_[state_ * dfa_alphabet_];
if (*ptr_)
{
end_state_ = true;
id_ = *(ptr_ + id_index);
uid_ = *(ptr_ + unique_id_index);
end_token_ = curr_;
}
}
if (end_state_)
{
// return longest match
start_token_ = end_token_;
}
else
{
id_ = npos;
uid_ = npos;
}
unique_id_ = uid_;
return id_;
}
////////////////////////////////////////////////////////////////////////////////
// this defines a generic accessors for the information above
struct lexer_conjure_static
{
// version number and feature-set of compatible static lexer engine
enum
{
static_version = 65536,
supports_bol = false,
supports_eol = false
};
// return the number of lexer states
static std::size_t state_count()
{
return lexer_state_count_conjure_static;
}
// return the name of the lexer state as given by 'idx'
static char const* state_name(std::size_t idx)
{
return lexer_state_names_conjure_static[idx];
}
// return the next matched token
template<typename Iterator>
static std::size_t next(std::size_t &start_state_, bool& bol_
, Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
return next_token_conjure_static(start_state_, bol_, start_token_, end_, unique_id_);
}
};
}}}}} // namespace boost::spirit::lex::lexertl::static_
#endif

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2001-2011 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)
// This small utility program generates the 2 static lexers, the static table
// driven and the static switch based lexer.
#include <fstream>
#include <iostream>
#include "lexer_def.hpp"
#include <boost/spirit/include/lex_generate_static_lexertl.hpp>
int main()
{
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
lexer_type lexer;
// first generate the static switch based lexer
std::ofstream out_static("conjure_static_switch_lexer.hpp");
bool result = boost::spirit::lex::lexertl::generate_static_switch(
lexer, out_static, "conjure_static_switch");
if (!result) {
std::cerr << "Failed to generate static switch based lexer\n";
return -1;
}
// now generate the static table based lexer
std::ofstream out("conjure_static_lexer.hpp");
result = boost::spirit::lex::lexertl::generate_static(
lexer, out, "conjure_static");
if (!result) {
std::cerr << "Failed to generate static table based lexer\n";
return -1;
}
return 0;
}

View File

@@ -0,0 +1,873 @@
// Copyright (c) 2008-2009 Ben Hanson
// Copyright (c) 2008-2011 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Auto-generated by boost::lexer, do not edit
#if !defined(BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_SWITCH_JUL_25_2011_07_25_53)
#define BOOST_SPIRIT_LEXER_NEXT_TOKEN_CONJURE_STATIC_SWITCH_JUL_25_2011_07_25_53
#include <boost/spirit/home/support/detail/lexer/char_traits.hpp>
////////////////////////////////////////////////////////////////////////////////
// the generated table of state names and the tokenizer have to be
// defined in the boost::spirit::lex::lexertl::static_ namespace
namespace boost { namespace spirit { namespace lex { namespace lexertl { namespace static_ {
////////////////////////////////////////////////////////////////////////////////
// this table defines the names of the lexer states
char const* const lexer_state_names_conjure_static_switch[1] =
{
"INITIAL"
};
////////////////////////////////////////////////////////////////////////////////
// this variable defines the number of lexer states
std::size_t const lexer_state_count_conjure_static_switch = 1;
////////////////////////////////////////////////////////////////////////////////
// this function returns the next matched token
template<typename Iterator>
std::size_t next_token_conjure_static_switch (std::size_t& /*start_state_*/, bool& /*bol_*/,
Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
static std::size_t const npos = static_cast<std::size_t>(~0);
if (start_token_ == end_)
{
unique_id_ = npos;
return 0;
}
Iterator curr_ = start_token_;
bool end_state_ = false;
std::size_t id_ = npos;
std::size_t uid_ = npos;
Iterator end_token_ = start_token_;
char ch_ = 0;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9')) goto state0_1;
if (ch_ == 't') goto state0_2;
if (ch_ == 'f') goto state0_3;
if (ch_ == 'v') goto state0_4;
if (ch_ == 'i') goto state0_5;
if (ch_ == 'e') goto state0_6;
if (ch_ == 'w') goto state0_7;
if (ch_ == 'r') goto state0_8;
if (ch_ == '|') goto state0_9;
if (ch_ == '&') goto state0_10;
if (ch_ == '=') goto state0_11;
if (ch_ == '!') goto state0_12;
if (ch_ == '<') goto state0_13;
if (ch_ == '>') goto state0_14;
if (ch_ == '+') goto state0_15;
if (ch_ == '-') goto state0_16;
if (ch_ == '*') goto state0_17;
if (ch_ == '/') goto state0_18;
if (ch_ == '(') goto state0_19;
if (ch_ == ')') goto state0_20;
if (ch_ == '{') goto state0_21;
if (ch_ == '}') goto state0_22;
if (ch_ == ',') goto state0_23;
if (ch_ == ';') goto state0_24;
if ((ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'g' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'q') || ch_ == 's' || ch_ == 'u' || (ch_ >= 'x' && ch_ <= 'z')) goto state0_25;
if ((ch_ >= '\t' && ch_ <= '\n') || ch_ == '\r' || ch_ == ' ') goto state0_26;
goto end;
state0_1:
end_state_ = true;
id_ = 35;
uid_ = 0;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9')) goto state0_1;
goto end;
state0_2:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'q') || (ch_ >= 's' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'r') goto state0_27;
goto end;
state0_3:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'b' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'a') goto state0_28;
goto end;
state0_4:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'n') || (ch_ >= 'p' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'o') goto state0_29;
goto end;
state0_5:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'e') || (ch_ >= 'g' && ch_ <= 'm') || (ch_ >= 'o' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'n') goto state0_30;
if (ch_ == 'f') goto state0_31;
goto end;
state0_6:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_32;
goto end;
state0_7:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'g') || (ch_ >= 'i' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'h') goto state0_33;
goto end;
state0_8:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_34;
goto end;
state0_9:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '|') goto state0_35;
goto end;
state0_10:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '&') goto state0_36;
goto end;
state0_11:
end_state_ = true;
id_ = 61;
uid_ = 26;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_37;
goto end;
state0_12:
end_state_ = true;
id_ = 262177;
uid_ = 20;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_38;
goto end;
state0_13:
end_state_ = true;
id_ = 131091;
uid_ = 12;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_39;
goto end;
state0_14:
end_state_ = true;
id_ = 131093;
uid_ = 14;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '=') goto state0_40;
goto end;
state0_15:
end_state_ = true;
id_ = 393241;
uid_ = 16;
end_token_ = curr_;
goto end;
state0_16:
end_state_ = true;
id_ = 393242;
uid_ = 17;
end_token_ = curr_;
goto end;
state0_17:
end_state_ = true;
id_ = 131099;
uid_ = 18;
end_token_ = curr_;
goto end;
state0_18:
end_state_ = true;
id_ = 131100;
uid_ = 19;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '*') goto state0_41;
goto end;
state0_19:
end_state_ = true;
id_ = 40;
uid_ = 21;
end_token_ = curr_;
goto end;
state0_20:
end_state_ = true;
id_ = 41;
uid_ = 22;
end_token_ = curr_;
goto end;
state0_21:
end_state_ = true;
id_ = 123;
uid_ = 23;
end_token_ = curr_;
goto end;
state0_22:
end_state_ = true;
id_ = 125;
uid_ = 24;
end_token_ = curr_;
goto end;
state0_23:
end_state_ = true;
id_ = 44;
uid_ = 25;
end_token_ = curr_;
goto end;
state0_24:
end_state_ = true;
id_ = 59;
uid_ = 27;
end_token_ = curr_;
goto end;
state0_25:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_26:
end_state_ = true;
id_ = 34;
uid_ = 30;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '\t' && ch_ <= '\n') || ch_ == '\r' || ch_ == ' ') goto state0_26;
goto end;
state0_27:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 't') || (ch_ >= 'v' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'u') goto state0_42;
goto end;
state0_28:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_43;
goto end;
state0_29:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'i') goto state0_44;
goto end;
state0_30:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 's') || (ch_ >= 'u' && ch_ <= 'z')) goto state0_25;
if (ch_ == 't') goto state0_45;
goto end;
state0_31:
end_state_ = true;
id_ = 65538;
uid_ = 4;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_32:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'r') || (ch_ >= 't' && ch_ <= 'z')) goto state0_25;
if (ch_ == 's') goto state0_46;
goto end;
state0_33:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'h') || (ch_ >= 'j' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'i') goto state0_47;
goto end;
state0_34:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 's') || (ch_ >= 'u' && ch_ <= 'z')) goto state0_25;
if (ch_ == 't') goto state0_48;
goto end;
state0_35:
end_state_ = true;
id_ = 131084;
uid_ = 8;
end_token_ = curr_;
goto end;
state0_36:
end_state_ = true;
id_ = 131085;
uid_ = 9;
end_token_ = curr_;
goto end;
state0_37:
end_state_ = true;
id_ = 131089;
uid_ = 10;
end_token_ = curr_;
goto end;
state0_38:
end_state_ = true;
id_ = 131090;
uid_ = 11;
end_token_ = curr_;
goto end;
state0_39:
end_state_ = true;
id_ = 131092;
uid_ = 13;
end_token_ = curr_;
goto end;
state0_40:
end_state_ = true;
id_ = 131094;
uid_ = 15;
end_token_ = curr_;
goto end;
state0_41:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*') goto state0_41;
if (ch_ == '*') goto state0_49;
goto end;
state0_42:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_50;
goto end;
state0_43:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'r') || (ch_ >= 't' && ch_ <= 'z')) goto state0_25;
if (ch_ == 's') goto state0_42;
goto end;
state0_44:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'c') || (ch_ >= 'e' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'd') goto state0_51;
goto end;
state0_45:
end_state_ = true;
id_ = 65537;
uid_ = 3;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_46:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_52;
goto end;
state0_47:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'k') || (ch_ >= 'm' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'l') goto state0_53;
goto end;
state0_48:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 't') || (ch_ >= 'v' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'u') goto state0_54;
goto end;
state0_49:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ == '*') goto state0_49;
if (ch_ != '*' && ch_ != '/') goto state0_55;
if (ch_ == '/') goto state0_56;
goto end;
state0_50:
end_state_ = true;
id_ = 36;
uid_ = 1;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_51:
end_state_ = true;
id_ = 65536;
uid_ = 2;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_52:
end_state_ = true;
id_ = 65539;
uid_ = 5;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_53:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'd') || (ch_ >= 'f' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'e') goto state0_57;
goto end;
state0_54:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'q') || (ch_ >= 's' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'r') goto state0_58;
goto end;
state0_55:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*') goto state0_55;
if (ch_ == '*') goto state0_59;
goto end;
state0_56:
end_state_ = true;
id_ = 33;
uid_ = 29;
end_token_ = curr_;
goto end;
state0_57:
end_state_ = true;
id_ = 65540;
uid_ = 6;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
goto end;
state0_58:
end_state_ = true;
id_ = 32;
uid_ = 28;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'm') || (ch_ >= 'o' && ch_ <= 'z')) goto state0_25;
if (ch_ == 'n') goto state0_60;
goto end;
state0_59:
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if (ch_ != '*' && ch_ != '/') goto state0_55;
if (ch_ == '/') goto state0_56;
if (ch_ == '*') goto state0_59;
goto end;
state0_60:
end_state_ = true;
id_ = 65541;
uid_ = 7;
end_token_ = curr_;
if (curr_ == end_) goto end;
ch_ = *curr_;
++curr_;
if ((ch_ >= '0' && ch_ <= '9') || (ch_ >= 'A' && ch_ <= 'Z') || ch_ == '_' || (ch_ >= 'a' && ch_ <= 'z')) goto state0_25;
end:
if (end_state_)
{
// return longest match
start_token_ = end_token_;
}
else
{
id_ = npos;
uid_ = npos;
}
unique_id_ = uid_;
return id_;
}
////////////////////////////////////////////////////////////////////////////////
// this defines a generic accessors for the information above
struct lexer_conjure_static_switch
{
// version number and feature-set of compatible static lexer engine
enum
{
static_version = 65536,
supports_bol = false,
supports_eol = false
};
// return the number of lexer states
static std::size_t state_count()
{
return lexer_state_count_conjure_static_switch;
}
// return the name of the lexer state as given by 'idx'
static char const* state_name(std::size_t idx)
{
return lexer_state_names_conjure_static_switch[idx];
}
// return the next matched token
template<typename Iterator>
static std::size_t next(std::size_t &start_state_, bool& bol_
, Iterator &start_token_, Iterator const& end_, std::size_t& unique_id_)
{
return next_token_conjure_static_switch(start_state_, bol_, start_token_, end_, unique_id_);
}
};
}}}}} // namespace boost::spirit::lex::lexertl::static_
#endif

View File

@@ -0,0 +1,99 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_ERROR_HANDLER_HPP)
#define BOOST_SPIRIT_CONJURE_ERROR_HANDLER_HPP
#include <iostream>
#include <string>
#include <vector>
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// The error handler
///////////////////////////////////////////////////////////////////////////////
template <typename BaseIterator, typename Iterator>
struct error_handler
{
template <typename, typename, typename>
struct result { typedef void type; };
error_handler(BaseIterator first, BaseIterator last)
: first(first), last(last) {}
template <typename Message, typename What>
void operator()(
Message const& message,
What const& what,
Iterator err_pos) const
{
// retrieve underlying iterator from current token, err_pos points
// to the last validly matched token, so we use its end iterator
// as the error position
BaseIterator err_pos_base = err_pos->matched().end();
int line;
BaseIterator line_start = get_pos(err_pos_base, line);
if (err_pos_base != last)
{
std::cout << message << what << " line " << line << ':' << std::endl;
std::cout << get_line(line_start) << std::endl;
for (; line_start != err_pos_base; ++line_start)
std::cout << ' ';
std::cout << '^' << std::endl;
}
else
{
std::cout << "Unexpected end of file. ";
std::cout << message << what << " line " << line << std::endl;
}
}
BaseIterator get_pos(BaseIterator err_pos, int& line) const
{
line = 1;
BaseIterator i = first;
BaseIterator 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(BaseIterator err_pos) const
{
BaseIterator i = err_pos;
// position i to the next EOL
while (i != last && (*i != '\r' && *i != '\n'))
++i;
return std::string(err_pos, i);
}
BaseIterator first;
BaseIterator last;
std::vector<Iterator> iters;
};
}
#endif

View File

@@ -0,0 +1,20 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "config.hpp"
#include "lexer.hpp"
#include "expression_def.hpp"
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
typedef lexer_type::iterator_type iterator_type;
template struct client::parser::expression<iterator_type, lexer_type>;

View File

@@ -0,0 +1,59 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_EXPRESSION_HPP)
#define BOOST_SPIRIT_CONJURE_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, typename Lexer>
struct expression : qi::grammar<Iterator, ast::expression()>
{
typedef error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
expression(error_handler_type& error_handler, Lexer const& l);
Lexer const& lexer;
qi::rule<Iterator, ast::expression()> expr;
qi::rule<Iterator, ast::operand()> unary_expr, postfix_expr;
qi::rule<Iterator, ast::function_call()> function_call;
qi::rule<Iterator, std::list<ast::expression>()> argument_list;
qi::rule<Iterator, std::string()> identifier;
qi::rule<Iterator, ast::primary_expr()> primary_expr;
};
}}
#endif

View File

@@ -0,0 +1,104 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "expression.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
#include <boost/phoenix/function.hpp>
#include <boost/spirit/include/lex_plain_token.hpp>
namespace client { namespace parser
{
template <typename Iterator, typename Lexer>
expression<Iterator, Lexer>::expression(
error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
, Lexer const& l)
: expression::base_type(expr), lexer(l)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
qi::tokenid_mask_type tokenid_mask;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
typedef function<error_handler_type> error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
///////////////////////////////////////////////////////////////////////
// Main expression grammar
expr =
unary_expr
>> *(tokenid_mask(token_ids::op_binary) > unary_expr)
;
unary_expr =
postfix_expr
| (tokenid_mask(token_ids::op_unary) > unary_expr)
;
postfix_expr =
function_call
| primary_expr
;
primary_expr =
lexer.lit_uint
| lexer.true_or_false
| identifier
| '(' > expr > ')'
;
function_call =
(identifier >> '(')
> argument_list
> ')'
;
argument_list = -(expr % ',');
identifier = lexer.identifier;
///////////////////////////////////////////////////////////////////////
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(expr)
(unary_expr)
(postfix_expr)
(primary_expr)
(function_call)
(argument_list)
(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 unary_expr, postfix_expr,
// and primary_expr call annotation.
on_success(unary_expr,
annotation_function(error_handler.iters)(_val, _1));
on_success(postfix_expr,
annotation_function(error_handler.iters)(_val, _1));
on_success(primary_expr,
annotation_function(error_handler.iters)(_val, _1));
}
}}

View File

@@ -0,0 +1,20 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if defined(_MSC_VER)
# pragma warning(disable: 4345)
#endif
#include "config.hpp"
#include "lexer.hpp"
#include "function_def.hpp"
typedef std::string::const_iterator base_iterator_type;
typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
typedef lexer_type::iterator_type iterator_type;
template struct client::parser::function<iterator_type, lexer_type>;

View File

@@ -0,0 +1,36 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#if !defined(BOOST_SPIRIT_CONJURE_FUNCTION_HPP)
#define BOOST_SPIRIT_CONJURE_FUNCTION_HPP
#include "statement.hpp"
namespace client { namespace parser
{
///////////////////////////////////////////////////////////////////////////////
// The function grammar
///////////////////////////////////////////////////////////////////////////////
template <typename Iterator, typename Lexer>
struct function : qi::grammar<Iterator, ast::function()>
{
typedef error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
function(error_handler_type& error_handler, Lexer const& l);
statement<Iterator, Lexer> body;
qi::rule<Iterator, ast::identifier()> identifier;
qi::rule<Iterator, std::list<ast::identifier>()> argument_list;
qi::rule<Iterator, ast::function()> start;
};
}}
#endif

View File

@@ -0,0 +1,64 @@
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Copyright (c) 2001-2011 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)
=============================================================================*/
#include "function.hpp"
#include "error_handler.hpp"
#include "annotation.hpp"
namespace client { namespace parser
{
template <typename Iterator, typename Lexer>
function<Iterator, Lexer>::function(
error_handler<typename Lexer::base_iterator_type, Iterator>& error_handler
, Lexer const& l)
: function::base_type(start), body(error_handler, l)
{
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
qi::_4_type _4;
qi::_val_type _val;
using qi::on_error;
using qi::on_success;
using qi::fail;
using boost::phoenix::function;
typedef client::error_handler<typename Lexer::base_iterator_type, Iterator>
error_handler_type;
typedef function<error_handler_type> error_handler_function;
typedef function<client::annotation<Iterator> > annotation_function;
identifier = body.expr.identifier;
argument_list = -(identifier % ',');
start = (l.token("void") | l.token("int"))
> identifier
> '(' > argument_list > ')'
> (';' | '{' > body > '}')
;
// Debugging and error handling and reporting support.
BOOST_SPIRIT_DEBUG_NODES(
(identifier)
(argument_list)
(start)
);
// Error handling: on error in start, call error_handler.
on_error<fail>(start,
error_handler_function(error_handler)(
"Error! Expecting ", _4, _3));
// Annotation: on success in start, call annotation.
on_success(identifier,
annotation_function(error_handler.iters)(_val, _1));
}
}}

Some files were not shown because too many files have changed in this diff Show More