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 //  Same as calc6, but this version also shows off grammar modularization.
10 //  Here you will see how expressions is built as a modular grammars.
11 //
12 //  [ JDG Sometime 2000 ]       pre-boost
13 //  [ JDG September 18, 2002 ]  spirit1
14 //  [ JDG April 8, 2007 ]       spirit2
15 //  [ JDG February 18, 2011 ]   Pure attributes. No semantic actions.
16 //  [ JDG April 9, 2014 ]       Spirit X3 (from qi calc6)
17 //  [ JDG May 2, 2014 ]         Modular grammar using BOOST_SPIRIT_DEFINE.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20 
21 #include "ast.hpp"
22 #include "vm.hpp"
23 #include "compiler.hpp"
24 #include "expression.hpp"
25 #include "error_handler.hpp"
26 
27 ///////////////////////////////////////////////////////////////////////////////
28 //  Main program
29 ///////////////////////////////////////////////////////////////////////////////
30 int
main()31 main()
32 {
33     std::cout << "/////////////////////////////////////////////////////////\n\n";
34     std::cout << "Expression parser...\n\n";
35     std::cout << "/////////////////////////////////////////////////////////\n\n";
36     std::cout << "Type an expression...or [q or Q] to quit\n\n";
37 
38     typedef std::string::const_iterator iterator_type;
39     typedef client::ast::expression ast_expression;
40     typedef client::compiler compiler;
41 
42     std::string str;
43     while (std::getline(std::cin, str))
44     {
45         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
46             break;
47 
48         using boost::spirit::x3::ascii::space;
49 
50         client::vmachine mach;                          // Our virtual machine
51         std::vector<int> code;                          // Our VM code
52         auto calc = client::expression();               // grammar
53 
54         ast_expression ast;                             // Our program (AST)
55         compiler compile(code);                         // Compiles the program
56 
57         iterator_type iter = str.begin();
58         iterator_type const end = str.end();
59         bool r = phrase_parse(iter, end, calc, space, ast);
60 
61         if (r && iter == end)
62         {
63             std::cout << "-------------------------\n";
64             std::cout << "Parsing succeeded\n";
65             compile(ast);
66             mach.execute(code);
67             std::cout << "\nResult: " << mach.top() << std::endl;
68             std::cout << "-------------------------\n";
69         }
70         else
71         {
72             std::string rest(iter, end);
73             std::cout << "-------------------------\n";
74             std::cout << "Parsing failed\n";
75             std::cout << "-------------------------\n";
76         }
77     }
78 
79     std::cout << "Bye... :-) \n\n";
80     return 0;
81 }
82