annotate src/prepare.sml @ 288:4260ad920c36

Converting string to int
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 11:33:13 -0400
parents c0e4ac23522d
children 550100a44cca
rev   line source
adamc@282 1 (* Copyright (c) 2008, Adam Chlipala
adamc@282 2 * All rights reserved.
adamc@282 3 *
adamc@282 4 * Redistribution and use in source and binary forms, with or without
adamc@282 5 * modification, are permitted provided that the following conditions are met:
adamc@282 6 *
adamc@282 7 * - Redistributions of source code must retain the above copyright notice,
adamc@282 8 * this list of conditions and the following disclaimer.
adamc@282 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@282 10 * this list of conditions and the following disclaimer in the documentation
adamc@282 11 * and/or other materials provided with the distribution.
adamc@282 12 * - The names of contributors may not be used to endorse or promote products
adamc@282 13 * derived from this software without specific prior written permission.
adamc@282 14 *
adamc@282 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@282 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@282 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@282 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@282 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@282 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@282 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@282 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@282 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@282 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@282 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@282 26 *)
adamc@282 27
adamc@282 28 structure Prepare :> PREPARE = struct
adamc@282 29
adamc@282 30 open Cjr
adamc@282 31
adamc@282 32 fun prepString (e, ss, n) =
adamc@282 33 case #1 e of
adamc@282 34 EPrim (Prim.String s) =>
adamc@282 35 SOME (s :: ss, n)
adamc@282 36 | EFfiApp ("Basis", "strcat", [e1, e2]) =>
adamc@282 37 (case prepString (e1, ss, n) of
adamc@282 38 NONE => NONE
adamc@282 39 | SOME (ss, n) => prepString (e2, ss, n))
adamc@282 40 | EFfiApp ("Basis", "sqlifyInt", [e]) =>
adamc@282 41 SOME ("$" ^ Int.toString (n + 1) ^ "::int8" :: ss, n + 1)
adamc@282 42 | EFfiApp ("Basis", "sqlifyFloat", [e]) =>
adamc@282 43 SOME ("$" ^ Int.toString (n + 1) ^ "::float8" :: ss, n + 1)
adamc@282 44 | EFfiApp ("Basis", "sqlifyString", [e]) =>
adamc@282 45 SOME ("$" ^ Int.toString (n + 1) ^ "::text" :: ss, n + 1)
adamc@282 46 | EFfiApp ("Basis", "sqlifyBool", [e]) =>
adamc@282 47 SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@282 48
adamc@282 49 | _ => NONE
adamc@282 50
adamc@282 51 fun prepExp (e as (_, loc), sns) =
adamc@282 52 case #1 e of
adamc@282 53 EPrim _ => (e, sns)
adamc@282 54 | ERel _ => (e, sns)
adamc@282 55 | ENamed _ => (e, sns)
adamc@282 56 | ECon (_, _, NONE) => (e, sns)
adamc@282 57 | ECon (dk, pc, SOME e) =>
adamc@282 58 let
adamc@282 59 val (e, sns) = prepExp (e, sns)
adamc@282 60 in
adamc@282 61 ((ECon (dk, pc, SOME e), loc), sns)
adamc@282 62 end
adamc@282 63 | EFfi _ => (e, sns)
adamc@282 64 | EFfiApp (m, x, es) =>
adamc@282 65 let
adamc@282 66 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 67 in
adamc@282 68 ((EFfiApp (m, x, es), loc), sns)
adamc@282 69 end
adamc@282 70 | EApp (e1, e2) =>
adamc@282 71 let
adamc@282 72 val (e1, sns) = prepExp (e1, sns)
adamc@282 73 val (e2, sns) = prepExp (e2, sns)
adamc@282 74 in
adamc@282 75 ((EApp (e1, e2), loc), sns)
adamc@282 76 end
adamc@282 77
adamc@282 78 | ERecord (rn, xes) =>
adamc@282 79 let
adamc@282 80 val (xes, sns) = ListUtil.foldlMap (fn ((x, e), sns) =>
adamc@282 81 let
adamc@282 82 val (e, sns) = prepExp (e, sns)
adamc@282 83 in
adamc@282 84 ((x, e), sns)
adamc@282 85 end) sns xes
adamc@282 86 in
adamc@282 87 ((ERecord (rn, xes), loc), sns)
adamc@282 88 end
adamc@282 89 | EField (e, s) =>
adamc@282 90 let
adamc@282 91 val (e, sns) = prepExp (e, sns)
adamc@282 92 in
adamc@282 93 ((EField (e, s), loc), sns)
adamc@282 94 end
adamc@282 95
adamc@282 96 | ECase (e, pes, ts) =>
adamc@282 97 let
adamc@282 98 val (e, sns) = prepExp (e, sns)
adamc@282 99 val (pes, sns) = ListUtil.foldlMap (fn ((p, e), sns) =>
adamc@282 100 let
adamc@282 101 val (e, sns) = prepExp (e, sns)
adamc@282 102 in
adamc@282 103 ((p, e), sns)
adamc@282 104 end) sns pes
adamc@282 105 in
adamc@282 106 ((ECase (e, pes, ts), loc), sns)
adamc@282 107 end
adamc@282 108
adamc@283 109 | EError (e, t) =>
adamc@283 110 let
adamc@283 111 val (e, sns) = prepExp (e, sns)
adamc@283 112 in
adamc@283 113 ((EError (e, t), loc), sns)
adamc@283 114 end
adamc@283 115
adamc@282 116 | EWrite e =>
adamc@282 117 let
adamc@282 118 val (e, sns) = prepExp (e, sns)
adamc@282 119 in
adamc@282 120 ((EWrite e, loc), sns)
adamc@282 121 end
adamc@282 122 | ESeq (e1, e2) =>
adamc@282 123 let
adamc@282 124 val (e1, sns) = prepExp (e1, sns)
adamc@282 125 val (e2, sns) = prepExp (e2, sns)
adamc@282 126 in
adamc@282 127 ((ESeq (e1, e2), loc), sns)
adamc@282 128 end
adamc@282 129 | ELet (x, t, e1, e2) =>
adamc@282 130 let
adamc@282 131 val (e1, sns) = prepExp (e1, sns)
adamc@282 132 val (e2, sns) = prepExp (e2, sns)
adamc@282 133 in
adamc@282 134 ((ELet (x, t, e1, e2), loc), sns)
adamc@282 135 end
adamc@282 136
adamc@282 137 | EQuery {exps, tables, rnum, state, query, body, initial, ...} =>
adamc@282 138 (case prepString (query, [], 0) of
adamc@282 139 NONE => (e, sns)
adamc@282 140 | SOME (ss, n) =>
adamc@282 141 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@282 142 state = state, query = query, body = body,
adamc@282 143 initial = initial, prepared = SOME (#2 sns)}, loc),
adamc@282 144 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1)))
adamc@282 145
adamc@282 146 fun prepDecl (d as (_, loc), sns) =
adamc@282 147 case #1 d of
adamc@282 148 DStruct _ => (d, sns)
adamc@282 149 | DDatatype _ => (d, sns)
adamc@282 150 | DDatatypeForward _ => (d, sns)
adamc@282 151 | DVal (x, n, t, e) =>
adamc@282 152 let
adamc@282 153 val (e, sns) = prepExp (e, sns)
adamc@282 154 in
adamc@282 155 ((DVal (x, n, t, e), loc), sns)
adamc@282 156 end
adamc@282 157 | DFun (x, n, xts, t, e) =>
adamc@282 158 let
adamc@282 159 val (e, sns) = prepExp (e, sns)
adamc@282 160 in
adamc@282 161 ((DFun (x, n, xts, t, e), loc), sns)
adamc@282 162 end
adamc@282 163 | DFunRec fs =>
adamc@282 164 let
adamc@282 165 val (fs, sns) = ListUtil.foldlMap (fn ((x, n, xts, t, e), sns) =>
adamc@282 166 let
adamc@282 167 val (e, sns) = prepExp (e, sns)
adamc@282 168 in
adamc@282 169 ((x, n, xts, t, e), sns)
adamc@282 170 end) sns fs
adamc@282 171 in
adamc@282 172 ((DFunRec fs, loc), sns)
adamc@282 173 end
adamc@282 174
adamc@282 175 | DTable _ => (d, sns)
adamc@282 176 | DDatabase _ => (d, sns)
adamc@282 177 | DPreparedStatements _ => (d, sns)
adamc@282 178
adamc@282 179 fun prepare (ds, ps) =
adamc@282 180 let
adamc@282 181 val (ds, (sns, _)) = ListUtil.foldlMap prepDecl ([], 0) ds
adamc@282 182 in
adamc@282 183 ((DPreparedStatements (rev sns), ErrorMsg.dummySpan) :: ds, ps)
adamc@282 184 end
adamc@282 185
adamc@282 186 end
adamc@282 187