]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/x3/calc/calc8/main.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / example / x3 / calc / calc8 / 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 variables and assignment. This time, we'll also
10 // be renaming some of the rules -- a strategy for a grander scheme
11 // to come ;-)
12 //
13 // This version also shows off grammar modularization. Here you will
14 // see how expressions and statements are built as modular grammars.
15 //
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.
19 //
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "ast.hpp"
23 #include "vm.hpp"
24 #include "compiler.hpp"
25 #include "statement.hpp"
26 #include "error_handler.hpp"
27 #include "config.hpp"
28 #include <iostream>
29
30 ///////////////////////////////////////////////////////////////////////////////
31 // Main program
32 ///////////////////////////////////////////////////////////////////////////////
33 int
34 main()
35 {
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";
46
47 std::string str;
48 std::string source;
49 while (std::getline(std::cin, str))
50 {
51 if (str.empty())
52 break;
53 source += str + '\n';
54 }
55
56 using client::parser::iterator_type;
57 iterator_type iter(source.begin());
58 iterator_type end(source.end());
59
60
61 client::vmachine vm; // Our virtual machine
62 client::code_gen::program program; // Our VM program
63 client::ast::statement_list ast; // Our AST
64
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
69
70 // Our compiler
71 client::code_gen::compiler compile(program, error_handler);
72
73 // Our parser
74 auto const parser =
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))
78 [
79 client::statement()
80 ];
81
82 using boost::spirit::x3::ascii::space;
83 bool success = phrase_parse(iter, end, parser, space, ast);
84
85 std::cout << "-------------------------\n";
86
87 if (success && iter == end)
88 {
89 if (compile(ast))
90 {
91 std::cout << "Success\n";
92 std::cout << "-------------------------\n";
93 vm.execute(program());
94
95 std::cout << "-------------------------\n";
96 std::cout << "Assembler----------------\n\n";
97 program.print_assembler();
98
99 std::cout << "-------------------------\n";
100 std::cout << "Results------------------\n\n";
101 program.print_variables(vm.get_stack());
102 }
103 else
104 {
105 std::cout << "Compile failure\n";
106 }
107 }
108 else
109 {
110 std::cout << "Parse failure\n";
111 }
112
113 std::cout << "-------------------------\n\n";
114 return 0;
115 }