1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
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
13 // This version also shows off grammar modularization. Here you will
14 // see how expressions and statements are built as modular grammars.
16 // [ JDG April 9, 2007 ] spirit2
17 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
19 ///////////////////////////////////////////////////////////////////////////////
21 #include "statement.hpp"
23 #include "compiler.hpp"
25 ///////////////////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////////////////
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";
44 while (std::getline(std::cin
, str
))
51 typedef std::string::const_iterator iterator_type
;
52 iterator_type iter
= source
.begin();
53 iterator_type end
= source
.end();
55 client::vmachine vm
; // Our virtual machine
56 client::code_gen::program program
; // Our VM program
57 client::ast::statement_list ast
; // Our AST
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
66 boost::spirit::ascii::space_type space
;
67 bool success
= phrase_parse(iter
, end
, parser
, space
, ast
);
69 std::cout
<< "-------------------------\n";
71 if (success
&& iter
== end
)
75 std::cout
<< "Success\n";
76 std::cout
<< "-------------------------\n";
77 vm
.execute(program());
79 std::cout
<< "-------------------------\n";
80 std::cout
<< "Assembler----------------\n\n";
81 program
.print_assembler();
83 std::cout
<< "-------------------------\n";
84 std::cout
<< "Results------------------\n\n";
85 program
.print_variables(vm
.get_stack());
89 std::cout
<< "Compile failure\n";
94 std::cout
<< "Parse failure\n";
97 std::cout
<< "-------------------------\n\n";