comparison src/prim.sml @ 2048:4d64af730e35

Differentiate between HTML and normal string literals
author Adam Chlipala <adam@chlipala.net>
date Fri, 01 Aug 2014 15:44:17 -0400
parents 3e7c7e200713
children
comparison
equal deleted inserted replaced
2047:6be31671911b 2048:4d64af730e35
1 (* Copyright (c) 2008, Adam Chlipala 1 (* Copyright (c) 2008, 2014, Adam Chlipala
2 * All rights reserved. 2 * All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met: 5 * modification, are permitted provided that the following conditions are met:
6 * 6 *
25 * POSSIBILITY OF SUCH DAMAGE. 25 * POSSIBILITY OF SUCH DAMAGE.
26 *) 26 *)
27 27
28 structure Prim :> PRIM = struct 28 structure Prim :> PRIM = struct
29 29
30 datatype string_mode = Normal | Html
31
30 datatype t = 32 datatype t =
31 Int of Int64.int 33 Int of Int64.int
32 | Float of Real64.real 34 | Float of Real64.real
33 | String of string 35 | String of string_mode * string
34 | Char of char 36 | Char of char
35 37
36 open Print.PD 38 open Print.PD
37 open Print 39 open Print
38 40
39 fun p_t t = 41 fun p_t t =
40 case t of 42 case t of
41 Int n => string (Int64.toString n) 43 Int n => string (Int64.toString n)
42 | Float n => string (Real64.toString n) 44 | Float n => string (Real64.toString n)
43 | String s => box [string "\"", string (String.toString s), string "\""] 45 | String (_, s) => box [string "\"", string (String.toString s), string "\""]
44 | Char ch => box [string "#\"", string (String.toString (String.str ch)), string "\""] 46 | Char ch => box [string "#\"", string (String.toString (String.str ch)), string "\""]
45 47
46 fun int2s n = 48 fun int2s n =
47 if Int64.compare (n, Int64.fromInt 0) = LESS then 49 if Int64.compare (n, Int64.fromInt 0) = LESS then
48 "-" ^ Int64.toString (Int64.~ n) ^ "LL" 50 "-" ^ Int64.toString (Int64.~ n) ^ "LL"
59 61
60 fun toString t = 62 fun toString t =
61 case t of 63 case t of
62 Int n => int2s' n 64 Int n => int2s' n
63 | Float n => float2s n 65 | Float n => float2s n
64 | String s => s 66 | String (_, s) => s
65 | Char ch => str ch 67 | Char ch => str ch
66 68
67 fun pad (n, ch, s) = 69 fun pad (n, ch, s) =
68 if size s >= n then 70 if size s >= n then
69 s 71 s
84 86
85 fun p_t_GCC t = 87 fun p_t_GCC t =
86 case t of 88 case t of
87 Int n => string (int2s n) 89 Int n => string (int2s n)
88 | Float n => string (float2s n) 90 | Float n => string (float2s n)
89 | String s => box [string "\"", string (toCString s), string "\""] 91 | String (_, s) => box [string "\"", string (toCString s), string "\""]
90 | Char ch => box [string "'", string (toCChar ch), string "'"] 92 | Char ch => box [string "'", string (toCChar ch), string "'"]
91 93
92 fun equal x = 94 fun equal x =
93 case x of 95 case x of
94 (Int n1, Int n2) => n1 = n2 96 (Int n1, Int n2) => n1 = n2
95 | (Float n1, Float n2) => Real64.== (n1, n2) 97 | (Float n1, Float n2) => Real64.== (n1, n2)
96 | (String s1, String s2) => s1 = s2 98 | (String (_, s1), String (_, s2)) => s1 = s2
97 | (Char ch1, Char ch2) => ch1 = ch2 99 | (Char ch1, Char ch2) => ch1 = ch2
98 100
99 | _ => false 101 | _ => false
100 102
101 fun compare (p1, p2) = 103 fun compare (p1, p2) =
106 108
107 | (Float n1, Float n2) => Real64.compare (n1, n2) 109 | (Float n1, Float n2) => Real64.compare (n1, n2)
108 | (Float _, _) => LESS 110 | (Float _, _) => LESS
109 | (_, Float _) => GREATER 111 | (_, Float _) => GREATER
110 112
111 | (String n1, String n2) => String.compare (n1, n2) 113 | (String (_, n1), String (_, n2)) => String.compare (n1, n2)
112 | (String _, _) => LESS 114 | (String _, _) => LESS
113 | (_, String _) => GREATER 115 | (_, String _) => GREATER
114 116
115 | (Char ch1, Char ch2) => Char.compare (ch1, ch2) 117 | (Char ch1, Char ch2) => Char.compare (ch1, ch2)
116 118