-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(engine/phases): intro
simplify_hoisting
This commit introduces a phase that simplifies the pattern: ``` /// Note `let` below is NOT monadic let hoistN = e in body ``` into `body[hoistN/e]`, when: - the local ident `hoistN` is of kind `SideEffectHoistVar` (we know it was produced by the side effect utils module); - `body` contains exactly one occurence of `hoistN`. That simplifies greatly the extracted code.
- Loading branch information
Showing
7 changed files
with
85 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
open! Prelude | ||
|
||
module Make (F : Features.T) = | ||
Phase_utils.MakeMonomorphicPhase | ||
(F) | ||
(struct | ||
let phase_id = Diagnostics.Phase.SimplifyHoisting | ||
|
||
open Ast.Make (F) | ||
module U = Ast_utils.Make (F) | ||
module Visitors = Ast_visitors.Make (F) | ||
|
||
module Error = Phase_utils.MakeError (struct | ||
let ctx = Diagnostics.Context.Phase phase_id | ||
end) | ||
|
||
let inline_matches = | ||
object | ||
inherit [_] Visitors.map as super | ||
|
||
method! visit_expr () e = | ||
match e with | ||
| { | ||
e = | ||
Let | ||
{ | ||
monadic = None; | ||
lhs = | ||
{ | ||
p = | ||
PBinding | ||
{ | ||
mut = Immutable; | ||
mode = ByValue; | ||
var; | ||
subpat = None; | ||
_; | ||
}; | ||
_; | ||
}; | ||
rhs; | ||
body; | ||
}; | ||
_; | ||
} | ||
when Local_ident.is_side_effect_hoist_var var -> | ||
let body, count = | ||
(object | ||
inherit [_] Visitors.mapreduce as super | ||
method zero = 0 | ||
method plus = ( + ) | ||
|
||
method! visit_expr () e = | ||
match e.e with | ||
| LocalVar v when [%eq: Local_ident.t] v var -> (rhs, 1) | ||
| _ -> super#visit_expr () e | ||
end) | ||
#visit_expr | ||
() body | ||
in | ||
if [%eq: int] count 1 then body else super#visit_expr () e | ||
| _ -> super#visit_expr () e | ||
end | ||
|
||
let ditems = List.map ~f:(inline_matches#visit_item ()) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(** This phase rewrites `let pat = match ... { ... => ..., ... => return ... }; e` | ||
into `match ... { ... => let pat = ...; e}`. *) | ||
|
||
module Make : Phase_utils.UNCONSTRAINTED_MONOMORPHIC_PHASE |