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