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

SCM Repository

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

Annotation of /branches/vis12/src/compiler/common/literal.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1685 - (view) (download)

1 : jhr 26 (* literal.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 26 * 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 : jhr 1116 (* integer literals from ML ints *)
30 :     val intLit : int -> literal
31 :    
32 : jhr 26 end = struct
33 :    
34 :     datatype literal
35 :     = Int of IntegerLit.integer (* integert literals *)
36 :     | Float of FloatLit.float (* real literals *)
37 :     | String of string (* strings *)
38 :     | Bool of bool (* booleans *)
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 (String s1, String s2) = String.compare(s1, s2)
55 :     | compare (Bool false, Bool true) = LESS
56 :     | compare (Bool true, Bool false) = GREATER
57 :     | compare (Bool _, Bool _) = EQUAL
58 :     | compare (Int _, _) = LESS
59 :     | compare (_, Int _) = GREATER
60 :     | compare (Float _, _) = LESS
61 :     | compare (_, Float _) = GREATER
62 :     | compare (String _, _) = LESS
63 :     | compare (_, String _) = GREATER
64 :    
65 :     (* for hash codes, use the low-order 4 bits for a type code *)
66 :     local
67 :     val intCd = 0w5
68 :     val floatCd = 0w7
69 :     val stringCd = 0w13
70 :     val boolCd = 0w17
71 :     fun h (hash, base) = Word.<<(hash, 0w4) + base
72 :     in
73 :     fun hash (Int i) = h(IntegerLit.hash i, intCd)
74 :     | hash (Float f) = h(FloatLit.hash f, floatCd)
75 :     | hash (String s) = h(HashString.hashString s, stringCd)
76 :     | hash (Bool false) = h(0w1, boolCd)
77 :     | hash (Bool true) = h(0w3, boolCd)
78 :     end (* local *)
79 :    
80 :     structure Tbl = HashTableFn (
81 :     struct
82 :     type hash_key = literal
83 :     val hashVal = hash
84 :     val sameKey = same
85 :     end)
86 :    
87 :     (* some standard constants *)
88 :     val trueLit = Bool true
89 :     val falseLit = Bool false
90 :    
91 : jhr 1116 fun intLit i = Int(IntInf.fromInt i)
92 :    
93 : jhr 26 end

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