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

SCM Repository

[diderot] View of /trunk/src/ast/type-util.sml
ViewVC logotype

View of /trunk/src/ast/type-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 81 - (download) (annotate)
Wed May 26 17:28:54 2010 UTC (9 years, 1 month ago) by jhr
File size: 2234 byte(s)
  Working on typechecker
(* type-util.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 *)

structure TypeUtil : sig

    val toString : Types.ty -> string

  end = struct

    structure Ty = Types
    structure MV = MetaVar

    fun listToString fmt sep items = String.concatWith sep (List.map fmt items)

    fun diffToString (Ty.DiffConst n) = Int.toString n
      | diffToString (Ty.DiffVar(dv, 0)) = MV.diffVarToString dv
      | diffToString (Ty.DiffVar(dv, i)) = if i < 0
	  then String.concat["(", MV.diffVarToString dv, "-", Int.toString(~i), ")"]
	  else String.concat["(", MV.diffVarToString dv, "+", Int.toString i, ")"]

    fun shapeToString (Ty.Shape shape) =
	  concat["[", listToString dimToString "," shape, "]"]
      | shapeToString (Ty.ShapeVar sv) = MV.shapeVarToString sv
      | shapeToString (Ty.ShapeExt(shape, d)) = let
	  fun toS (Ty.Shape shape) = (listToString dimToString "," shape) ^ ","
	    | toS (Ty.ShapeVar sv) = MV.shapeVarToString sv ^ ";"
	    | toS (Ty.ShapeExt(shape, d)) = concat[toS shape, dimToString d, ","]
	  in
	    toS shape ^ dimToString d
	  end

    and dimToString (Ty.DimConst n) = Int.toString n
      | dimToString (Ty.DimVar v) = MV.dimVarToString v

    fun toString ty = (case ty
	   of Ty.T_Var tv => MV.tyVarToString tv
	    | Ty.T_Bool => "bool"
	    | Ty.T_Int => "int"
	    | Ty.T_String => "string"
	    | Ty.T_Kernel n => "kernel#" ^ diffToString n
	    | Ty.T_Tensor(Ty.Shape[]) => "real"
	    | Ty.T_Tensor(Ty.Shape[Ty.DimConst 2]) => "vec2"
	    | Ty.T_Tensor(Ty.Shape[Ty.DimConst 3]) => "vec3"
	    | Ty.T_Tensor(Ty.Shape[Ty.DimConst 4]) => "vec4"
	    | Ty.T_Tensor shape => "tensor" ^ shapeToString shape
	    | Ty.T_Image{dim, shape} => concat[
		  "image(", dimToString dim, ")", shapeToString shape
		]
	    | Ty.T_Field{diff, dim, shape} => concat[
		  "field#", diffToString diff, "(", dimToString dim,
		  ")", shapeToString shape
		]
	    | Ty.T_Fun(tys1, ty2) => let
		fun tysToString [] = "()"
		  | tysToString [ty] = toString ty
		  | tysToString tys = String.concat[
			"(", listToString toString " * " tys, ")"
		      ]
		in
		  String.concat[tysToString tys1, " -> ", toString ty2]
		end
	  (* end case *))

  end

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