main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  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. // Not a calculator anymore, right? :-)
  10. //
  11. // [ JDG April 10, 2007 ] spirit2
  12. // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
  13. // [ HK June 3, 2011 ] Adding lexer
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include "config.hpp"
  17. #include "function.hpp"
  18. #include "vm.hpp"
  19. #include "compiler.hpp"
  20. #include "lexer.hpp"
  21. #include <boost/lexical_cast.hpp>
  22. #include <fstream>
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // Main program
  25. ///////////////////////////////////////////////////////////////////////////////
  26. int main(int argc, char **argv)
  27. {
  28. char const* filename;
  29. if (argc > 1)
  30. {
  31. filename = argv[1];
  32. }
  33. else
  34. {
  35. std::cerr << "Error: No input file provided." << std::endl;
  36. return 1;
  37. }
  38. std::ifstream in(filename, std::ios_base::in);
  39. if (!in)
  40. {
  41. std::cerr << "Error: Could not open input file: "
  42. << filename << std::endl;
  43. return 1;
  44. }
  45. std::string source_code; // We will read the contents here.
  46. in.unsetf(std::ios::skipws); // No white space skipping!
  47. std::copy(
  48. std::istream_iterator<char>(in),
  49. std::istream_iterator<char>(),
  50. std::back_inserter(source_code));
  51. typedef std::string::const_iterator base_iterator_type;
  52. typedef client::lexer::conjure_tokens<base_iterator_type> lexer_type;
  53. typedef lexer_type::iterator_type iterator_type;
  54. lexer_type lexer; // Our lexer
  55. base_iterator_type first = source_code.begin();
  56. base_iterator_type last = source_code.end();
  57. iterator_type iter = lexer.begin(first, last);
  58. iterator_type end = lexer.end();
  59. client::vmachine vm; // Our virtual machine
  60. client::ast::function_list ast; // Our AST
  61. client::error_handler<base_iterator_type, iterator_type>
  62. error_handler(first, last); // Our error handler
  63. client::parser::function<iterator_type, lexer_type>
  64. function(error_handler, lexer); // Our parser
  65. client::code_gen::compiler
  66. compiler(error_handler); // Our compiler
  67. // note: we don't need a skipper
  68. bool success = parse(iter, end, +function, ast);
  69. std::cout << "-------------------------\n";
  70. if (success && iter == end)
  71. {
  72. if (compiler(ast))
  73. {
  74. boost::shared_ptr<client::code_gen::function>
  75. p = compiler.find_function("main");
  76. if (!p)
  77. return 1;
  78. int nargs = argc-2;
  79. if (p->nargs() != nargs)
  80. {
  81. std::cerr << "Error: main function requires " << p->nargs() << " arguments." << std::endl;
  82. std::cerr << nargs << " supplied." << std::endl;
  83. return 1;
  84. }
  85. std::cout << "Success\n";
  86. std::cout << "-------------------------\n";
  87. std::cout << "Assembler----------------\n\n";
  88. compiler.print_assembler();
  89. // Push the arguments into our stack
  90. for (int i = 0; i < nargs; ++i)
  91. vm.get_stack()[i] = boost::lexical_cast<int>(argv[i+2]);
  92. // Call the interpreter
  93. int r = vm.execute(compiler.get_code());
  94. std::cout << "-------------------------\n";
  95. std::cout << "Result: " << r << std::endl;
  96. std::cout << "-------------------------\n\n";
  97. }
  98. else
  99. {
  100. std::cout << "Compile failure\n";
  101. }
  102. }
  103. else
  104. {
  105. error_handler.dump_error_line(first);
  106. std::cout << "Parse failure\n";
  107. }
  108. return 0;
  109. }