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
15 // [ JDG July 18, 2011 ] Switching to LLVM backend
17 ///////////////////////////////////////////////////////////////////////////////
20 #include "function.hpp"
22 #include "compiler.hpp"
24 #include <boost/lexical_cast.hpp>
27 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
30 int main(int argc
, char **argv
)
39 std::cerr
<< "Error: No input file provided." << std::endl
;
43 std::ifstream
in(filename
, std::ios_base::in
);
47 std::cerr
<< "Error: Could not open input file: "
48 << filename
<< std::endl
;
52 std::string source_code
; // We will read the contents here.
53 in
.unsetf(std::ios::skipws
); // No white space skipping!
55 std::istream_iterator
<char>(in
),
56 std::istream_iterator
<char>(),
57 std::back_inserter(source_code
));
59 typedef std::string::const_iterator base_iterator_type
;
60 typedef client::lexer::conjure_tokens
<base_iterator_type
> lexer_type
;
61 typedef lexer_type::iterator_type iterator_type
;
63 lexer_type lexer
; // Our lexer
65 base_iterator_type first
= source_code
.begin();
66 base_iterator_type last
= source_code
.end();
68 iterator_type iter
= lexer
.begin(first
, last
);
69 iterator_type end
= lexer
.end();
71 client::vmachine vm
; // Our virtual machine
72 client::ast::function_list ast
; // Our AST
74 client::error_handler
<base_iterator_type
, iterator_type
>
75 error_handler(first
, last
); // Our error handler
76 client::parser::function
<iterator_type
, lexer_type
>
77 function(error_handler
, lexer
); // Our parser
78 client::code_gen::compiler
79 compiler(vm
, error_handler
); // Our compiler
81 // note: we don't need a skipper
82 bool success
= parse(iter
, end
, +function
, ast
);
84 std::cout
<< "-------------------------\n";
86 if (success
&& iter
== end
)
90 // JIT the main function
91 client::function main_function
= vm
.get_function("main");
94 std::cerr
<< "function main not found" << std::endl
;
99 if (main_function
.arity() != nargs
)
101 std::cerr
<< "Error: main function requires "
102 << main_function
.arity() << " arguments." << std::endl
;
103 std::cerr
<< nargs
<< " supplied." << std::endl
;
107 std::cout
<< "Success\n";
108 std::cout
<< "-------------------------\n";
109 std::cout
<< "Assembler----------------\n\n";
110 vm
.print_assembler();
112 // Call the main function
114 char** args
= argv
+ 2;
117 case 0: r
= main_function(); break;
119 case 1: r
= main_function(
120 boost::lexical_cast
<int>(args
[0]));
123 case 2: r
= main_function(
124 boost::lexical_cast
<int>(args
[0]),
125 boost::lexical_cast
<int>(args
[1]));
128 case 3: r
= main_function(
129 boost::lexical_cast
<int>(args
[0]),
130 boost::lexical_cast
<int>(args
[1]),
131 boost::lexical_cast
<int>(args
[2]));
135 std::cerr
<< "Function calls with more " <<
136 "than 3 arguments not supported" << std::endl
;
140 std::cout
<< "-------------------------\n";
141 std::cout
<< "Result: " << r
<< std::endl
;
142 std::cout
<< "-------------------------\n\n";
146 std::cout
<< "Compile failure\n";
151 std::cout
<< "Parse failure\n";