main.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. ///////////////////////////////////////////////////////////////////////////////
  7. //
  8. // Now we'll introduce boolean expressions and control structures.
  9. // Is it obvious now what we are up to? ;-)
  10. //
  11. // [ JDG April 9, 2007 ] spirit2
  12. // [ JDG February 18, 2011 ] Pure attributes. No semantic actions.
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include "statement.hpp"
  16. #include "vm.hpp"
  17. #include "compiler.hpp"
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // Main program
  20. ///////////////////////////////////////////////////////////////////////////////
  21. int
  22. main()
  23. {
  24. std::cout << "/////////////////////////////////////////////////////////\n\n";
  25. std::cout << "Statement parser...\n\n";
  26. std::cout << "/////////////////////////////////////////////////////////\n\n";
  27. std::cout << "Type some statements... ";
  28. std::cout << "An empty line ends input, compiles, runs and prints results\n\n";
  29. std::cout << "Example:\n\n";
  30. std::cout << " var a = 123;\n";
  31. std::cout << " var b = 456;\n";
  32. std::cout << " var c = a + b * 2;\n\n";
  33. std::cout << "-------------------------\n";
  34. std::string str;
  35. std::string source;
  36. while (std::getline(std::cin, str))
  37. {
  38. if (str.empty())
  39. break;
  40. source += str + '\n';
  41. }
  42. typedef std::string::const_iterator iterator_type;
  43. iterator_type iter = source.begin();
  44. iterator_type end = source.end();
  45. client::vmachine vm; // Our virtual machine
  46. client::code_gen::program program; // Our VM program
  47. client::ast::statement_list ast; // Our AST
  48. client::error_handler<iterator_type>
  49. error_handler(iter, end); // Our error handler
  50. client::parser::statement<iterator_type>
  51. parser(error_handler); // Our parser
  52. client::code_gen::compiler
  53. compile(program, error_handler); // Our compiler
  54. boost::spirit::ascii::space_type space;
  55. bool success = phrase_parse(iter, end, parser, space, ast);
  56. std::cout << "-------------------------\n";
  57. if (success && iter == end)
  58. {
  59. if (compile.start(ast))
  60. {
  61. std::cout << "Success\n";
  62. std::cout << "-------------------------\n";
  63. vm.execute(program());
  64. std::cout << "-------------------------\n";
  65. std::cout << "Assembler----------------\n\n";
  66. program.print_assembler();
  67. std::cout << "-------------------------\n";
  68. std::cout << "Results------------------\n\n";
  69. program.print_variables(vm.stack);
  70. }
  71. else
  72. {
  73. std::cout << "Compile failure\n";
  74. }
  75. }
  76. else
  77. {
  78. std::cout << "Parse failure\n";
  79. }
  80. std::cout << "-------------------------\n\n";
  81. return 0;
  82. }