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
main()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 client::vmachine vm; // Our virtual machine
61 client::code_gen::program program; // Our VM program
62 client::ast::statement_list ast; // Our AST
63
64 using boost::spirit::x3::with;
65 using client::parser::error_handler_type;
66 using client::parser::error_handler_tag;
67 error_handler_type error_handler(iter, end, std::cerr); // Our error handler
68
69 // Our compiler
70 client::code_gen::compiler compile(program, error_handler);
71
72 // Our parser
73 auto const parser =
74 // we pass our error handler to the parser so we can access
75 // it later on in our on_error and on_sucess handlers
76 with<error_handler_tag>(std::ref(error_handler))
77 [
78 client::statement()
79 ];
80
81 using boost::spirit::x3::ascii::space;
82 bool success = phrase_parse(iter, end, parser, space, ast);
83
84 std::cout << "-------------------------\n";
85
86 if (success && iter == end)
87 {
88 if (compile(ast))
89 {
90 std::cout << "Success\n";
91 std::cout << "-------------------------\n";
92 vm.execute(program());
93
94 std::cout << "-------------------------\n";
95 std::cout << "Assembler----------------\n\n";
96 program.print_assembler();
97
98 std::cout << "-------------------------\n";
99 std::cout << "Results------------------\n\n";
100 program.print_variables(vm.get_stack());
101 }
102 else
103 {
104 std::cout << "Compile failure\n";
105 }
106 }
107 else
108 {
109 std::cout << "Parse failure\n";
110 }
111
112 std::cout << "-------------------------\n\n";
113 return 0;
114 }
115