Driver.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "Driver.h"
  2. #include "Ast.h"
  3. #include "Semantic.h"
  4. #include "CodeGeneration.h"
  5. #include "Logging.h"
  6. #include <cstdio>
  7. extern std::unique_ptr<std::vector<std::unique_ptr<qlow::ast::Class>>> parsedClasses;
  8. extern FILE* qlow_parser_in;
  9. extern int qlow_parser_parse(void);
  10. using namespace qlow;
  11. Options Options::parseOptions(int argc, char** argv)
  12. {
  13. static const std::map<std::string, bool Options::*> boolArgs =
  14. {
  15. {"-S", &Options::emitAssembly},
  16. {"--emit-assembly", &Options::emitAssembly},
  17. {"-L", &Options::emitLlvm},
  18. {"--emit-llvm", &Options::emitLlvm},
  19. };
  20. Options options{};
  21. for (int i = 1; i < argc; i++) {
  22. std::string arg = argv[i];
  23. if (boolArgs.find(arg) != boolArgs.end()) {
  24. bool Options::* attr = boolArgs.find(arg)->second;
  25. options.*attr = true;
  26. }
  27. else if (arg == "-o" || arg == "--out") {
  28. if (argc > i + 1) {
  29. options.outfile = argv[++i];
  30. }
  31. else {
  32. throw "Please specify a filename after '-o'";
  33. }
  34. }
  35. else {
  36. options.infiles.push_back(std::move(arg));
  37. }
  38. }
  39. return options;
  40. }
  41. Driver::Driver(int argc, char** argv) :
  42. options{ Options::parseOptions(argc, argv) }
  43. {
  44. }
  45. int Driver::run(void)
  46. {
  47. Logger& logger = Logger::getInstance();
  48. //logger.logError("driver not yet implemented", {options.emitAssembly ? "asm" : "noasm", 10, 11, 12, 13});
  49. std::vector<std::unique_ptr<qlow::ast::Class>> classes;
  50. for (auto& filename : options.infiles) {
  51. std::FILE* file = std::fopen(filename.c_str(), "r");
  52. try {
  53. classes = parseFile(file);
  54. } catch (const char* errMsg) {
  55. logger.logError(errMsg);
  56. return 1;
  57. }
  58. if (file)
  59. std::fclose(file);
  60. }
  61. return 0;
  62. }
  63. std::vector<std::unique_ptr<qlow::ast::Class>> Driver::parseFile(FILE* file)
  64. {
  65. ::qlow_parser_in = file;
  66. if (!::qlow_parser_in)
  67. throw "Could not run parser: Invalid file";
  68. ::qlow_parser_parse();
  69. auto retval = std::move(*parsedClasses);
  70. parsedClasses.reset();
  71. return retval;
  72. }