/tryocaml/js_of_ocaml-patched/compiler/subst.ml
OCaml | 103 lines | 70 code | 12 blank | 21 comment | 0 complexity | 3847031f925d15002e6171906d050507 MD5 | raw file
Possible License(s): GPL-2.0
1(* Js_of_ocaml compiler 2 * http://www.ocsigen.org/js_of_ocaml/ 3 * Copyright (C) 2010 Jérôme Vouillon 4 * Laboratoire PPS - CNRS Université Paris Diderot 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, with linking exception; 9 * either version 2.1 of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 *) 20 21open Code 22 23let expr s e = 24 match e with 25 Const _ | Constant _ -> 26 e 27 | Apply (f, l, n) -> 28 Apply (s f, List.map (fun x -> s x) l, n) 29 | Block (n, a) -> 30 Block (n, Array.map (fun x -> s x) a) 31 | Field (x, n) -> 32 Field (s x, n) 33 | Closure (l, pc) -> 34 Closure (l, pc) 35 | Prim (p, l) -> 36 Prim (p, List.map (fun x -> match x with Pv x -> Pv (s x) | Pc _ -> x) l) 37 38let instr s i = 39 match i with 40 Let (x, e) -> 41 Let (x, expr s e) 42 | Set_field (x, n, y) -> 43 Set_field (s x, n, s y) 44 | Offset_ref (x, n) -> 45 Offset_ref (s x, n) 46 | Array_set (x, y, z) -> 47 Array_set (s x, s y, s z) 48 49let instrs s l = List.map (fun i -> instr s i) l 50 51let subst_cont s (pc, arg) = (pc, List.map (fun x -> s x) arg) 52 53let last s l = 54 match l with 55 Stop -> 56 l 57 | Branch cont -> 58 Branch (subst_cont s cont) 59 | Pushtrap (cont1, x, cont2, pc) -> 60 Pushtrap (subst_cont s cont1, x, subst_cont s cont2, pc) 61 | Return x -> 62 Return (s x) 63 | Raise x -> 64 Raise (s x) 65 | Cond (c, x, cont1, cont2) -> 66 Cond (c, s x, subst_cont s cont1, subst_cont s cont2) 67 | Switch (x, a1, a2) -> 68 Switch (s x, 69 Array.map (fun cont -> subst_cont s cont) a1, 70 Array.map (fun cont -> subst_cont s cont) a2) 71 | Poptrap cont -> 72 Poptrap (subst_cont s cont) 73 74let program s (pc, blocks, free_pc) = 75 let blocks = 76 AddrMap.map 77 (fun block -> 78 { params = block.params; 79 handler = Util.opt_map 80 (fun (x, cont) -> (x, subst_cont s cont)) block.handler; 81 body = instrs s block.body; 82 branch = last s block.branch }) blocks 83 in 84 (pc, blocks, free_pc) 85 86(****) 87 88let from_array s = 89 fun x -> match s.(Var.idx x) with Some y -> y | None -> x 90 91(****) 92 93let rec build_mapping params args = 94 match params, args with 95 x :: params, y :: args -> 96 VarMap.add x y (build_mapping params args) 97 | [], _ -> 98 VarMap.empty 99 | _ -> 100 assert false 101 102let from_map m = 103 fun x -> try VarMap.find x m with Not_found -> x