SCM Repository
[diderot] / trunk / src / common / literal.sml |
View of /trunk/src/common/literal.sml
Parent Directory
|
Revision Log
Revision 26 -
(download)
(annotate)
Tue Feb 9 00:43:01 2010 UTC (12 years, 4 months ago) by jhr
File size: 2960 byte(s)
Tue Feb 9 00:43:01 2010 UTC (12 years, 4 months ago) by jhr
File size: 2960 byte(s)
Working on Diderot parser
(* literal.sml * * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) * All rights reserved. * * Literal values. *) structure Literal : sig datatype literal = Int of IntegerLit.integer (* integert literals *) | Float of FloatLit.float (* real literals *) | String of string (* strings *) | Bool of bool (* booleans *) val toString : literal -> string val same : (literal * literal) -> bool val compare : (literal * literal) -> order val hash : literal -> word structure Tbl : MONO_HASH_TABLE where type Key.hash_key = literal (* some standard constants *) val trueLit : literal val falseLit : literal end = struct datatype literal = Int of IntegerLit.integer (* integert literals *) | Float of FloatLit.float (* real literals *) | String of string (* strings *) | Bool of bool (* booleans *) fun utf8ToStr s = concat(rev(UTF8.fold (fn (w, l) => wcharToStr w :: l) [] s)) fun toString (Int i) = IntegerLit.toString i | toString (Float flt) = FloatLit.toString flt | toString (String s) = concat["\"", String.toCString s, "\""] | toString (Bool true) = "true" | toString (Bool false) = "false" fun same (Int i1, Int i2) = IntegerLit.same(i1, i2) | same (Float f1, Float f2) = FloatLit.same(f1, f2) | same (String s1, String s2) = (s1 = s2) | same (Bool b1, Bool b2) = (b1 = b2) | same _ = false fun compare (Int i1, Int i2) = IntegerLit.compare(i1, i2) | compare (Float f1, Float f2) = FloatLit.compare(f1, f2) | compare (Char c1, Char c2) = Word.compare(c1, c2) | compare (String s1, String s2) = String.compare(s1, s2) | compare (Bool false, Bool true) = LESS | compare (Bool true, Bool false) = GREATER | compare (Bool _, Bool _) = EQUAL | compare (Int _, _) = LESS | compare (_, Int _) = GREATER | compare (Float _, _) = LESS | compare (_, Float _) = GREATER | compare (Char _, _) = LESS | compare (_, Char _) = GREATER | compare (String _, _) = LESS | compare (_, String _) = GREATER (* for hash codes, use the low-order 4 bits for a type code *) local val intCd = 0w5 val floatCd = 0w7 val charCd = 0w11 val stringCd = 0w13 val boolCd = 0w17 fun h (hash, base) = Word.<<(hash, 0w4) + base in fun hash (Int i) = h(IntegerLit.hash i, intCd) | hash (Float f) = h(FloatLit.hash f, floatCd) | hash (Char c) = h(c, charCd) | hash (String s) = h(HashString.hashString s, stringCd) | hash (Bool false) = h(0w1, boolCd) | hash (Bool true) = h(0w3, boolCd) end (* local *) structure Tbl = HashTableFn ( struct type hash_key = literal val hashVal = hash val sameKey = same end) (* some standard constants *) val trueLit = Bool true val falseLit = Bool false end
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |