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 3349 - (view) (download)

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

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