Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/common/literal.sml
ViewVC logotype

Annotation of /trunk/src/compiler/common/literal.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (view) (download)

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 toString (Int i) = IntegerLit.toString i
38 :     | toString (Float flt) = FloatLit.toString flt
39 :     | toString (String s) = concat["\"", String.toCString s, "\""]
40 :     | toString (Bool true) = "true"
41 :     | toString (Bool false) = "false"
42 :    
43 :     fun same (Int i1, Int i2) = IntegerLit.same(i1, i2)
44 :     | same (Float f1, Float f2) = FloatLit.same(f1, f2)
45 :     | same (String s1, String s2) = (s1 = s2)
46 :     | same (Bool b1, Bool b2) = (b1 = b2)
47 :     | same _ = false
48 :    
49 :     fun compare (Int i1, Int i2) = IntegerLit.compare(i1, i2)
50 :     | compare (Float f1, Float f2) = FloatLit.compare(f1, f2)
51 :     | compare (String s1, String s2) = String.compare(s1, s2)
52 :     | compare (Bool false, Bool true) = LESS
53 :     | compare (Bool true, Bool false) = GREATER
54 :     | compare (Bool _, Bool _) = EQUAL
55 :     | compare (Int _, _) = LESS
56 :     | compare (_, Int _) = GREATER
57 :     | compare (Float _, _) = LESS
58 :     | compare (_, Float _) = GREATER
59 :     | compare (String _, _) = LESS
60 :     | compare (_, String _) = GREATER
61 :    
62 :     (* for hash codes, use the low-order 4 bits for a type code *)
63 :     local
64 :     val intCd = 0w5
65 :     val floatCd = 0w7
66 :     val stringCd = 0w13
67 :     val boolCd = 0w17
68 :     fun h (hash, base) = Word.<<(hash, 0w4) + base
69 :     in
70 :     fun hash (Int i) = h(IntegerLit.hash i, intCd)
71 :     | hash (Float f) = h(FloatLit.hash f, floatCd)
72 :     | hash (String s) = h(HashString.hashString s, stringCd)
73 :     | hash (Bool false) = h(0w1, boolCd)
74 :     | hash (Bool true) = h(0w3, boolCd)
75 :     end (* local *)
76 :    
77 :     structure Tbl = HashTableFn (
78 :     struct
79 :     type hash_key = literal
80 :     val hashVal = hash
81 :     val sameKey = same
82 :     end)
83 :    
84 :     (* some standard constants *)
85 :     val trueLit = Bool true
86 :     val falseLit = Bool false
87 :    
88 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0