Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Based on https://github.com/sabel83/mpllibs/blob/master/libs/metaparse/example/compile_to_native_code/main.cpp
- // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
- // 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)
- #define MPLLIBS_STRING_MAX_LENGTH 128
- #include <vector>
- #include <cstring>
- #include <cstdio>
- #include <mpllibs/metaparse/any.hpp>
- #include <mpllibs/metaparse/lit_c.hpp>
- #include <mpllibs/metaparse/last_of.hpp>
- #include <mpllibs/metaparse/middle_of.hpp>
- #include <mpllibs/metaparse/space.hpp>
- #include <mpllibs/metaparse/foldlp.hpp>
- #include <mpllibs/metaparse/one_of.hpp>
- #include <mpllibs/metaparse/token.hpp>
- #include <mpllibs/metaparse/entire_input.hpp>
- #include <mpllibs/metaparse/string.hpp>
- #include <mpllibs/metaparse/transform.hpp>
- #include <mpllibs/metaparse/always.hpp>
- #include <mpllibs/metaparse/build_parser.hpp>
- #include <boost/mpl/apply_wrap.hpp>
- #include <boost/proto/proto.hpp>
- using mpllibs::metaparse::lit_c;
- using mpllibs::metaparse::last_of;
- using mpllibs::metaparse::middle_of;
- using mpllibs::metaparse::space;
- using mpllibs::metaparse::any;
- using mpllibs::metaparse::build_parser;
- using mpllibs::metaparse::foldlp;
- using mpllibs::metaparse::one_of;
- using mpllibs::metaparse::token;
- using mpllibs::metaparse::entire_input;
- using mpllibs::metaparse::transform;
- using mpllibs::metaparse::always;
- using boost::mpl::apply_wrap1;
- struct while_tag{};
- struct build_while
- {
- typedef build_while type;
- template <typename Expr>
- struct apply
- {
- typedef apply type;
- typedef typename boost::proto::unary_expr< while_tag, typename Expr::proto_type >::type proto_type;
- };
- };
- struct build_sequence
- {
- typedef build_sequence type;
- template <class C, class State>
- struct apply
- {
- typedef apply type;
- typedef typename boost::proto::comma<typename State::proto_type, typename C::proto_type >::type proto_type;
- };
- };
- template <typename T>
- struct value
- {
- typedef value type;
- typedef typename boost::proto::terminal<T>::type proto_type;
- };
- #ifdef _S
- #error _S already defined
- #endif
- #define _S MPLLIBS_STRING
- struct advance{};
- struct move_back{};
- struct increment{};
- struct decrement{};
- struct put_char{};
- struct get_char{};
- typedef token < lit_c<'>'> > advance_token;
- typedef token < lit_c<'<'> > move_back_token;
- typedef token < lit_c<'+'> > increment_token;
- typedef token < lit_c<'-'> > decrement_token;
- typedef token < lit_c<'.'> > put_char_token;
- typedef token < lit_c<','> > get_char_token;
- typedef token < lit_c<'['> > while_open_token;
- typedef token < lit_c<']'> > while_close_token;
- typedef
- one_of<
- always<advance_token, value<advance> >,
- always<move_back_token, value<move_back> >,
- always<increment_token, value<increment> >,
- always<decrement_token, value<decrement> >,
- always<put_char_token, value<put_char> >,
- always<get_char_token, value<get_char> >
- >
- command;
- struct list_of_commands;
- typedef
- transform< middle_of<while_open_token, list_of_commands, while_close_token>, build_while >
- while_expr;
- struct list_of_commands : foldlp <one_of<command, while_expr>, command, build_sequence>{};
- typedef last_of<any<space>, list_of_commands> expression;
- typedef build_parser<entire_input<expression> > function_parser;
- struct brainfuck_context
- : boost::proto::callable_context< brainfuck_context const >
- {
- brainfuck_context ( char** ptr_) : ptr( ptr_ ) {}
- char ** ptr;
- typedef void result_type;
- void operator() ( boost::proto::tag::terminal, advance) const
- {
- ++*ptr;
- }
- void operator() ( boost::proto::tag::terminal, move_back) const
- {
- --*ptr;
- }
- void operator() ( boost::proto::tag::terminal, increment) const
- {
- ++**ptr;
- }
- void operator() ( boost::proto::tag::terminal, decrement) const
- {
- --**ptr;
- }
- void operator() ( boost::proto::tag::terminal, put_char) const
- {
- std::putchar(**ptr);
- }
- void operator() ( boost::proto::tag::terminal, get_char) const
- {
- **ptr=std::getchar();
- }
- template <typename Expr>
- void operator() ( while_tag, const Expr& expr ) const
- {
- while(**ptr)
- boost::proto::eval(expr,*this);
- }
- };
- #ifdef LAMBDA
- #error LAMBDA already defined
- #endif
- #define LAMBDA(exp) apply_wrap1<function_parser, MPLLIBS_STRING(exp)>::type::proto_type()
- int main()
- {
- char array[30000]={};
- char *ptr=array;
- brainfuck_context ctx(&ptr);
- boost::proto::eval( LAMBDA ( "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." ), ctx );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement