1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 ///////////////////////////////////////////////////////////////////////////////
10 // Not a calculator anymore, right? :-)
12 // [ JDG April 10, 2007 ] spirit2
13 // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
14 // [ HK June 3, 2011 ] Adding lexer
16 ///////////////////////////////////////////////////////////////////////////////
19 #include "function.hpp"
21 #include "compiler.hpp"
23 #include <boost/lexical_cast.hpp>
26 ///////////////////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////////////////
29 int main(int argc
, char **argv
)
38 std::cerr
<< "Error: No input file provided." << std::endl
;
42 std::ifstream
in(filename
, std::ios_base::in
);
46 std::cerr
<< "Error: Could not open input file: "
47 << filename
<< std::endl
;
51 std::string source_code
; // We will read the contents here.
52 in
.unsetf(std::ios::skipws
); // No white space skipping!
54 std::istream_iterator
<char>(in
),
55 std::istream_iterator
<char>(),
56 std::back_inserter(source_code
));
58 typedef std::string::const_iterator base_iterator_type
;
59 typedef client::lexer::conjure_tokens
<base_iterator_type
> lexer_type
;
60 typedef lexer_type::iterator_type iterator_type
;
62 lexer_type lexer
; // Our lexer
64 base_iterator_type first
= source_code
.begin();
65 base_iterator_type last
= source_code
.end();
67 iterator_type iter
= lexer
.begin(first
, last
);
68 iterator_type end
= lexer
.end();
70 client::vmachine vm
; // Our virtual machine
71 client::ast::function_list ast
; // Our AST
73 client::error_handler
<base_iterator_type
, iterator_type
>
74 error_handler(first
, last
); // Our error handler
75 client::parser::function
<iterator_type
, lexer_type
>
76 function(error_handler
, lexer
); // Our parser
77 client::code_gen::compiler
78 compiler(error_handler
); // Our compiler
80 // note: we don't need a skipper
81 bool success
= parse(iter
, end
, +function
, ast
);
83 std::cout
<< "-------------------------\n";
85 if (success
&& iter
== end
)
89 boost::shared_ptr
<client::code_gen::function
>
90 p
= compiler
.find_function("main");
95 if (p
->nargs() != nargs
)
97 std::cerr
<< "Error: main function requires " << p
->nargs() << " arguments." << std::endl
;
98 std::cerr
<< nargs
<< " supplied." << std::endl
;
102 std::cout
<< "Success\n";
103 std::cout
<< "-------------------------\n";
104 std::cout
<< "Assembler----------------\n\n";
105 compiler
.print_assembler();
107 // Push the arguments into our stack
108 for (int i
= 0; i
< nargs
; ++i
)
109 vm
.get_stack()[i
] = boost::lexical_cast
<int>(argv
[i
+2]);
111 // Call the interpreter
112 int r
= vm
.execute(compiler
.get_code());
114 std::cout
<< "-------------------------\n";
115 std::cout
<< "Result: " << r
<< std::endl
;
116 std::cout
<< "-------------------------\n\n";
120 std::cout
<< "Compile failure\n";
125 error_handler
.dump_error_line(first
);
126 std::cout
<< "Parse failure\n";