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

SCM Repository

[diderot] Annotation of /trunk/src/typechecker/typechecker.sml
ViewVC logotype

Annotation of /trunk/src/typechecker/typechecker.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 80 - (view) (download)

1 : jhr 69 (* typechecker.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure Typechecker : sig
8 :    
9 :     val check : ParseTree.program -> AST.program
10 :    
11 :     end = struct
12 :    
13 : jhr 70 structure PT = ParseTree
14 : jhr 69 structure Ty = Types
15 :    
16 : jhr 70 (* check a differentiation level, which muse be >= 0 *)
17 :     fun checkDiff (cxt, k) =
18 :     if (k < 0)
19 :     then raise Fail "differentiation must be >= 0"
20 : jhr 75 else Ty.DiffConst(IntInf.toInt k)
21 : jhr 70
22 :     (* check a dimension, which must be 2 or 3 *)
23 :     fun checkDim (cxt, d) =
24 :     if (d < 2) orelse (3 < d)
25 :     then raise Fail "invalid dimension; must be 2 or 3"
26 : jhr 75 else Ty.DimConst(IntInf.toInt d)
27 : jhr 70
28 :     (* check a shape *)
29 :     fun checkShape (cxt, shape) = let
30 :     fun chkDim d = if (d < 1)
31 :     then raise Fail "invalid shape dimension; must be >= 1"
32 : jhr 75 else Ty.DimConst(IntInf.toInt d)
33 : jhr 70 in
34 :     Ty.Shape(List.map chkDim shape)
35 :     end
36 :    
37 : jhr 69 (* check the well-formedness of a type and translate it to an AST type *)
38 : jhr 70 fun checkTy (cxt, ty) = (case ty
39 : jhr 71 of PT.T_Mark m => checkTy(#span m, #tree m)
40 : jhr 70 | PT.T_Bool => Ty.T_Bool
41 :     | PT.T_Int => Ty.T_Int
42 :     | PT.T_Real => Ty.realTy
43 :     | PT.T_String => Ty.T_String
44 :     | PT.T_Vec n => (* NOTE: the parser guarantees that 2 <= n <= 4 *)
45 :     Ty.vecTy(IntInf.toInt n)
46 :     | PT.T_Kernel k => Ty.T_Kernel(checkDiff(cxt, k))
47 :     | PT.T_Field{diff, dim, shape} => Ty.T_Field{
48 :     diff = checkDiff (cxt, diff),
49 :     dim = checkDim (cxt, dim),
50 :     shape = checkShape (cxt, shape)
51 :     }
52 :     | PT.T_Tensor shape => Ty.T_Tensor(checkShape(cxt, shape))
53 :     | PT.T_Image{dim, shape} => Ty.T_Image{
54 :     dim = checkDim (cxt, dim),
55 :     shape = checkShape (cxt, shape)
56 :     }
57 :     | PT.T_Array(ty, dims) => raise Fail "Array type"
58 : jhr 69 (* end case *))
59 :    
60 : jhr 71 fun checkLit lit = (case lit
61 :     of (Literal.Int _) => (AST.E_Lit lit, Ty.T_Int)
62 :     | (Literal.Float _) => (AST.E_Lit lit, Ty.realTy)
63 :     | (Literal.String s) => (AST.E_Lit lit, Ty.T_String)
64 :     | (Literal.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
65 :     (* end case *))
66 :    
67 : jhr 70 (*
68 :     (* typecheck an expression and translate it to AST *)
69 : jhr 71 fun checkExpr (env, cxt, e) = (case e
70 :     of PT.E_Mark m => checkExpr (env, #span m, #tree m)
71 :     | PT.E_Var x => (case Env.findVar (env, x)
72 :     of SOME x' => (case Var.typeOf x'
73 : jhr 80 of ([], ty) => (AST.E_Var x', ty)
74 :     | scheme => let
75 :     val (args, ty) = Util.instantiate scheme
76 :     in
77 :     (AST.E_VarInst(x', args, ty), ty)
78 :     end
79 : jhr 71 (* end case *))
80 :     | NONE => raise Fail "undefined variable"
81 :     (* end case *))
82 :     | PT.E_Lit lit => checkLit lit
83 : jhr 70 | PT.E_BinOp of expr * var * expr
84 :     | PT.E_UnaryOp of var * expr
85 :     | PT.E_Tuple of expr list
86 :     | PT.E_Apply of var * expr list
87 :     | PT.E_Cons of ty * expr list
88 :     (* end case *))
89 :    
90 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
91 :     of PT.VD_Mark m => checkVarDecl (env, #span m, kind, #tree m)
92 :     | PT.VD_Decl(ty, x, e) => let
93 :     val ty = checkType ty
94 :     val x' = Var.new (x, kind, ty)
95 :     val (e', ty') = checkExpr (env, cxt, e)
96 :     in
97 :     (* FIXME: check types *)
98 :     AST.VD_Decl(x', e')
99 :     end
100 :     (* end case *))
101 :    
102 : jhr 70 (* typecheck a statement and translate it to AST *)
103 : jhr 71 fun checkStmt (env, cxt, s) = (case s
104 :     of PT.S_Mark m => checkStmt (env, #span m, #tree m)
105 : jhr 72 | PT.S_Block stms => let
106 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
107 :     | chk (env, s::ss, stms) = let
108 :     val (s', env') = checkStmt (env, cxt, s)
109 :     in
110 :     chk (env', ss, s'::ss)
111 :     end
112 :     in
113 :     (chk (env, stms, []), env)
114 :     end
115 :     | PT.S_Decl vd => let
116 :     val vd as AST.VD_Decl(x, _) = checkVarDecl (env, cxt, Var.LocalVar, vd)
117 :     in
118 :     (AST.S_Decl vd, Env.insertLocal(env, x, x'))
119 :     end
120 :     | PT.S_IfThen(e, s) => let
121 :     val (e', ty) = checkExpr (env, cxt, e)
122 :     val s' = checkStmt (env, cxt, s)
123 :     in
124 :     (* check that condition has bool type *)
125 :     case ty
126 :     of Ty.T_Bool => ()
127 :     | _ => raise Fail "condition not boolean type"
128 :     (* end case *);
129 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
130 :     end
131 :     | PT.S_IfThenElse(e, s1, s2) => let
132 :     val (e', ty) = checkExpr (env, cxt, e)
133 :     val s1' = checkStmt (env, cxt, s1)
134 :     val s2' = checkStmt (env, cxt, s2)
135 :     in
136 :     (* check that condition has bool type *)
137 :     case ty
138 :     of Ty.T_Bool => ()
139 :     | _ => raise Fail "condition not boolean type"
140 :     (* end case *);
141 :     (AST.S_IfThenElse(e', s1', s2'), env)
142 :     end
143 :     | PT.S_Assign(x, e) => (case Env.findVar (env, x)
144 :     of NONE => raise Fail "undefined variable"
145 :     | SOME x' => let
146 :     val (e', ty) = checkExpr (env, cxt, e)
147 :     in
148 :     (* FIXME: check types *)
149 :     (* check that x' is mutable *)
150 :     case Var.kindOf x'
151 :     of Var.ActorStateVar => ()
152 :     | Var.LocalVar => ()
153 :     | _ => raise Fail "assignment to immutable variable"
154 :     (* end case *);
155 :     (AST.S_Assign(x', e'), env)
156 :     end
157 :     (* end case *))
158 :     | PT.S_New(actor, args) => let
159 :     val argTys' = List.map (fn e => checkExpr(env, cxt, e)) args
160 :     val (args', tys') = ListPair.unzip argTys'
161 :     in
162 :     (* FIXME: check that actor is defined and has the argument types match *)
163 :     AST.S_New(actor, args')
164 :     end
165 :     | PT.S_Die => (AST.S_Die, env)
166 :     | PT.S_Stabilize => (AST.S_Stabilize, env)
167 : jhr 70 (* end case *))
168 :    
169 : jhr 71 fun checkDecl (env, cxt, d) = (case d
170 :     of PT.D_Mark m => checkDecl (env, #span m, #tree m)
171 : jhr 72 | PT.D_Input(ty, x, optExp) => let
172 : jhr 71 val ty = checkTy(cxt, ty)
173 :     val x' = Var.new(x, Var.InputVar, ty)
174 :     val dcl = (case optExp
175 :     of NONE => AST.D_Input(x', NONE)
176 :     | SOME e => let
177 :     val (e', ty') = checkExpr (env, cxt, e)
178 :     in
179 :     (* FIXME: check types *)
180 :     AST.D_Input(x', SOME e')
181 :     end
182 :     (* end case *))
183 :     in
184 :     (dcl, Env.insertGlobal(env, x, x'))
185 :     end
186 : jhr 72 | PT.D_Var vd => let
187 :     val vd as AST.VD_Decl(x, _) = checkVarDecl (env, cxt, Var.GlobalVar, vd)
188 :     in
189 :     (AST.D_Var vd, Env.insertGlobal(env, x, x'))
190 :     end
191 :     | PT.D_Actor{nam, params, state, methods} => ??
192 : jhr 70 | PT.D_InitialArray of create * iter list
193 :     | PT.D_InitialCollection of create * iter list
194 :     (* end case *))
195 :     *)
196 :    
197 : jhr 71 fun check (PT.Program dcls) = AST.Program[]
198 : jhr 70
199 : jhr 69 end

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