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

Fixed assigning/loading elements in imported arrays

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

+ 14 - 22
phases/assemble.ml

@@ -3,6 +3,14 @@ open Types
 open Util
 open Stringify
 
+let get_scope node = function
+  | GlobalDec _ -> Extern
+  | dec ->
+    match (depthof dec, depthof node) with
+    | (0, _)            -> Glob
+    | (a, b) when a = b -> Current
+    | (a, b)            -> Rel (b - a)
+
 let comline comment = InlineComment (EmptyLine, comment)
 
 let assemble program =
@@ -25,6 +33,7 @@ let assemble program =
       | _ -> []
     in
     match node with
+
     (* Global *)
 
     | Program (decls, _) ->
@@ -77,12 +86,7 @@ let assemble program =
     (* Statements *)
 
     | VarLet (dec, None, value, _) ->
-      let store =
-        match (depthof dec, depthof node) with
-        | (0, _)            -> Store (typeof dec, Glob,        indexof dec)
-        | (a, b) when a = b -> Store (typeof dec, Current,     indexof dec)
-        | (a, b)            -> Store (typeof dec, Rel (b - a), indexof dec)
-      in
+      let store = Store (typeof dec, get_scope node dec, indexof dec) in
       trav value @ [InlineComment (store, node2str node)]
 
     | Return (value, _) ->
@@ -233,6 +237,7 @@ let assemble program =
       [InlineComment (Label (endlabel), node2str node)]
 
     (* Arrays *)
+
     | Allocate (dec, [dim], _) ->
       let store =
         match (depthof dec, depthof node) with
@@ -249,27 +254,14 @@ let assemble program =
                                          (should be one-dimensional)")))
 
     | VarUse (dec, Some dims, _) ->
-      let load =
-        match dec with
-        | GlobalDec (ctype, name, _) ->
-          Load (ctype, Extern, indexof dec)
-        | _ ->
-          match (depthof dec, depthof node) with
-          | (0, _)            -> Load (typeof dec, Glob,        indexof dec)
-          | (a, b) when a = b -> Load (typeof dec, Current,     indexof dec)
-          | (a, b)            -> Load (typeof dec, Rel (b - a), indexof dec)
-      in
+      let load = Load (typeof dec, get_scope node dec, indexof dec) in
       (trav_all (List.rev dims)) @          (* push dimensions *)
       [InlineComment (load, nameof dec)] @  (* push array reference *)
       [InlineComment (LoadArray (basetypeof dec), node2str node)]
 
     | VarLet (dec, Some dims, value, _) ->
-      let load =
-        match (depthof dec, depthof node) with
-        | (0, _)            -> Load (typeof dec, Glob,        indexof dec)
-        | (a, b) when a = b -> Load (typeof dec, Current,     indexof dec)
-        | (a, b)            -> Load (typeof dec, Rel (b - a), indexof dec)
-      in
+      let load = Load (typeof dec, get_scope node dec, indexof dec) in
+      (trav_all (List.rev dims)) @          (* push dimensions *)
       (trav value) @                        (* push value *)
       (trav_all dims) @                     (* push dimensions *)
       [InlineComment (load, nameof dec)] @  (* push array reference *)

+ 1 - 1
test/arrays/combined_extern_array/expected.out

@@ -1 +1 @@
-123
+1235

+ 2 - 0
test/arrays/combined_extern_array/main.cvc

@@ -6,5 +6,7 @@ export int main() {
     printInt(foo[0]);
     printInt(foo[1]);
     printInt(foo[2]);
+    foo[1] = 5;
+    printInt(foo[1]);
     return 0;
 }