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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (view) (download)

1 : jhr 63 (* type-util.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure TypeUtil : sig
8 :    
9 :     val toString : Types.ty -> string
10 :    
11 :     end = struct
12 :    
13 :     structure Ty = Types
14 :    
15 :     fun listToString fmt sep items = String.concatWith sep (List.map fmt items)
16 :    
17 :     fun varToString (Ty.TV{kind, stamp}) = let
18 :     val prefix = (case kind
19 :     of Ty.TK_NAT => "'nat"
20 :     | Ty.TK_SHAPE => "'shp"
21 :     | Ty.TK_TYPE => "'ty"
22 :     (* end case *))
23 :     in
24 :     prefix ^ Stamp.toString stamp
25 :     end
26 :    
27 :     fun natToString (Ty.NatConst n) = Int.toString n
28 :     | natToString (Ty.NatExp(n, i)) = if i < 0
29 :     then String.concat[natToString n, "-", Int.toString(~i)]
30 :     else String.concat[natToString n, "+", Int.toString i]
31 :     | natToString (Ty.NatVar v) = varToString v
32 :    
33 :     fun shapeToString (Shape shape) =
34 :     concat["[", listToString natToString "," shape, "]"]
35 :     | shapeToString (ShapeVar sv) = varToString sv
36 :     | shapeToString (ShapeExt(shape, d)) = let
37 :     fun toS (Shape shape) = (listToString natToString "," shape) ^ ","
38 :     | toS (ShapeVar sv) = varToString sv ^ ";"
39 :     | toS (ShapeExt(shape, d)) = concat[toS shape, natToString d, ","]
40 :     in
41 :     toS shape ^ natToString d
42 :     end
43 :    
44 :     fun toString ty = (case ty
45 :     of Ty.T_Var tv => varToString tv
46 :     | Ty.T_Bool => "bool"
47 :     | Ty.T_Int => "int"
48 :     | Ty.T_String => "string"
49 :     | Ty.T_Kernel n => "kernel#" ^ natToString n
50 :     | Ty.T_Tensor[] => "real"
51 :     | Ty.T_Tensor[Ty.NatConst 2] => "vec2"
52 :     | Ty.T_Tensor[Ty.NatConst 3] => "vec3"
53 :     | Ty.T_Tensor[Ty.NatConst 4] => "vec4"
54 :     | Ty.T_Tensor shape => "tensor" ^ shapeToString shape
55 :     | Ty.T_Image{dim, shape} => concat[
56 :     "image(", natToString dim, ")", shapeToString shape
57 :     ]
58 :     | Ty.T_Field{diff, dim, shape} => concat[
59 :     "field#", natToString diff, "(", natToString dim,
60 :     ")", shapeToString shape
61 :     ]
62 :     | Ty.T_Fun(tys1, tys2) => let
63 :     fun tysToString [] = "()"
64 :     | tysToString [ty] = toString ty
65 :     | tysToString tys = String.concat[
66 :     "(", listToString toString " * " tys, ")"
67 :     ]
68 :     in
69 :     String.concat[tysToString tys1, " -> ", tysToSTring tys2]
70 :     end
71 :     (* end case *))
72 :    
73 :     end

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