annotate src/prim.sml @ 1121:0cee0c8d8c37

Support for protocol-specific expunger dispatch
author Adam Chlipala <adamc@hcoop.net>
date Sun, 10 Jan 2010 10:40:57 -0500
parents b06a2a65e670
children d7d5b167f267
rev   line source
adamc@14 1 (* Copyright (c) 2008, Adam Chlipala
adamc@14 2 * All rights reserved.
adamc@14 3 *
adamc@14 4 * Redistribution and use in source and binary forms, with or without
adamc@14 5 * modification, are permitted provided that the following conditions are met:
adamc@14 6 *
adamc@14 7 * - Redistributions of source code must retain the above copyright notice,
adamc@14 8 * this list of conditions and the following disclaimer.
adamc@14 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@14 10 * this list of conditions and the following disclaimer in the documentation
adamc@14 11 * and/or other materials provided with the distribution.
adamc@14 12 * - The names of contributors may not be used to endorse or promote products
adamc@14 13 * derived from this software without specific prior written permission.
adamc@14 14 *
adamc@14 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@14 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@14 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@14 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@14 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@14 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@14 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@14 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@14 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@14 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@14 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@14 26 *)
adamc@14 27
adamc@14 28 structure Prim :> PRIM = struct
adamc@14 29
adamc@14 30 datatype t =
adamc@14 31 Int of Int64.int
adamc@14 32 | Float of Real64.real
adamc@14 33 | String of string
adamc@821 34 | Char of char
adamc@14 35
adamc@14 36 open Print.PD
adamc@14 37 open Print
adamc@14 38
adamc@14 39 fun p_t t =
adamc@14 40 case t of
adamc@14 41 Int n => string (Int64.toString n)
adamc@14 42 | Float n => string (Real64.toString n)
adamc@95 43 | String s => box [string "\"", string (String.toString s), string "\""]
adamc@942 44 | Char ch => box [string "#\"", string (String.toString (String.str ch)), string "\""]
adamc@14 45
adamc@276 46 fun int2s n =
adamc@276 47 if Int64.compare (n, Int64.fromInt 0) = LESS then
adamc@276 48 "-" ^ Int64.toString (Int64.~ n) ^ "LL"
adamc@276 49 else
adamc@276 50 Int64.toString n ^ "LL"
adamc@276 51
adamc@582 52 fun int2s' n =
adamc@582 53 if Int64.compare (n, Int64.fromInt 0) = LESS then
adamc@582 54 "-" ^ Int64.toString (Int64.~ n)
adamc@582 55 else
adamc@582 56 Int64.toString n
adamc@582 57
adamc@276 58 fun float2s n =
adamc@276 59 if Real64.compare (n, Real64.fromInt 0) = LESS then
adamc@285 60 "-" ^ Real64.toString (Real64.~ n)
adamc@276 61 else
adamc@285 62 Real64.toString n
adamc@276 63
adamc@567 64 fun toString t =
adamc@567 65 case t of
adamc@582 66 Int n => int2s' n
adamc@567 67 | Float n => float2s n
adamc@567 68 | String s => s
adamc@821 69 | Char ch => str ch
adamc@567 70
adamc@1053 71 fun pad (n, ch, s) =
adamc@1053 72 if size s >= n then
adamc@1053 73 s
adamc@1053 74 else
adamc@1053 75 str ch ^ pad (n-1, ch, s)
adamc@1053 76
adamc@1054 77 val gccify = String.translate (fn ch =>
adamc@1054 78 case ch of
adamc@1054 79 #"\"" => "\\\""
adamc@1054 80 | #"\\" => "\\\\"
adamc@1054 81 | #"'" => "\\'"
adamc@1054 82 | #"\n" => "\\n"
adamc@1054 83 | #"\r" => "\\r"
adamc@1054 84 | #"\t" => "\\t"
adamc@1054 85 | #" " => " "
adamc@1054 86 | _ =>
adamc@1054 87 if Char.isPrint ch then
adamc@1054 88 str ch
adamc@1054 89 else
adamc@1054 90 "\\" ^ pad (3, #"0", Int.fmt StringCvt.OCT (ord ch)))
adamc@1053 91
adamc@276 92 fun p_t_GCC t =
adamc@276 93 case t of
adamc@276 94 Int n => string (int2s n)
adamc@276 95 | Float n => string (float2s n)
adamc@1053 96 | String s => box [string "\"", string (gccify s), string "\""]
adamc@1053 97 | Char ch => box [string "'", string (gccify (str ch)), string "'"]
adamc@276 98
adamc@183 99 fun equal x =
adamc@183 100 case x of
adamc@183 101 (Int n1, Int n2) => n1 = n2
adamc@183 102 | (Float n1, Float n2) => Real64.== (n1, n2)
adamc@183 103 | (String s1, String s2) => s1 = s2
adamc@821 104 | (Char ch1, Char ch2) => ch1 = ch2
adamc@183 105
adamc@183 106 | _ => false
adamc@183 107
adamc@479 108 fun compare (p1, p2) =
adamc@479 109 case (p1, p2) of
adamc@479 110 (Int n1, Int n2) => Int64.compare (n1, n2)
adamc@479 111 | (Int _, _) => LESS
adamc@479 112 | (_, Int _) => GREATER
adamc@479 113
adamc@479 114 | (Float n1, Float n2) => Real64.compare (n1, n2)
adamc@479 115 | (Float _, _) => LESS
adamc@821 116 | (_, Float _) => GREATER
adamc@479 117
adamc@479 118 | (String n1, String n2) => String.compare (n1, n2)
adamc@821 119 | (String _, _) => LESS
adamc@821 120 | (_, String _) => GREATER
adamc@821 121
adamc@821 122 | (Char ch1, Char ch2) => Char.compare (ch1, ch2)
adamc@479 123
adamc@14 124 end