open Ast open Trav open Stringify let rec var_init node = let move_inits body = let rec trav inits = function (* local declarations: collect initialisations *) | VarDec (ctype, name, Some init) :: t -> VarDec (ctype, name, None) :: (trav (inits @ [Assign (name, init)]) t) | (VarDec (_, _, None) as h) :: t | (FunDef (_, _, _, _, _) as h) :: t -> h :: (trav inits t) (* rest of function body: recurse *) | rest -> inits @ (List.map var_init rest) in trav [] body in match node with | FunDef (export, ret_type, name, params, body) -> FunDef (export, ret_type, name, params, move_inits body) | _ -> transform var_init node let rec phase repr = let _ = print_endline "- Var init" in match repr with | Node (node, verbose) -> Node (var_init node, verbose) | _ -> failwith "invalid input for this phase"