Просмотр исходного кода

Updated/added a bunch of test cases to test new scoping rules

Taddeus Kroes 11 лет назад
Родитель
Сommit
2cc381f53a

+ 0 - 5
test/arrays/check_error/redefined_dimension.cvc

@@ -1,5 +0,0 @@
-int n = 1;
-void foo() {
-    // n is NOT 1 here since the name "n" is first redefined as array
-    int[n] n;
-}

+ 5 - 0
test/arrays/check_success/redefined_dimension.cvc

@@ -0,0 +1,5 @@
+int n = 1;
+void foo() {
+    // This is legal since the local 'n' is defined after this statement
+    int[n] n;
+}

+ 50 - 0
test/arrays/check_success/scopes.cvc

@@ -0,0 +1,50 @@
+extern void printInt(int val);
+extern void printNewlines(int num);
+
+int a = 1;
+int b = 2;
+
+int[4] c;
+
+extern int[m] d;
+
+int foo() {
+    int a = a + b + 1;
+    int m = m;
+    int baz(int c, int[b] d) {
+        int a = c + b;
+        return a + d[c];
+    }
+    a = a + 1;
+    return baz(a + d[m - 1], c);
+}
+
+int bar() {
+    int a = b;
+    int b;
+    return a;
+}
+
+void baz() {
+    int i;
+    printInt(i);
+    for (int i = 1, 10) {
+        printInt(i);
+        for (int i = 1, 10) {
+            //i = 0;
+            printInt(i);
+        }
+        printInt(i);
+    }
+    printInt(i);
+}
+
+export int main() {
+    printInt(foo());
+    printNewlines(1);
+    printInt(bar());
+    printNewlines(1);
+    baz();
+    printNewlines(1);
+    return 0;
+}

+ 52 - 0
test/arrays/functional/scopes.cvc

@@ -0,0 +1,52 @@
+extern void printInt(int val);
+extern void printSpaces(int num);
+extern void printNewlines(int num);
+
+int a = 1;
+int b = 2;
+
+int[4] c = [5, -1, 3];
+
+int foo() {
+    int a = a + b + 1;          // 1 + 2 + 1
+    int baz(int c, int[b] d) {
+        int a = c + b;          // 8 + 4
+        return a + d[c - 8];    // 12 + 5
+    }
+    a = a + 1;                  // 4 + 1
+    return baz(a + c[2], c);    // baz(5 + 3, c)
+}
+
+int bar() {
+    int a = b;
+    int b;
+    return a;
+}
+
+void baz() {
+    int i = 9;
+    printInt(i);                // 9
+    printNewlines(1);
+    for (int i = 1, 3) {
+        printInt(i);            // 1, 2
+        printNewlines(1);
+        for (int i = 4, 7) {
+            printSpaces(1);     //  4 5 6
+            printInt(i);
+        }
+        printNewlines(1);
+        printInt(i);            // 1, 2
+        printNewlines(1);
+    }
+    printInt(i);                // 9
+    printNewlines(1);
+}
+
+export int main() {
+    printInt(foo());  // 17
+    printNewlines(1);
+    printInt(bar());  // 2
+    printNewlines(1);
+    baz();
+    return 0;
+}

+ 10 - 0
test/arrays/functional/scopes.out

@@ -0,0 +1,10 @@
+17
+2
+9
+1
+ 4 5 6
+1
+2
+ 4 5 6
+2
+9

+ 36 - 5
test/basic/functional/var_init.cvc

@@ -1,16 +1,47 @@
 extern void printInt(int val);
 extern void printNewlines(int num);
 
-int glob = 2;
+int a = 1;
+int b = 10;
 
-export int main() {
+void foo() {
     int a = 3;
-    int b = a;
+    int c = a;  // references local 'a'
     printInt(a);        // 3
     printNewlines(1);
-    printInt(b);        // 3
+    printInt(c);        // 3
     printNewlines(1);
-    printInt(glob);     // 2
+    printInt(b);        // 10
     printNewlines(1);
+}
+
+void bar() {
+    // initializer uses global variable 'a'
+    int a = a + 1;
+    printInt(a);
+    printNewlines(1);
+}
+
+void baz() {
+    // initializer uses global variable 'b'
+    int a = b;
+    int b = 5;
+    printInt(a);
+    printNewlines(1);
+}
+
+export int main() {
+    foo();  // 3, 3, 10
+
+    bar();  // 2
+
+    printInt(a);  // 1
+    printNewlines(1);
+
+    baz();  // 10
+
+    printInt(b);  // 10
+    printNewlines(1);
+
     return 0;
 }

+ 4 - 0
test/basic/functional/var_init.out

@@ -1,3 +1,7 @@
 3
 3
+10
 2
+1
+10
+10

+ 9 - 14
test/nested_funs/functional/scopes.cvc

@@ -2,15 +2,13 @@
  * Different shadowing concepts illustrated
  */
 
-extern void printInt( int val);
-extern void printSpaces( int num);
-extern void printNewlines( int num);
-
+extern void printInt(int val);
+extern void printSpaces(int num);
+extern void printNewlines(int num);
 
 export int main()
 {
-
-    // this variable is going will be escaping below
+    // this variable will be escaped below
     int a = 123;
 
     // The bir variable is an example of a local (ie non-escaped) variable
@@ -39,25 +37,19 @@ export int main()
         a = 55555;
     }
 
-
-
     bar(a);
-    printNewlines(1);
     printInt(a);
     printNewlines(2);
 
     baz(a);
-    printNewlines(1);
     printInt(a);
     printNewlines(2);
 
     bor(a);
-    printNewlines(1);
     printInt(a);
     printNewlines(2);
 
-    //boz();  FIXME
-    printNewlines(1);
+    boz();
     printInt(a);
     printNewlines(2);
 
@@ -67,8 +59,9 @@ export int main()
     biz();
     printInt(a);
     printNewlines(2);
+
     printInt(bir);
-    printNewlines(2);
+    printNewlines(1);
 
     return 0;
 
@@ -81,6 +74,7 @@ void baz(int b){
     void baz_inner(){
         int b = 22222;
         printInt(b);
+        printNewlines(1);
     }
 
     baz_inner();
@@ -94,6 +88,7 @@ void bor(int b){
 
         void b() {
             printInt(33333);
+            printNewlines(1);
         }
         b();
     }

+ 1 - 3
test/nested_funs/functional/scopes.out

@@ -1,5 +1,4 @@
 11111
-
 123
 
 22222
@@ -8,7 +7,7 @@
 33333
 123
 
-
+11234
 123
 
 123
@@ -16,4 +15,3 @@
 55555
 
 123
-