1 : |
jhr |
26 |
(* literal.sml
|
2 : |
|
|
*
|
3 : |
|
|
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
|
4 : |
|
|
* All rights reserved.
|
5 : |
|
|
*
|
6 : |
|
|
* Literal values.
|
7 : |
|
|
*)
|
8 : |
|
|
|
9 : |
|
|
structure Literal : sig
|
10 : |
|
|
|
11 : |
|
|
datatype literal
|
12 : |
|
|
= Int of IntegerLit.integer (* integert literals *)
|
13 : |
|
|
| Float of FloatLit.float (* real literals *)
|
14 : |
|
|
| String of string (* strings *)
|
15 : |
|
|
| Bool of bool (* booleans *)
|
16 : |
|
|
|
17 : |
|
|
val toString : literal -> string
|
18 : |
|
|
|
19 : |
|
|
val same : (literal * literal) -> bool
|
20 : |
|
|
val compare : (literal * literal) -> order
|
21 : |
|
|
val hash : literal -> word
|
22 : |
|
|
|
23 : |
|
|
structure Tbl : MONO_HASH_TABLE where type Key.hash_key = literal
|
24 : |
|
|
|
25 : |
|
|
(* some standard constants *)
|
26 : |
|
|
val trueLit : literal
|
27 : |
|
|
val falseLit : literal
|
28 : |
|
|
|
29 : |
|
|
end = struct
|
30 : |
|
|
|
31 : |
|
|
datatype literal
|
32 : |
|
|
= Int of IntegerLit.integer (* integert literals *)
|
33 : |
|
|
| Float of FloatLit.float (* real literals *)
|
34 : |
|
|
| String of string (* strings *)
|
35 : |
|
|
| Bool of bool (* booleans *)
|
36 : |
|
|
|
37 : |
|
|
fun utf8ToStr s =
|
38 : |
|
|
concat(rev(UTF8.fold (fn (w, l) => wcharToStr w :: l) [] s))
|
39 : |
|
|
|
40 : |
|
|
fun toString (Int i) = IntegerLit.toString i
|
41 : |
|
|
| toString (Float flt) = FloatLit.toString flt
|
42 : |
|
|
| toString (String s) = concat["\"", String.toCString s, "\""]
|
43 : |
|
|
| toString (Bool true) = "true"
|
44 : |
|
|
| toString (Bool false) = "false"
|
45 : |
|
|
|
46 : |
|
|
fun same (Int i1, Int i2) = IntegerLit.same(i1, i2)
|
47 : |
|
|
| same (Float f1, Float f2) = FloatLit.same(f1, f2)
|
48 : |
|
|
| same (String s1, String s2) = (s1 = s2)
|
49 : |
|
|
| same (Bool b1, Bool b2) = (b1 = b2)
|
50 : |
|
|
| same _ = false
|
51 : |
|
|
|
52 : |
|
|
fun compare (Int i1, Int i2) = IntegerLit.compare(i1, i2)
|
53 : |
|
|
| compare (Float f1, Float f2) = FloatLit.compare(f1, f2)
|
54 : |
|
|
| compare (Char c1, Char c2) = Word.compare(c1, c2)
|
55 : |
|
|
| compare (String s1, String s2) = String.compare(s1, s2)
|
56 : |
|
|
| compare (Bool false, Bool true) = LESS
|
57 : |
|
|
| compare (Bool true, Bool false) = GREATER
|
58 : |
|
|
| compare (Bool _, Bool _) = EQUAL
|
59 : |
|
|
| compare (Int _, _) = LESS
|
60 : |
|
|
| compare (_, Int _) = GREATER
|
61 : |
|
|
| compare (Float _, _) = LESS
|
62 : |
|
|
| compare (_, Float _) = GREATER
|
63 : |
|
|
| compare (Char _, _) = LESS
|
64 : |
|
|
| compare (_, Char _) = GREATER
|
65 : |
|
|
| compare (String _, _) = LESS
|
66 : |
|
|
| compare (_, String _) = GREATER
|
67 : |
|
|
|
68 : |
|
|
(* for hash codes, use the low-order 4 bits for a type code *)
|
69 : |
|
|
local
|
70 : |
|
|
val intCd = 0w5
|
71 : |
|
|
val floatCd = 0w7
|
72 : |
|
|
val charCd = 0w11
|
73 : |
|
|
val stringCd = 0w13
|
74 : |
|
|
val boolCd = 0w17
|
75 : |
|
|
fun h (hash, base) = Word.<<(hash, 0w4) + base
|
76 : |
|
|
in
|
77 : |
|
|
fun hash (Int i) = h(IntegerLit.hash i, intCd)
|
78 : |
|
|
| hash (Float f) = h(FloatLit.hash f, floatCd)
|
79 : |
|
|
| hash (Char c) = h(c, charCd)
|
80 : |
|
|
| hash (String s) = h(HashString.hashString s, stringCd)
|
81 : |
|
|
| hash (Bool false) = h(0w1, boolCd)
|
82 : |
|
|
| hash (Bool true) = h(0w3, boolCd)
|
83 : |
|
|
end (* local *)
|
84 : |
|
|
|
85 : |
|
|
structure Tbl = HashTableFn (
|
86 : |
|
|
struct
|
87 : |
|
|
type hash_key = literal
|
88 : |
|
|
val hashVal = hash
|
89 : |
|
|
val sameKey = same
|
90 : |
|
|
end)
|
91 : |
|
|
|
92 : |
|
|
(* some standard constants *)
|
93 : |
|
|
val trueLit = Bool true
|
94 : |
|
|
val falseLit = Bool false
|
95 : |
|
|
|
96 : |
|
|
end
|