main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <config.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. #include <cstring>
  7. #include "tree.hpp"
  8. #include "globals.hpp"
  9. #include "create_wrapper.hpp"
  10. #include "create_docu.hpp"
  11. extern int yyparse();
  12. extern int yylex();
  13. CompilationUnit* unit = 0;
  14. std::istream* input = 0;
  15. std::string inputfile;
  16. std::string selected_namespace;
  17. std::string modulename = "wrapper";
  18. void usage()
  19. {
  20. std::cout << "Usage: miniswig --input FILE --output-cpp FILE --output-hpp FILE [--module NAME] [--select-namespace NAME]\n";
  21. std::cout << "\n";
  22. }
  23. int main(int argc, char** argv)
  24. {
  25. std::string outputcpp;
  26. std::string outputhpp;
  27. std::string output_doc;
  28. for(int i = 0; i < argc; ++i) {
  29. if(strcmp(argv[i], "--module") == 0) {
  30. if(i+1 >= argc) {
  31. std::cerr << "Need to specify a module name.\n";
  32. usage();
  33. return 1;
  34. }
  35. modulename = argv[++i];
  36. } else if(strcmp(argv[i], "--input") == 0) {
  37. if(i+1 >= argc) {
  38. std::cerr << "Need to specify input file name.\n";
  39. usage();
  40. return 1;
  41. }
  42. inputfile = argv[++i];
  43. } else if(strcmp(argv[i], "--output-cpp") == 0) {
  44. if(i+1 >= argc) {
  45. std::cerr << "Need to specify output cpp name.\n";
  46. usage();
  47. return 1;
  48. }
  49. outputcpp = argv[++i];
  50. } else if(strcmp(argv[i], "--output-hpp") == 0) {
  51. if(i+1 >= argc) {
  52. std::cerr << "Need to specify output hpp name.\n";
  53. usage();
  54. return 1;
  55. }
  56. outputhpp = argv[++i];
  57. } else if(strcmp(argv[i], "--select-namespace") == 0) {
  58. if(i+1 >= argc) {
  59. std::cerr << "Need to specify a namespace.\n";
  60. usage();
  61. return 1;
  62. }
  63. selected_namespace = argv[++i];
  64. } else if(strcmp(argv[i], "--output-doc") == 0) {
  65. if(i+1 >= argc) {
  66. std::cerr << "Need to specify document xml file.\n";
  67. usage();
  68. return 1;
  69. }
  70. output_doc = argv[++i];
  71. } else if(argv[i][0] == '-') {
  72. std::cerr << "Unknown option '" << argv[i] << "'.\n";
  73. usage();
  74. return 1;
  75. } else {
  76. }
  77. }
  78. if( inputfile == "" || (
  79. (outputcpp == "" || outputhpp == "") && output_doc == "")) {
  80. std::cerr << "Not all options specified.\n";
  81. usage();
  82. return 1;
  83. }
  84. try {
  85. input = new std::ifstream(inputfile.c_str());
  86. if(!input->good()) {
  87. std::cerr << "Couldn't open file '" << inputfile << "' for reading.\n";
  88. return 1;
  89. }
  90. current_file = inputfile;
  91. unit = new CompilationUnit();
  92. Namespace* std_namespace = new Namespace();
  93. std_namespace->name = "std";
  94. std_namespace->types.push_back(new StringType());
  95. unit->namespaces.push_back(std_namespace);
  96. unit->types.push_back(new HSQUIRRELVMType());
  97. unit->types.push_back(new SQIntegerType());
  98. yyparse();
  99. Namespace* ns = unit;
  100. if(selected_namespace != "") {
  101. ns = ns->findNamespace(selected_namespace);
  102. }
  103. if(outputcpp != "") {
  104. std::ofstream cppout(outputcpp.c_str());
  105. if(!cppout.good()) {
  106. std::cerr << "Couldn't open file '"
  107. << outputcpp << "' for writing.\n";
  108. return 1;
  109. }
  110. std::ofstream hppout(outputhpp.c_str());
  111. if(!hppout.good()) {
  112. std::cerr << "Couldn't open file '" << outputhpp
  113. << "' for writing.\n";
  114. return 1;
  115. }
  116. WrapperCreator creator(cppout, hppout);
  117. creator.create_wrapper(ns);
  118. }
  119. if(output_doc != "") {
  120. std::ofstream dout(output_doc.c_str());
  121. if(!dout.good()) {
  122. std::cerr << "Couldn't open file '"
  123. << output_doc << "' for writing.\n";
  124. return 1;
  125. }
  126. DocuCreator creator(dout);
  127. creator.create_docu(ns);
  128. }
  129. } catch(std::exception& e) {
  130. std::cerr << "Exception: " << e.what() << "\n";
  131. return 1;
  132. }
  133. return 0;
  134. }