(* literal.sml * * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) * * COPYRIGHT (c) 2015 The University of Chicago * 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 (* integer literals from ML ints *) val intLit : int -> 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 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 (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 (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 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 (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 fun intLit i = Int(IntInf.fromInt i) end
Click to toggle
does not end with </html> tag
does not end with </body> tag
The output has ended thus: l trueLit = Bool true val falseLit = Bool false fun intLit i = Int(IntInf.fromInt i) end