Kajetan Johannes Hammerle 3 years ago
parent
commit
3077a0dc9b
5 changed files with 54 additions and 1 deletions
  1. 6 0
      tests/comments/line
  2. 4 0
      tests/comments/line.out
  3. 6 0
      tests/comments/lines
  4. 4 0
      tests/comments/lines.out
  5. 34 1
      tokenizer/Tokenizer.c

+ 6 - 0
tests/comments/line

@@ -0,0 +1,6 @@
+print 1;
+print 2; //print 3;
+//print 4; print 5;
+print 6; //print 7;
+print 8;
+//print 9;

+ 4 - 0
tests/comments/line.out

@@ -0,0 +1,4 @@
+1
+2
+6
+8

+ 6 - 0
tests/comments/lines

@@ -0,0 +1,6 @@
+print 1;
+print 2; /*print 3;
+print 4; print 5;*/
+print 6; /*print 7;****/
+print 8;
+/*print 9;*/

+ 4 - 0
tests/comments/lines.out

@@ -0,0 +1,4 @@
+1
+2
+6
+8

+ 34 - 1
tokenizer/Tokenizer.c

@@ -145,6 +145,39 @@ static bool tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
     return fReadIf(c) ? tAddToken2(tce, tc) : tAddToken2(te, t);
 }
 
+static bool tLineComment() {
+    while(true) {
+        int c = fRead();
+        if(c == EOF || c == '\n') {
+            line++;
+            return true;
+        }
+    }
+}
+
+static bool tMultipleLineComment() {
+    while(true) {
+        int c = fRead();
+        if(c == EOF) {
+            tError("unclosed comment at line %d", line);
+            return false;
+        } else if(c == '\n') {
+            line++;
+        } else if(c == '*' && fReadIf('/')) {
+            return true;
+        }
+    }
+}
+
+static bool tSlash() {
+    if(fReadIf('/')) {
+        return tLineComment();
+    } else if(fReadIf('*')) {
+        return tMultipleLineComment();
+    }
+    return tAddToken2(T_DIV_SET, T_DIV);
+}
+
 static bool tParseToken() {
     int c = fRead();
     if(c == EOF) {
@@ -160,7 +193,7 @@ static bool tParseToken() {
         case '+': return tAddToken3('+', T_INCREMENT, T_ADD_SET, T_ADD);
         case '-': return tAddToken3('-', T_DECREMENT, T_SUB_SET, T_SUB);
         case '*': return tAddToken2(T_MUL_SET, T_MUL);
-        case '/': return tAddToken2(T_DIV_SET, T_DIV);
+        case '/': return tSlash();
         case '%': return tAddToken2(T_MOD_SET, T_MOD);
         case '<':
             return tAddToken4('<', T_LEFT_SHIFT_SET, T_LEFT_SHIFT, T_LESS_EQUAL,