package js_of_ocaml-compiler
Compiler from OCaml bytecode to JavaScript
Install
Dune Dependency
Authors
Maintainers
Sources
js_of_ocaml-5.7.0.tbz
sha256=cdf1dea0d9397a498b630abe8014908b6dd83b10ee0c7ff5777a6443a62983be
sha512=5cf7d883c97f4b81ab73687c8f29b624acaef28c002b5d739108954973671dd5fb4f869d8fedb485b4b17bc8aa10d8b55b30ae7ace21a9350c2a1ac59b28eb60
doc/src/js_of_ocaml-compiler/var_printer.ml.html
Source file var_printer.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
(* Js_of_ocaml compiler * http://www.ocsigen.org/js_of_ocaml/ * Copyright (C) 2013 Hugo Heuzard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, with linking exception; * either version 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) open! Stdlib module Alphabet = struct type t = { c1 : string ; c1_len : int ; cn : string ; cn_len : int } let create ~c1 ~cn = { c1; c1_len = String.length c1; cn; cn_len = String.length cn } let javascript = create ~c1:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$" ~cn:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$" let rec size t x acc = if x < t.c1_len then 1 + acc else size t ((x - t.c1_len) / t.cn_len) (acc + 1) let to_string (t : t) (x : int) = let size = size t x 0 in let buf = Bytes.create size in let rec loop i x = match i with | 0 -> assert (x < t.c1_len); Bytes.set buf i t.c1.[x]; Bytes.unsafe_to_string buf | i -> let x = x - t.c1_len in Bytes.set buf i t.cn.[x mod t.cn_len]; loop (pred i) (x / t.cn_len) in loop (size - 1) x end type t = { names : (int, string) Hashtbl.t ; known : (int, string) Hashtbl.t ; cache : (int * int, string) Hashtbl.t ; alphabet : Alphabet.t ; mutable last : int ; mutable pretty : bool ; mutable stable : bool } let name_raw t v nm = Hashtbl.replace t.names v nm let merge_name n1 n2 = match n1, n2 with | "", n2 -> n2 | n1, "" -> n1 | n1, n2 -> if generated_name n1 then n2 else if generated_name n2 then n1 else if String.length n1 > String.length n2 then n1 else n2 let propagate_name t v v' = try let name = Hashtbl.find t.names v in match Hashtbl.find t.names v' with | exception Not_found -> name_raw t v' name | name' -> name_raw t v' (merge_name name name') with Not_found -> () let name t v nm_orig = let len = String.length nm_orig in if len > 0 then ( let buf = Buffer.create (String.length nm_orig) in let idx = ref 0 in while !idx < len && not (Char.is_alpha nm_orig.[!idx]) do incr idx done; let pending = ref false in if !idx >= len then ( pending := true; idx := 0); for i = !idx to len - 1 do if Char.is_alpha nm_orig.[i] || Char.is_num nm_orig.[i] then ( if !pending then Buffer.add_char buf '_'; Buffer.add_char buf nm_orig.[i]; pending := false) else pending := true done; let str = Buffer.contents buf in let str = match str, nm_orig with | "", ">>=" -> "symbol_bind" | "", ">>|" -> "symbol_map" | "", _ -> "symbol" | str, _ -> str in (* protect against large names *) let max_len = 30 in let str = if String.length str > max_len then String.sub str ~pos:0 ~len:max_len else str in name_raw t v str) let get_name t v = try Some (Hashtbl.find t.names v) with Not_found -> None let format_var t i x = let s = Alphabet.to_string t.alphabet x in if t.stable then Format.sprintf "v%d" i else if t.pretty then Format.sprintf "_%s_" s else s let reserved = ref StringSet.empty let add_reserved s = reserved := StringSet.add s !reserved let _ = reserved := StringSet.union !reserved Reserved.keyword let get_reserved () = !reserved let is_reserved s = StringSet.mem s !reserved let rec to_string t ?origin i = let origin = match origin with | Some i when t.pretty -> i | _ -> i in try Hashtbl.find t.cache (i, origin) with Not_found -> let name = try Hashtbl.find t.known i with Not_found -> t.last <- t.last + 1; let j = t.last in let s = format_var t i j in if is_reserved s then to_string t i else ( Hashtbl.add t.known i s; s) in let name = if t.pretty then try let nm = Hashtbl.find t.names origin in nm ^ name with Not_found -> name else name in Hashtbl.add t.cache (i, origin) name; name let set_pretty t b = t.pretty <- b let set_stable t b = t.stable <- b let reset t = Hashtbl.clear t.names; Hashtbl.clear t.known; Hashtbl.clear t.cache; t.last <- -1 let create ?(pretty = false) ?(stable = false) alphabet = let t = { names = Hashtbl.create 107 ; known = Hashtbl.create 1001 ; cache = Hashtbl.create 1001 ; alphabet ; last = -1 ; pretty ; stable } in t
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>