|
@@ -178,13 +178,17 @@ let rec move_inits = function
|
|
|
| node -> transform_children move_inits node
|
|
| node -> transform_children move_inits node
|
|
|
|
|
|
|
|
let for_to_while node =
|
|
let for_to_while node =
|
|
|
- let rec replace_var var replacement = function
|
|
|
|
|
|
|
+ let rec replace_var var replacement node =
|
|
|
|
|
+ let trav = (replace_var var replacement) in
|
|
|
|
|
+ match node with
|
|
|
| Var (name, None, ann) when name = var ->
|
|
| Var (name, None, ann) when name = var ->
|
|
|
Var (replacement, None, ann)
|
|
Var (replacement, None, ann)
|
|
|
- | node -> transform_children (replace_var var replacement) node
|
|
|
|
|
|
|
+ | For (counter, start, stop, step, body, ann) when counter = var ->
|
|
|
|
|
+ For (replacement, trav start, trav stop, trav step, trav body, ann)
|
|
|
|
|
+ | node ->
|
|
|
|
|
+ transform_children trav node
|
|
|
in
|
|
in
|
|
|
- let rec traverse = function
|
|
|
|
|
- (* DISABLED, inline VarDec nodes are supported
|
|
|
|
|
|
|
+ let rec traverse new_vars = function
|
|
|
| FunDef (export, ret_type, name, params, body, ann) ->
|
|
| FunDef (export, ret_type, name, params, body, ann) ->
|
|
|
let rec place_decs decs = function
|
|
let rec place_decs decs = function
|
|
|
| Block (VarDecs lst :: tl) -> Block (VarDecs (decs @ lst) :: tl)
|
|
| Block (VarDecs lst :: tl) -> Block (VarDecs (decs @ lst) :: tl)
|
|
@@ -196,14 +200,13 @@ let for_to_while node =
|
|
|
let new_vardecs = List.map create_vardec !new_vars in
|
|
let new_vardecs = List.map create_vardec !new_vars in
|
|
|
let body = place_decs new_vardecs body in
|
|
let body = place_decs new_vardecs body in
|
|
|
FunDef (export, ret_type, name, params, body, ann)
|
|
FunDef (export, ret_type, name, params, body, ann)
|
|
|
- *)
|
|
|
|
|
|
|
|
|
|
(* Transform for-loops to while-loops *)
|
|
(* Transform for-loops to while-loops *)
|
|
|
| For (counter, start, stop, step, body, ann) ->
|
|
| For (counter, start, stop, step, body, ann) ->
|
|
|
let _i = fresh_id counter in
|
|
let _i = fresh_id counter in
|
|
|
let _stop = fresh_const "stop" in
|
|
let _stop = fresh_const "stop" in
|
|
|
let _step = fresh_const "step" in
|
|
let _step = fresh_const "step" in
|
|
|
- (*new_vars := !new_vars @ [_i; _stop; _step];*)
|
|
|
|
|
|
|
+ new_vars := !new_vars @ [_i; _stop; _step];
|
|
|
|
|
|
|
|
let vi = Var (_i, None, []) in
|
|
let vi = Var (_i, None, []) in
|
|
|
let vstop = Var (_stop, None, annof stop) in
|
|
let vstop = Var (_stop, None, annof stop) in
|
|
@@ -215,16 +218,13 @@ let for_to_while node =
|
|
|
[]
|
|
[]
|
|
|
) in
|
|
) in
|
|
|
Block [
|
|
Block [
|
|
|
- VarDec (Int, _i, None, annof start);
|
|
|
|
|
- VarDec (Int, _stop, None, annof stop);
|
|
|
|
|
- VarDec (Int, _step, None, annof step);
|
|
|
|
|
Assign (_i, None, start, annof start);
|
|
Assign (_i, None, start, annof start);
|
|
|
Assign (_stop, None, stop, annof stop);
|
|
Assign (_stop, None, stop, annof stop);
|
|
|
Assign (_step, None, step, annof step);
|
|
Assign (_step, None, step, annof step);
|
|
|
- While (cond, (Block (
|
|
|
|
|
- block_body (replace_var counter _i (traverse body)) @
|
|
|
|
|
|
|
+ traverse new_vars (While (cond, (Block (
|
|
|
|
|
+ block_body (replace_var counter _i body) @
|
|
|
[Assign (_i, None, Binop (Add, vi, vstep, []), [])]
|
|
[Assign (_i, None, Binop (Add, vi, vstep, []), [])]
|
|
|
- )), ann);
|
|
|
|
|
|
|
+ )), ann));
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
(* Transform while-loops to do-while loops in if-statements *)
|
|
(* Transform while-loops to do-while loops in if-statements *)
|
|
@@ -235,9 +235,9 @@ let for_to_while node =
|
|
|
Block [If (cond, Block [DoWhile (cond, body, ann)], ann)]
|
|
Block [If (cond, Block [DoWhile (cond, body, ann)], ann)]
|
|
|
*)
|
|
*)
|
|
|
|
|
|
|
|
- | node -> transform_children traverse node
|
|
|
|
|
|
|
+ | node -> transform_children (traverse new_vars) node
|
|
|
in
|
|
in
|
|
|
- traverse node
|
|
|
|
|
|
|
+ traverse (ref []) node
|
|
|
|
|
|
|
|
let rec sublist n = function
|
|
let rec sublist n = function
|
|
|
| [] when n > 0 -> raise (Invalid_argument "n")
|
|
| [] when n > 0 -> raise (Invalid_argument "n")
|