Skip to content

Commit

Permalink
Add TCO support for reversed branches
Browse files Browse the repository at this point in the history
  • Loading branch information
juusaw committed Apr 16, 2019
1 parent 7227904 commit 6db9775
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ struct
let table' = ref table in
let b t = match (evalExp cond_exp t depth) with
VAL [] -> false
| VAL _ -> (*print_string (printVal (snd (List.hd_exn (List.tl_exn t)))); *) true
| VAL _ -> true
| _ -> raise (RunError ("illegal arg to if", p)) in
let () = while b !table' do
table' := (List.fold (List.zip_exn params arg_exps) ~init:[] ~f:(fun l e ->
Expand Down
6 changes: 5 additions & 1 deletion src/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,15 @@ struct
| Comp (exp, s1, s2, pos) -> string_of_exp exp

let optimize_tco (prog: program): program =
let names_equal s1 s2 = Int.equal 0 (String.compare s1 s2) in
let decls, e = prog in
let decls' = List.map decls ~f:(fun (name, decl) ->
match decl with
Func (params, IF (condition_exp, CALL (fn_name, arg_exps, _), else_exp, p1), p2) when Int.equal 0 (String.compare name fn_name) ->
Func (params, IF (condition_exp, CALL (fn_name, arg_exps, _), else_exp, p1), p2) when names_equal name fn_name ->
(name, Func (params, LOOP (arg_exps, condition_exp, else_exp, params, p1), p2))
| Func (params, IF (condition_exp, then_exp, CALL (fn_name, arg_exps, _), p1), p2) when names_equal name fn_name ->
let inverse_condition = IF (condition_exp, EMPTY, NUM (1, p2), p2) in
(name, Func (params, LOOP (arg_exps, inverse_condition, then_exp, params, p1), p2))
| d -> (name, d)
) in
(decls', e)
Expand Down

0 comments on commit 6db9775

Please sign in to comment.