Jelajahi Sumber

added verbose flag and improved ignoring of lines only containing whitespaces (and comments)

Noah 4 tahun lalu
induk
melakukan
0fd7bbd77a
2 mengubah file dengan 41 tambahan dan 5 penghapusan
  1. 8 0
      src/main.cpp
  2. 33 5
      src/tibasic.cpp

+ 8 - 0
src/main.cpp

@@ -58,6 +58,9 @@ void stripExtension(const char *in, char *out, size_t len)
     *strrchr(out, '.') = 0;
 }
 
+// declare global variable to set debug mode later
+bool verbose = false;
+
 int main( int argc, char* argv[] )
 {
 	// check for valid number of arguments
@@ -89,6 +92,8 @@ int main( int argc, char* argv[] )
             }
             outFile = argv[i];
         }
+        else if(!strcmp(argv[i], "-v"))
+            verbose = true;
         else if(!strcmp(argv[i], "-d"))
             bDecompile = true;
         else
@@ -122,6 +127,9 @@ int main( int argc, char* argv[] )
         else
             outFile += ".8xp";
 
+        // print that verbose mode is activated
+        if(verbose)
+            log(Info, "Verbose Mode successfully activated");
     }
 
     // Make sure we have tokens to work with!

+ 33 - 5
src/tibasic.cpp

@@ -42,6 +42,19 @@ size_t Compiler::sumBytes(const char *data, size_t len)
     return ret;
 }
 
+string trim(const string& str)
+{
+    size_t first = str.find_first_not_of(' ');
+    if (string::npos == first)
+    {
+        return str;
+    }
+    size_t last = str.find_last_not_of(' ');
+    return str.substr(first, (last - first + 1));
+}
+
+extern bool verbose;
+
 bool Compiler::compile(string inFile, string outFile)
 {
     ifstream f(inFile.c_str(), ifstream::in);
@@ -56,13 +69,27 @@ bool Compiler::compile(string inFile, string outFile)
     {
         getline(f, tmpLine, '\n');
 
-        // ignore comments
-        tmpLine = tmpLine.substr(0, tmpLine.find("#", 0));
-
         // ignore empty lines
         if(!tmpLine.length())
         {
-            //log(Debug, "Line with only whitespaces / comments detected!");
+            if(verbose)
+                log(Debug, "Empty line detected!");
+            continue;
+        }
+
+        // remove comments
+        tmpLine = tmpLine.substr(0, tmpLine.find("#", 0));
+
+        // strip spaces at the beginning and end of lines
+        tmpLine = trim(tmpLine);
+
+
+        // ignore lines with now only whitespaces
+        bool containsSpaces = tmpLine.find_first_not_of(' ') != std::string::npos;
+        if(!containsSpaces)
+        {
+            if(verbose)
+                log(Debug, "Line with only whitespaces / comments detected!");
             continue;
         }
 
@@ -103,7 +130,8 @@ bool Compiler::compile(string inFile, string outFile)
             {
                 outputSize += token.sz;
                 output.push_back(token);
-
+                if(verbose)
+                    std::cout << "Debug: '" << s << "'\n";
                 tmpLine = tmpLine.substr(s.length(), tmpLine.length());
             }
         }