Explorar o código

Error printer now handles tabs in input file correctly

Taddeus Kroes %!s(int64=12) %!d(string=hai) anos
pai
achega
d57caab46f
Modificáronse 4 ficheiros con 85 adicións e 29 borrados
  1. 5 0
      test/old/array_assign.cvc
  2. 37 0
      test/old/simple.cvc
  3. 28 28
      test/testsuite/functional_test/1_array.cvc
  4. 15 1
      util.ml

+ 5 - 0
test/old/array_assign.cvc

@@ -0,0 +1,5 @@
+void foo() {
+    int[4] a;
+
+    a = [1, 2, 3];
+}

+ 37 - 0
test/old/simple.cvc

@@ -0,0 +1,37 @@
+extern void printInt(int val);
+extern void printSpaces(int num);
+extern void printNewlines(int num);
+
+/*void foo(int a, int b, int c) {
+    int d;
+    printInt(a);
+    printInt(b);
+    printInt(c);
+    printInt(d);
+}
+void bar() {
+    foo(1, 2, 3);
+}*/
+void foo(int[a, b] p) {
+    int l;
+    int[2,3] q;
+    l = a;
+    printInt(l);
+    printSpaces(1);
+    l = b;
+    printInt(l);
+    printSpaces(1);
+    //q = p;              // should give an error
+                        // only possible without dimension reduction
+    printInt(p[1, 0]);  // 10
+    printSpaces(1);
+    printNewlines(1);
+}
+
+export int main() {
+    int i = 1;
+    int[4, i] arr;
+    arr[1, 0] = 10;
+    foo(arr);
+    return 0;
+}

+ 28 - 28
test/testsuite/functional_test/1_array.cvc

@@ -6,36 +6,36 @@ export int main()
 {
     int a;
     int b;
-    int [5,3]t =1;
+	int [5,3]t =1;
 
     int [5,3]c =[[1,2,300],
-                 1,
-                 3,
-                 [3,4],
-                 6
-                ];
-
-
-    c[4-2,2] = 2;
-    for(int q=0,5)
-    {
-        for(int p=0,3)
-        {   printInt(c[q,p]);
-            printSpaces(3);
-        }
-        printNewlines(1);
-
-    }
-    printNewlines(3);
-
-    for(int q=0,5)
-    {
-        for(int p=0,3)
-        {   printInt(t[q,p]);
-            printSpaces(3);
-        }
-        printNewlines(1);
-    }
+				 1,
+				 3,
+				 [3,4],
+				 6
+				];
+
+
+	c[4-2,2] = 2;
+	for(int q=0,5)
+	{
+		for(int p=0,3)
+		{	printInt(c[q,p]);
+			printSpaces(3);
+		}
+		printNewlines(1);
+
+	}
+	printNewlines(3);
+
+	for(int q=0,5)
+	{
+		for(int p=0,3)
+		{	printInt(t[q,p]);
+			printSpaces(3);
+		}
+		printNewlines(1);
+	}
 
     return 1;
 }

+ 15 - 1
util.ml

@@ -1,4 +1,5 @@
 open Printf
+open Str
 open Lexing
 open Types
 
@@ -336,6 +337,18 @@ let get_line str n =
     String.sub str linestart (lineend - linestart)
 *)
 
+let count_tabs str upto =
+    let rec count n = function
+        | 0 -> n
+        | i -> count (if String.get str (i - 1) = '\t' then n + 1 else n) (i - 1)
+    in count 0 upto
+
+let tabwidth = 4
+
+let retab str = global_replace (regexp "\t") (repeat " " tabwidth) str
+
+let indent n = repeat (repeat " " (tabwidth - 1)) n
+
 let prerr_loc (fname, ystart, yend, xstart, xend) =
     let file = open_in fname in
 
@@ -349,7 +362,8 @@ let prerr_loc (fname, ystart, yend, xstart, xend) =
         let left = if l = ystart then xstart else 1 in
         let right = if l = yend then xend else linewidth in
         if linewidth > 0 then (
-            prerr_endline line;
+            prerr_endline (retab line);
+            prerr_string (indent (count_tabs line right));
             for i = 1 to left - 1 do prerr_char ' ' done;
             for i = left to right do prerr_char '^' done;
             prerr_endline "";