desug.ml 987 B

1234567891011121314151617181920212223242526272829
  1. open Ast
  2. open Trav
  3. open Stringify
  4. let rec var_init node =
  5. let move_inits body =
  6. let rec trav inits = function
  7. (* local declarations: collect initialisations *)
  8. | VarDec (ctype, name, Some init) :: t ->
  9. VarDec (ctype, name, None) :: (trav (inits @ [Assign (name, init)]) t)
  10. | (VarDec (_, _, None) as h) :: t
  11. | (FunDef (_, _, _, _, _) as h) :: t ->
  12. h :: (trav inits t)
  13. (* rest of function body: recurse *)
  14. | rest -> inits @ (List.map var_init rest)
  15. in trav [] body
  16. in
  17. match node with
  18. | FunDef (export, ret_type, name, params, body) ->
  19. FunDef (export, ret_type, name, params, move_inits body)
  20. | _ -> transform var_init node
  21. let rec phase repr =
  22. let _ = print_endline "- Var init" in
  23. match repr with
  24. | Node (node, verbose) ->
  25. Node (var_init node, verbose)
  26. | _ -> failwith "invalid input for this phase"