]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/calc/calc9/main.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / calc / calc9 / main.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3
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 ///////////////////////////////////////////////////////////////////////////////
8 //
9 // Now we'll introduce boolean expressions and control structures.
10 // Is it obvious now what we are up to? ;-)
11 //
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.
15 //
16 ///////////////////////////////////////////////////////////////////////////////
17
18 #include "ast.hpp"
19 #include "vm.hpp"
20 #include "compiler.hpp"
21 #include "statement.hpp"
22 #include "error_handler.hpp"
23 #include "config.hpp"
24 #include <iostream>
25
26 ///////////////////////////////////////////////////////////////////////////////
27 // Main program
28 ///////////////////////////////////////////////////////////////////////////////
29 int
30 main()
31 {
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";
42
43 std::string str;
44 std::string source;
45 while (std::getline(std::cin, str))
46 {
47 if (str.empty())
48 break;
49 source += str + '\n';
50 }
51
52 using client::parser::iterator_type;
53 iterator_type iter(source.begin());
54 iterator_type end(source.end());
55
56
57 client::vmachine vm; // Our virtual machine
58 client::code_gen::program program; // Our VM program
59 client::ast::statement_list ast; // Our AST
60
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
64
65 // Our compiler
66 client::code_gen::compiler compile(program, error_handler);
67
68 // Our parser
69 auto const parser =
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))
73 [
74 client::statement()
75 ];
76
77 using boost::spirit::x3::ascii::space;
78 bool success = phrase_parse(iter, end, parser, space, ast);
79
80 std::cout << "-------------------------\n";
81
82 if (success && iter == end)
83 {
84 if (compile.start(ast))
85 {
86 std::cout << "Success\n";
87 std::cout << "-------------------------\n";
88 vm.execute(program());
89
90 std::cout << "-------------------------\n";
91 std::cout << "Assembler----------------\n\n";
92 program.print_assembler();
93
94 std::cout << "-------------------------\n";
95 std::cout << "Results------------------\n\n";
96 program.print_variables(vm.get_stack());
97 }
98 else
99 {
100 std::cout << "Compile failure\n";
101 }
102 }
103 else
104 {
105 std::cout << "Parse failure\n";
106 }
107
108 std::cout << "-------------------------\n\n";
109 return 0;
110 }