Advertisement
JorgeNavalon

Brainfuck Hello World - metaparse

Jul 25th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. // Based on https://github.com/sabel83/mpllibs/blob/master/libs/metaparse/example/compile_to_native_code/main.cpp
  2. // Copyright Abel Sinkovics (abel@sinkovics.hu)  2012.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. //    (See accompanying file LICENSE_1_0.txt or copy at
  5. //          http://www.boost.org/LICENSE_1_0.txt)
  6.  
  7. #define MPLLIBS_STRING_MAX_LENGTH 128
  8.  
  9. #include <vector>
  10. #include <cstring>
  11. #include <cstdio>
  12.  
  13. #include <mpllibs/metaparse/any.hpp>
  14. #include <mpllibs/metaparse/lit_c.hpp>
  15. #include <mpllibs/metaparse/last_of.hpp>
  16. #include <mpllibs/metaparse/middle_of.hpp>
  17. #include <mpllibs/metaparse/space.hpp>
  18. #include <mpllibs/metaparse/foldlp.hpp>
  19. #include <mpllibs/metaparse/one_of.hpp>
  20. #include <mpllibs/metaparse/token.hpp>
  21. #include <mpllibs/metaparse/entire_input.hpp>
  22. #include <mpllibs/metaparse/string.hpp>
  23. #include <mpllibs/metaparse/transform.hpp>
  24. #include <mpllibs/metaparse/always.hpp>
  25. #include <mpllibs/metaparse/build_parser.hpp>
  26.  
  27. #include <boost/mpl/apply_wrap.hpp>
  28.  
  29. #include <boost/proto/proto.hpp>
  30.  
  31. using mpllibs::metaparse::lit_c;
  32. using mpllibs::metaparse::last_of;
  33. using mpllibs::metaparse::middle_of;
  34. using mpllibs::metaparse::space;
  35. using mpllibs::metaparse::any;
  36. using mpllibs::metaparse::build_parser;
  37. using mpllibs::metaparse::foldlp;
  38. using mpllibs::metaparse::one_of;
  39. using mpllibs::metaparse::token;
  40. using mpllibs::metaparse::entire_input;
  41. using mpllibs::metaparse::transform;
  42. using mpllibs::metaparse::always;
  43.  
  44. using boost::mpl::apply_wrap1;
  45.  
  46. struct while_tag{};
  47.  
  48. struct build_while
  49. {
  50.     typedef build_while type;
  51.    
  52.     template <typename Expr>
  53.     struct apply
  54.     {
  55.         typedef apply type;
  56.         typedef typename boost::proto::unary_expr< while_tag, typename Expr::proto_type >::type proto_type;
  57.     };
  58. };
  59.  
  60. struct build_sequence
  61. {
  62.     typedef build_sequence type;
  63.  
  64.     template <class C, class State>
  65.     struct apply
  66.     {
  67.         typedef apply type;
  68.         typedef typename boost::proto::comma<typename State::proto_type, typename C::proto_type >::type proto_type;
  69.     };
  70. };
  71.  
  72. template <typename T>
  73. struct value
  74. {
  75.     typedef value type;
  76.     typedef typename boost::proto::terminal<T>::type proto_type;
  77. };
  78.  
  79.  
  80.  
  81. #ifdef _S
  82. #error _S already defined
  83. #endif
  84. #define _S MPLLIBS_STRING
  85.  
  86. struct advance{};
  87. struct move_back{};
  88. struct increment{};
  89. struct decrement{};
  90. struct put_char{};
  91. struct get_char{};
  92.  
  93.  
  94.  
  95. typedef token < lit_c<'>'> > advance_token;
  96. typedef token < lit_c<'<'> > move_back_token;
  97. typedef token < lit_c<'+'> > increment_token;
  98. typedef token < lit_c<'-'> > decrement_token;
  99. typedef token < lit_c<'.'> > put_char_token;
  100. typedef token < lit_c<','> > get_char_token;
  101. typedef token < lit_c<'['> > while_open_token;
  102. typedef token < lit_c<']'> > while_close_token;
  103.  
  104.  
  105.  
  106.  
  107. typedef
  108. one_of<
  109.         always<advance_token, value<advance> >,
  110.         always<move_back_token, value<move_back> >,
  111.         always<increment_token, value<increment> >,
  112.         always<decrement_token, value<decrement> >,
  113.         always<put_char_token, value<put_char> >,
  114.         always<get_char_token, value<get_char> >
  115. >
  116. command;
  117.  
  118. struct list_of_commands;
  119.  
  120. typedef
  121. transform< middle_of<while_open_token, list_of_commands, while_close_token>, build_while >
  122. while_expr;
  123.  
  124. struct list_of_commands : foldlp <one_of<command, while_expr>, command, build_sequence>{};
  125.  
  126. typedef last_of<any<space>, list_of_commands> expression;
  127.  
  128. typedef build_parser<entire_input<expression> > function_parser;
  129.  
  130.  
  131. struct brainfuck_context
  132.         : boost::proto::callable_context< brainfuck_context const >
  133. {
  134.     brainfuck_context ( char** ptr_) : ptr( ptr_ ) {}
  135.  
  136.     char ** ptr;
  137.  
  138.     typedef void result_type;
  139.  
  140.     void operator() ( boost::proto::tag::terminal, advance) const
  141.     {
  142.        ++*ptr;
  143.     }
  144.    
  145.     void operator() ( boost::proto::tag::terminal, move_back) const
  146.     {
  147.        --*ptr;
  148.     }
  149.    
  150.     void operator() ( boost::proto::tag::terminal, increment) const
  151.     {
  152.        ++**ptr;
  153.     }
  154.    
  155.     void operator() ( boost::proto::tag::terminal, decrement) const
  156.     {
  157.        --**ptr;
  158.     }
  159.    
  160.     void operator() ( boost::proto::tag::terminal, put_char) const
  161.     {
  162.        std::putchar(**ptr);
  163.     }
  164.    
  165.     void operator() ( boost::proto::tag::terminal, get_char) const
  166.     {
  167.        **ptr=std::getchar();
  168.     }
  169.    
  170.     template <typename Expr>
  171.     void operator() ( while_tag, const Expr& expr ) const
  172.     {
  173.        while(**ptr)
  174.             boost::proto::eval(expr,*this);
  175.     }
  176. };
  177.  
  178.  
  179.  
  180. #ifdef LAMBDA
  181. #error LAMBDA already defined
  182. #endif
  183. #define LAMBDA(exp) apply_wrap1<function_parser, MPLLIBS_STRING(exp)>::type::proto_type()
  184.  
  185. int main()
  186. {
  187.     char array[30000]={};
  188.     char *ptr=array;
  189.     brainfuck_context ctx(&ptr);
  190.  
  191.     boost::proto::eval( LAMBDA ( "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." ), ctx );  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement