main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2011 Matthew Iselin
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. // When VC++ compiles in debug mode, it will set _DEBUG. This sets DEBUG
  17. // in order to avoid the VC++-ism.
  18. #ifdef _DEBUG
  19. #define DEBUG
  20. #endif
  21. #include <iostream>
  22. #include <string>
  23. #include <string.h>
  24. #include "tibasic.h"
  25. #ifdef _WIN32
  26. #include "Shlwapi.h"
  27. #endif
  28. using namespace std;
  29. /// Helper function to convert a string to uppercase.
  30. char* strtoupper(char* str)
  31. {
  32. for( size_t i = 0; i < strlen( str ); i++ )
  33. {
  34. if( ! ( isupper( str[i] ) ) && isalpha( str[i] ) )
  35. str[i] = _toupper( str[i] );
  36. }
  37. return str;
  38. }
  39. /// Logs output from the build
  40. void log(LogSeverity severity, const char *out)
  41. {
  42. cout << severityToString(severity) << ": " << out << endl;
  43. }
  44. void stripExtension(const char *in, char *out, size_t len)
  45. {
  46. strncpy(out, in, len);
  47. *strrchr(out, '.') = 0;
  48. }
  49. int main( int argc, char* argv[] )
  50. {
  51. // check for valid number of arguments
  52. if((argc < 2) || (argv[1] == NULL))
  53. {
  54. // Inform the user
  55. log(Error, "Usage: tibasic.exe [options] filename\nOptions:\n\t-v:\tVerbose mode");
  56. return 1;
  57. }
  58. Compiler compiler; Compiler *pCompiler = &compiler;
  59. string inFile, outFile;
  60. bool bDecompile = false;
  61. // Parse arguments
  62. for(int i = 1; i < argc; i++)
  63. {
  64. if(!strcmp(argv[i], "-o") && !outFile.length())
  65. {
  66. // Output filename
  67. if((i + 1) >= argc)
  68. {
  69. log(Error, "-o requires a parameter (output filename).");
  70. return 1;
  71. }
  72. outFile = argv[i + 1];
  73. }
  74. else if(!strcmp(argv[i], "-v"))
  75. pCompiler->verbosity(true);
  76. else if(!strcmp(argv[i], "-d"))
  77. bDecompile = true;
  78. else if(!inFile.length())
  79. inFile = argv[i];
  80. }
  81. // If no output was given, rename the input with .8xp instead of .tib and
  82. // use that as the output.
  83. if(!outFile.length())
  84. {
  85. char *tmp = new char[inFile.length()];
  86. stripExtension(inFile.c_str(), tmp, inFile.length());
  87. outFile = tmp;
  88. if(bDecompile)
  89. outFile += ".tib";
  90. else
  91. outFile += ".8xp";
  92. delete [] tmp;
  93. }
  94. // Make sure we have tokens to work with!
  95. initialiseTokens();
  96. // Compile time!
  97. if(inFile.length() && outFile.length())
  98. {
  99. bool res = false;
  100. if(bDecompile)
  101. res = pCompiler->decompile(inFile, outFile);
  102. else
  103. res = pCompiler->compile(inFile, outFile);
  104. if(!res)
  105. {
  106. log(Error, "Compilation failed.");
  107. return 1;
  108. }
  109. }
  110. else
  111. {
  112. log(Error, "Either an input or output filename was not given.");
  113. return 1;
  114. }
  115. return 0;
  116. }