|
|
@@ -1,10 +1,10 @@
|
|
|
open Ast
|
|
|
open Util
|
|
|
|
|
|
-let rec flatten = function
|
|
|
+let rec flatten_blocks = function
|
|
|
| [] -> []
|
|
|
- | Block nodes :: t -> (flatten nodes) @ (flatten t)
|
|
|
- | h :: t -> h :: (flatten t)
|
|
|
+ | Block nodes :: t -> (flatten_blocks nodes) @ (flatten_blocks t)
|
|
|
+ | h :: t -> h :: (flatten_blocks t)
|
|
|
|
|
|
let rec var_init = function
|
|
|
(* Split local variable initialisations in declaration and assignment *)
|
|
|
@@ -13,23 +13,26 @@ let rec var_init = function
|
|
|
let rec trav inits node = match node with
|
|
|
(* translate scalar array initialisation to ArrayScalar node,
|
|
|
* for easy replacement later on *)
|
|
|
- | VarDec (ArrayDef (_, _) as vtype, name, Some ((BoolConst (_, l)) as v), loc) :: t
|
|
|
- | VarDec (ArrayDef (_, _) as vtype, name, Some ((FloatConst (_, l)) as v), loc) :: t
|
|
|
- | VarDec (ArrayDef (_, _) as vtype, name, Some ((IntConst (_, l)) as v), loc) :: t ->
|
|
|
+ | VarDec (ArrayDef (_, _) as vtype, name,
|
|
|
+ Some ((BoolConst (_, l)) as v), loc) :: t
|
|
|
+ | VarDec (ArrayDef (_, _) as vtype, name,
|
|
|
+ Some ((FloatConst (_, l)) as v), loc) :: t
|
|
|
+ | VarDec (ArrayDef (_, _) as vtype, name,
|
|
|
+ Some ((IntConst (_, l)) as v), loc) :: t ->
|
|
|
trav inits (VarDec (vtype, name, Some (ArrayScalar (v, l)), loc) :: t)
|
|
|
|
|
|
| VarDec (ctype, name, init, loc) :: t ->
|
|
|
(* array definition: create __allocate statement *)
|
|
|
let alloc = match ctype with
|
|
|
- | ArrayDef (_, dims) -> [Allocate (name, dims, noloc)]
|
|
|
+ | ArrayDef (_, dims) -> [Allocate (name, dims, loc)]
|
|
|
| _ -> []
|
|
|
in
|
|
|
- (* variable initialisation: create assign statement *)
|
|
|
- let stats = match init with
|
|
|
+ (* initialisation: create assign statement *)
|
|
|
+ let add = match init with
|
|
|
| Some value -> alloc @ [Assign (name, value, loc)]
|
|
|
| None -> alloc
|
|
|
in
|
|
|
- VarDec (ctype, name, None, loc) :: (trav (inits @ stats) t)
|
|
|
+ VarDec (ctype, name, None, loc) :: (trav (inits @ add) t)
|
|
|
|
|
|
(* initialisations need to be placed after local functions *)
|
|
|
| (FunDef (_, _, _, _, _, _) as h) :: t ->
|
|
|
@@ -48,7 +51,7 @@ let rec var_init = function
|
|
|
|
|
|
(* Move global initialisations to __init function *)
|
|
|
| Program (decls, loc) ->
|
|
|
- let decls = flatten (List.map var_init decls) in
|
|
|
+ let decls = flatten_blocks (List.map var_init decls) in
|
|
|
let rec trav assigns = function
|
|
|
| [] -> (assigns, [])
|
|
|
| (Assign (_, _, _) as h) :: t -> trav (assigns @ [h]) t
|