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

VarDec and Assign are now interleaved, making scoping correct

Taddeus Kroes 12 лет назад
Родитель
Сommit
e2cc38a7b6

+ 6 - 4
phases/desug.ml

@@ -72,7 +72,7 @@ let rec array_dims node =
     let (decs, dims) = make_dims make_dimname values make_dec in
     Block (decs @ [GlobalDef (export, ArrayDims (ctype, dims), name, None, ann)])
 
-    (*
+  (* DISABLED, this is also done in extern.ml
   | GlobalDec (ArrayDims (ctype, values), name, ann) ->
     (*
     let rec make_decs = function
@@ -87,7 +87,7 @@ let rec array_dims node =
     let make_dec value name = GlobalDec (Int, name, []) in
     let (decs, dims) = make_dims name values make_dec in
     Block (decs @ [GlobalDec (ArrayDims (ctype, dims), name, ann)])
-    *)
+  *)
 
   | node -> transform_children array_dims node
 
@@ -153,7 +153,8 @@ let rec move_inits = function
       Program (init_func :: decls, ann)
     end
 
-  (* Split local variable initialisations in declaration and assignment *)
+  (* DISABLED, interleaved declarations and assignments are allowed in the
+   * intermediate representation
   | FunDef (export, ret_type, name, params, Block body, ann) ->
     let rec place_inits inits = function
       | VarDecs lst :: tl ->
@@ -165,6 +166,7 @@ let rec move_inits = function
     in
     let body = Block (place_inits [] body) in
     FunDef (export, ret_type, name, params, body, ann)
+  *)
 
   | node -> transform_children move_inits node
 
@@ -218,8 +220,8 @@ let for_to_while node =
         )), ann));
       ]
 
-    (* DISABLED, while-loops are explicitly supported by the assembly phase
     (* Transform while-loops to do-while loops in if-statements *)
+    (* DISABLED, while-loops are explicitly supported by the assembly phase
     | While (cond, body, ann) ->
       let cond = traverse new_vars cond in
       let body = traverse new_vars body in

+ 16 - 0
test/basic/functional/var_init.cvc

@@ -0,0 +1,16 @@
+extern void printInt(int val);
+extern void printNewlines(int num);
+
+int glob = 2;
+
+export int main() {
+    int a = 3;
+    int b = a;
+    printInt(a);        // 3
+    printNewlines(1);
+    printInt(b);        // 3
+    printNewlines(1);
+    printInt(glob);     // 2
+    printNewlines(1);
+    return 0;
+}

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

@@ -0,0 +1,3 @@
+3
+3
+2

+ 12 - 0
test/basic/functional/var_init_advanced.cvc

@@ -0,0 +1,12 @@
+extern void printInt(int val);
+extern void printNewlines(int num);
+
+int b = 1;
+
+export int main() {
+    int a = b;
+    int b;          // Redefines "b" later than assignment "a = b"
+    printInt(a);    // 1
+    printNewlines(1);
+    return 0;
+}

+ 1 - 0
test/basic/functional/var_init_advanced.out

@@ -0,0 +1 @@
+1