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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (download) (annotate)
Wed Jun 23 19:28:48 2010 UTC (9 years, 2 months ago) by jhr
File size: 2681 byte(s)
  Moving compiler sources into src/compiler
(* 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 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

  end

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