]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 ///////////////////////////////////////////////////////////////////////////////
9 // Now we'll introduce variables and assignment. This time, we'll also
10 // be renaming some of the rules -- a strategy for a grander scheme
13 // This version also shows off grammar modularization. Here you will
14 // see how expressions and statements are built as modular grammars.
16 // [ JDG April 9, 2007 ] spirit2
17 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
18 // [ JDG May 17, 2014 ] Ported from qi calc7 example.
20 ///////////////////////////////////////////////////////////////////////////////
24 #include "compiler.hpp"
25 #include "statement.hpp"
26 #include "error_handler.hpp"
30 ///////////////////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////////
36 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
37 std::cout
<< "Statement parser...\n\n";
38 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
39 std::cout
<< "Type some statements... ";
40 std::cout
<< "An empty line ends input, compiles, runs and prints results\n\n";
41 std::cout
<< "Example:\n\n";
42 std::cout
<< " var a = 123;\n";
43 std::cout
<< " var b = 456;\n";
44 std::cout
<< " var c = a + b * 2;\n\n";
45 std::cout
<< "-------------------------\n";
49 while (std::getline(std::cin
, str
))
56 using client::parser::iterator_type
;
57 iterator_type
iter(source
.begin());
58 iterator_type
end(source
.end());
61 client::vmachine vm
; // Our virtual machine
62 client::code_gen::program program
; // Our VM program
63 client::ast::statement_list ast
; // Our AST
65 using boost::spirit::x3::with
;
66 using client::parser::error_handler_type
;
67 using client::parser::error_handler_tag
;
68 error_handler_type
error_handler(iter
, end
, std::cerr
); // Our error handler
71 client::code_gen::compiler
compile(program
, error_handler
);
75 // we pass our error handler to the parser so we can access
76 // it later on in our on_error and on_sucess handlers
77 with
<error_handler_tag
>(std::ref(error_handler
))
82 using boost::spirit::x3::ascii::space
;
83 bool success
= phrase_parse(iter
, end
, parser
, space
, ast
);
85 std::cout
<< "-------------------------\n";
87 if (success
&& iter
== end
)
91 std::cout
<< "Success\n";
92 std::cout
<< "-------------------------\n";
93 vm
.execute(program());
95 std::cout
<< "-------------------------\n";
96 std::cout
<< "Assembler----------------\n\n";
97 program
.print_assembler();
99 std::cout
<< "-------------------------\n";
100 std::cout
<< "Results------------------\n\n";
101 program
.print_variables(vm
.get_stack());
105 std::cout
<< "Compile failure\n";
110 std::cout
<< "Parse failure\n";
113 std::cout
<< "-------------------------\n\n";