]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/calc/calc9/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 boolean expressions and control structures.
10 // Is it obvious now what we are up to? ;-)
12 // [ JDG April 9, 2007 ] spirit2
13 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
14 // [ JDG June 6, 2014 ] Ported from qi calc8 example.
16 ///////////////////////////////////////////////////////////////////////////////
20 #include "compiler.hpp"
21 #include "statement.hpp"
22 #include "error_handler.hpp"
26 ///////////////////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////////////////
32 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
33 std::cout
<< "Statement parser...\n\n";
34 std::cout
<< "/////////////////////////////////////////////////////////\n\n";
35 std::cout
<< "Type some statements... ";
36 std::cout
<< "An empty line ends input, compiles, runs and prints results\n\n";
37 std::cout
<< "Example:\n\n";
38 std::cout
<< " var a = 123;\n";
39 std::cout
<< " var b = 456;\n";
40 std::cout
<< " var c = a + b * 2;\n\n";
41 std::cout
<< "-------------------------\n";
45 while (std::getline(std::cin
, str
))
52 using client::parser::iterator_type
;
53 iterator_type
iter(source
.begin());
54 iterator_type
end(source
.end());
57 client::vmachine vm
; // Our virtual machine
58 client::code_gen::program program
; // Our VM program
59 client::ast::statement_list ast
; // Our AST
61 using boost::spirit::x3::with
;
62 using client::parser::error_handler_type
;
63 error_handler_type
error_handler(iter
, end
, std::cerr
); // Our error handler
66 client::code_gen::compiler
compile(program
, error_handler
);
70 // we pass our error handler to the parser so we can access
71 // it later on in our on_error and on_sucess handlers
72 with
<client::parser::error_handler_tag
>(std::ref(error_handler
))
77 using boost::spirit::x3::ascii::space
;
78 bool success
= phrase_parse(iter
, end
, parser
, space
, ast
);
80 std::cout
<< "-------------------------\n";
82 if (success
&& iter
== end
)
84 if (compile
.start(ast
))
86 std::cout
<< "Success\n";
87 std::cout
<< "-------------------------\n";
88 vm
.execute(program());
90 std::cout
<< "-------------------------\n";
91 std::cout
<< "Assembler----------------\n\n";
92 program
.print_assembler();
94 std::cout
<< "-------------------------\n";
95 std::cout
<< "Results------------------\n\n";
96 program
.print_variables(vm
.get_stack());
100 std::cout
<< "Compile failure\n";
105 std::cout
<< "Parse failure\n";
108 std::cout
<< "-------------------------\n\n";