summaryrefslogtreecommitdiff
path: root/src/name_js.sml
blob: 70ac000cd16f7102f4366d39f1f58de4d5849a99 (plain)
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
(* Copyright (c) 2012, Adam Chlipala
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - The names of contributors may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *)

(* Phase that introduces names for fragments of JavaScript code, so that they
 * may be moved to app.js and not repeated in each generated page *)

structure NameJS :> NAME_JS = struct

open Mono

structure U = MonoUtil
structure IS = IntBinarySet

val freeVars = U.Exp.foldB {typ = #2,
                            exp = fn (free, e, vs) =>
                                     case e of
                                         ERel n =>
                                         if n < free then
                                             vs
                                         else
                                             IS.add (vs, n - free)
                                       | _ => vs,
                            bind = fn (free, b) =>
                                      case b of
                                          U.Exp.RelE _ => free+1
                                        | _ => free}
               0 IS.empty

fun index (ls, v) =
    case ls of
        [] => raise Fail "NameJs.index"
      | v' :: ls' => if v = v' then 0 else 1 + index (ls', v)

fun squish vs = U.Exp.mapB {typ = fn x => x,
                            exp = fn free => fn e =>
                                                case e of
                                                    ERel n =>
                                                    if n < free then
                                                        e
                                                    else
                                                        ERel (free + index (vs, n - free) + 1)
                                                  | _ => e,
                            bind = fn (free, b) =>
                                      case b of
                                          U.Exp.RelE _ => free+1
                                        | _ => free}
                           0

fun rewrite file =
    let
        val (file, _) = ListUtil.foldlMapConcat (fn (d, nextName) =>
                                                    let
                                                        val (d, (nextName, newDs)) =
                                                            U.Decl.foldMapB {typ = fn x => x,
                                                                             decl = fn (_, e, s) => (e, s),
                                                                             exp = fn (env, e, st as (nextName, newDs)) =>
                                                                                      case e of
                                                                                          EJavaScript (mode, e') =>
                                                                                          (case mode of
                                                                                               Source _ => (e, st)
                                                                                             | _ =>
                                                                                               let
                                                                                                   fun isTrulySimple (e, _) =
                                                                                                       case e of
                                                                                                           ERel _ => true
                                                                                                         | ENamed _ => true
                                                                                                         | ERecord [] => true
                                                                                                         | _ => false

                                                                                                   fun isAlreadySimple e =
                                                                                                       case #1 e of
                                                                                                           EApp (e, arg) => isTrulySimple arg andalso isAlreadySimple e
                                                                                                         | _ => isTrulySimple e
                                                                                               in
                                                                                                   if isAlreadySimple e' then
                                                                                                       (e, st)
                                                                                                   else
                                                                                                       let
                                                                                                           val loc = #2 e'

                                                                                                           val vs = freeVars e'
                                                                                                           val vs = IS.listItems vs
                                                                                                                    
                                                                                                           val x = "script" ^ Int.toString nextName
                                                                                                                   
                                                                                                           val un = (TRecord [], loc)
                                                                                                           val s = (TFfi ("Basis", "string"), loc)
                                                                                                           val base = (TFun (un, s), loc)
                                                                                                           val t = foldl (fn (n, t) => (TFun (#2 (List.nth (env, n)), t), loc)) base vs
                                                                                                           val e' = squish vs e'
                                                                                                           val e' = (EAbs ("_", un, s, e'), loc)
                                                                                                           val (e', _) = foldl (fn (n, (e', t)) =>
                                                                                                                                   let
                                                                                                                                       val (x, this) = List.nth (env, n)
                                                                                                                                   in
                                                                                                                                       ((EAbs (x, this, t, e'), loc),
                                                                                                                                        (TFun (this, t), loc))
                                                                                                                                   end) (e', base) vs
                                                                                                           val d = (x, nextName, t, e', "<script>")

                                                                                                           val e = (ENamed nextName, loc)
                                                                                                           val e = foldr (fn (n, e) => (EApp (e, (ERel n, loc)), loc)) e vs
                                                                                                           val e = (EApp (e, (ERecord [], loc)), loc)
                                                                                                           val e = EJavaScript (Script, e)
                                                                                                       in
                                                                                                           (e, (nextName+1, d :: newDs))
                                                                                                       end
                                                                                               end)
                                                                                        | _ => (e, st),
                                                                             bind = fn (env, b) =>
                                                                                       case b of
                                                                                           U.Decl.RelE x => x :: env
                                                                                         | _ => env}
                                                            [] (nextName, []) d
                                                    in
                                                        (case newDs of
                                                             [] => [d]
                                                           | _ => case #1 d of
                                                                      DValRec vis => [(DValRec (vis @ newDs), #2 d)]
                                                                    | _ => List.revAppend (map (fn vi => (DVal vi, #2 d)) newDs, [d]),
                                                         nextName)
                                                    end) (U.File.maxName file + 1) file
    in
        file
    end

end