1/*=============================================================================
2 Copyright (c) 2001-2011 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//
15///////////////////////////////////////////////////////////////////////////////
16
17#include "statement.hpp"
18#include "vm.hpp"
19#include "compiler.hpp"
20
21///////////////////////////////////////////////////////////////////////////////
22// Main program
23///////////////////////////////////////////////////////////////////////////////
24int
25main()
26{
27 std::cout << "/////////////////////////////////////////////////////////\n\n";
28 std::cout << "Statement parser...\n\n";
29 std::cout << "/////////////////////////////////////////////////////////\n\n";
30 std::cout << "Type some statements... ";
31 std::cout << "An empty line ends input, compiles, runs and prints results\n\n";
32 std::cout << "Example:\n\n";
33 std::cout << " var a = 123;\n";
34 std::cout << " var b = 456;\n";
35 std::cout << " var c = a + b * 2;\n\n";
36 std::cout << "-------------------------\n";
37
38 std::string str;
39 std::string source;
40 while (std::getline(is&: std::cin, str&: str))
41 {
42 if (str.empty())
43 break;
44 source += str + '\n';
45 }
46
47 typedef std::string::const_iterator iterator_type;
48 iterator_type iter = source.begin();
49 iterator_type end = source.end();
50
51 client::vmachine vm; // Our virtual machine
52 client::code_gen::program program; // Our VM program
53 client::ast::statement_list ast; // Our AST
54
55 client::error_handler<iterator_type>
56 error_handler(iter, end); // Our error handler
57 client::parser::statement<iterator_type>
58 parser(error_handler); // Our parser
59 client::code_gen::compiler
60 compile(program, error_handler); // Our compiler
61
62 boost::spirit::ascii::space_type space;
63 bool success = phrase_parse(first&: iter, last: end, expr: parser, skipper: space, attr&: ast);
64
65 std::cout << "-------------------------\n";
66
67 if (success && iter == end)
68 {
69 if (compile.start(x: ast))
70 {
71 std::cout << "Success\n";
72 std::cout << "-------------------------\n";
73 vm.execute(code: program());
74
75 std::cout << "-------------------------\n";
76 std::cout << "Assembler----------------\n\n";
77 program.print_assembler();
78
79 std::cout << "-------------------------\n";
80 std::cout << "Results------------------\n\n";
81 program.print_variables(stack: vm.stack);
82 }
83 else
84 {
85 std::cout << "Compile failure\n";
86 }
87 }
88 else
89 {
90 std::cout << "Parse failure\n";
91 }
92
93 std::cout << "-------------------------\n\n";
94 return 0;
95}
96
97
98

source code of boost/libs/spirit/example/qi/compiler_tutorial/calc8/main.cpp