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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (view) (download)

1 : jhr 69 (* typechecker.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 69 * All rights reserved.
5 : jhr 228 *
6 : jhr 69 *)
7 :    
8 :     structure Typechecker : sig
9 :    
10 : jhr 88 exception Error
11 :    
12 : jhr 86 val check : Error.err_stream -> ParseTree.program -> AST.program
13 : jhr 69
14 :     end = struct
15 :    
16 : jhr 70 structure PT = ParseTree
17 : jhr 69 structure Ty = Types
18 : jhr 96 structure TU = TypeUtil
19 : jhr 81 structure U = Util
20 : jhr 69
21 : jhr 228 datatype scope = GlobalScope | ActorScope | MethodScope | InitScope
22 : jhr 169
23 : jhr 228 type env = {scope : scope, env : Env.env}
24 :    
25 :     fun actorScope {scope, env} = {scope=ActorScope, env=env}
26 :     fun methodScope {scope, env} = {scope=MethodScope, env=env}
27 :     fun initScope {scope, env} = {scope=InitScope, env=env}
28 :    
29 :     fun inActor {scope=ActorScope, env} = true
30 :     | inActor {scope=MethodScope, ...} = true
31 :     | inActor _ = false
32 :    
33 :     fun insertLocal ({scope, env}, x, x') = {scope=scope, env=Env.insertLocal(env, x, x')}
34 :     fun insertGlobal ({scope, env}, x, x') = {scope=scope, env=Env.insertGlobal(env, x, x')}
35 :    
36 : jhr 88 exception Error
37 :    
38 : jhr 86 type context = Error.err_stream * Error.span
39 :    
40 :     fun withContext ((errStrm, _), {span, tree}) =
41 :     ((errStrm, span), tree)
42 :     fun withEnvAndContext (env, (errStrm, _), {span, tree}) =
43 :     (env, (errStrm, span), tree)
44 :    
45 : jhr 88 fun error ((errStrm, span), msg) = (
46 :     Error.errorAt(errStrm, span, msg);
47 :     raise Error)
48 : jhr 86
49 : jhr 88 datatype token
50 :     = S of string | A of Atom.atom
51 :     | V of AST.var | TY of Types.ty | TYS of Types.ty list
52 :    
53 :     fun err (cxt, toks) = let
54 :     fun tok2str (S s) = s
55 :     | tok2str (A a) = Atom.toString a
56 :     | tok2str (V x) = Var.nameOf x
57 : jhr 96 | tok2str (TY ty) = TU.toString ty
58 : jhr 88 | tok2str (TYS []) = "()"
59 : jhr 96 | tok2str (TYS[ty]) = TU.toString ty
60 : jhr 88 | tok2str (TYS tys) = String.concat[
61 : jhr 96 "(", String.concatWith " * " (List.map TU.toString tys), ")"
62 : jhr 88 ]
63 :     in
64 :     error(cxt, List.map tok2str toks)
65 :     end
66 :    
67 : jhr 83 val realZero = AST.E_Lit(Literal.Float(FloatLit.zero true))
68 :    
69 : jhr 70 (* check a differentiation level, which muse be >= 0 *)
70 :     fun checkDiff (cxt, k) =
71 :     if (k < 0)
72 :     then raise Fail "differentiation must be >= 0"
73 : jhr 75 else Ty.DiffConst(IntInf.toInt k)
74 : jhr 70
75 :     (* check a dimension, which must be 2 or 3 *)
76 :     fun checkDim (cxt, d) =
77 : jhr 82 if (d <= 0)
78 :     then raise Fail "invalid dimension; must be > 0"
79 : jhr 75 else Ty.DimConst(IntInf.toInt d)
80 : jhr 70
81 :     (* check a shape *)
82 :     fun checkShape (cxt, shape) = let
83 :     fun chkDim d = if (d < 1)
84 :     then raise Fail "invalid shape dimension; must be >= 1"
85 : jhr 75 else Ty.DimConst(IntInf.toInt d)
86 : jhr 70 in
87 :     Ty.Shape(List.map chkDim shape)
88 :     end
89 :    
90 : jhr 69 (* check the well-formedness of a type and translate it to an AST type *)
91 : jhr 70 fun checkTy (cxt, ty) = (case ty
92 : jhr 86 of PT.T_Mark m => checkTy(withContext(cxt, m))
93 : jhr 70 | PT.T_Bool => Ty.T_Bool
94 :     | PT.T_Int => Ty.T_Int
95 :     | PT.T_Real => Ty.realTy
96 :     | PT.T_String => Ty.T_String
97 :     | PT.T_Vec n => (* NOTE: the parser guarantees that 2 <= n <= 4 *)
98 :     Ty.vecTy(IntInf.toInt n)
99 :     | PT.T_Kernel k => Ty.T_Kernel(checkDiff(cxt, k))
100 :     | PT.T_Field{diff, dim, shape} => Ty.T_Field{
101 :     diff = checkDiff (cxt, diff),
102 :     dim = checkDim (cxt, dim),
103 :     shape = checkShape (cxt, shape)
104 :     }
105 :     | PT.T_Tensor shape => Ty.T_Tensor(checkShape(cxt, shape))
106 :     | PT.T_Image{dim, shape} => Ty.T_Image{
107 :     dim = checkDim (cxt, dim),
108 :     shape = checkShape (cxt, shape)
109 :     }
110 : jhr 99 | PT.T_Array(ty, dims) => raise Fail "Array type not supported"
111 : jhr 69 (* end case *))
112 :    
113 : jhr 71 fun checkLit lit = (case lit
114 :     of (Literal.Int _) => (AST.E_Lit lit, Ty.T_Int)
115 :     | (Literal.Float _) => (AST.E_Lit lit, Ty.realTy)
116 :     | (Literal.String s) => (AST.E_Lit lit, Ty.T_String)
117 :     | (Literal.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
118 :     (* end case *))
119 :    
120 : jhr 85 (* resolve overloading: we use a simple scheme that selects the first operator in the
121 :     * list that matches the argument types.
122 :     *)
123 : jhr 91 fun resolveOverload (cxt, rator, argTys, args, candidates) = let
124 :     fun tryCandidates [] = err(cxt, [
125 :     S "unable to resolve overloaded operator \"", A rator, S "\"\n",
126 :     S " argument type is: ", TYS argTys, S "\n"
127 : jhr 85 ])
128 :     | tryCandidates (x::xs) = let
129 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf x)
130 :     in
131 :     if U.tryMatchTypes(domTy, argTys)
132 :     then (AST.E_Apply(x, tyArgs, args, rngTy), rngTy)
133 :     else tryCandidates xs
134 :     end
135 :     in
136 :     tryCandidates candidates
137 :     end
138 :    
139 : jhr 70 (* typecheck an expression and translate it to AST *)
140 : jhr 169 fun checkExpr (env : env, cxt, e) = (case e
141 : jhr 86 of PT.E_Mark m => checkExpr (withEnvAndContext (env, cxt, m))
142 : jhr 169 | PT.E_Var x => (case Env.findVar (#env env, x)
143 : jhr 171 of SOME x' => (AST.E_Var x', Var.monoTypeOf x')
144 : jhr 88 | NONE => err(cxt, [S "undeclared variable ", A x])
145 : jhr 71 (* end case *))
146 :     | PT.E_Lit lit => checkLit lit
147 : jhr 81 | PT.E_OrElse(e1, e2) => let
148 :     val (e1', ty1) = checkExpr(env, cxt, e1)
149 :     val (e2', ty2) = checkExpr(env, cxt, e2)
150 :     in
151 :     case (ty1, ty2)
152 :     of (Ty.T_Bool, Ty.T_Bool) =>
153 : jhr 416 (AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2', Ty.T_Bool), Ty.T_Bool)
154 : jhr 99 | _ => err (cxt, [S "arguments to \"||\" must have bool type"])
155 : jhr 81 (* end case *)
156 :     end
157 :     | PT.E_AndAlso(e1, e2) => let
158 :     val (e1', ty1) = checkExpr(env, cxt, e1)
159 :     val (e2', ty2) = checkExpr(env, cxt, e2)
160 :     in
161 :     case (ty1, ty2)
162 :     of (Ty.T_Bool, Ty.T_Bool) =>
163 : jhr 416 (AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false), Ty.T_Bool), Ty.T_Bool)
164 : jhr 99 | _ => err (cxt, [S "arguments to \"&&\" must have bool type"])
165 : jhr 81 (* end case *)
166 :     end
167 : jhr 370 | PT.E_Cond(e1, cond, e2) => let
168 :     val (e1', ty1) = checkExpr(env, cxt, e1)
169 :     val (e2', ty2) = checkExpr(env, cxt, e2)
170 :     in
171 :     case checkExpr(env, cxt, cond)
172 :     of (cond', Ty.T_Bool) =>
173 :     if U.matchType(ty1, ty2)
174 : jhr 416 then (AST.E_Cond(cond', e1', e2', ty1), ty1)
175 : jhr 370 else err (cxt, [
176 :     S "type do not match in conditional expression\n",
177 :     S " true branch: ", TY ty1,
178 :     S " false branch: ", TY ty2
179 :     ])
180 :     | (_, ty') => err (cxt, [S "expected bool type, but found ", TY ty'])
181 :     (* end case *)
182 :     end
183 : jhr 81 | PT.E_BinOp(e1, rator, e2) => let
184 :     val (e1', ty1) = checkExpr(env, cxt, e1)
185 :     val (e2', ty2) = checkExpr(env, cxt, e2)
186 :     in
187 :     case Basis.findOp rator
188 :     of [rator] => let
189 :     val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf rator)
190 :     in
191 :     if U.matchTypes(domTy, [ty1, ty2])
192 :     then (AST.E_Apply(rator, tyArgs, [e1', e2'], rngTy), rngTy)
193 : jhr 89 else err (cxt, [
194 :     S "type error for binary operator \"", V rator, S "\"\n",
195 :     S " expected: ", TYS domTy, S "\n",
196 :     S " but found: ", TYS[ty1, ty2], S "\n"
197 : jhr 85 ])
198 : jhr 81 end
199 : jhr 91 | ovldList => resolveOverload (cxt, rator, [ty1, ty2], [e1', e2'], ovldList)
200 : jhr 81 (* end case *)
201 :     end
202 :     | PT.E_UnaryOp(rator, e) => let
203 :     val (e', ty) = checkExpr(env, cxt, e)
204 :     in
205 :     case Basis.findOp rator
206 :     of [rator] => let
207 : jhr 381 val (tyArgs, Ty.T_Fun([domTy], rngTy)) = U.instantiate(Var.typeOf rator)
208 : jhr 81 in
209 :     if U.matchType(domTy, ty)
210 :     then (AST.E_Apply(rator, tyArgs, [e'], rngTy), rngTy)
211 : jhr 89 else err (cxt, [
212 :     S "type error for unary operator \"", V rator, S "\"\n",
213 :     S " expected: ", TY domTy, S "\n",
214 :     S " but found: ", TY ty, S "\n"
215 : jhr 85 ])
216 : jhr 81 end
217 : jhr 91 | ovldList => resolveOverload (cxt, rator, [ty], [e'], ovldList)
218 : jhr 81 (* end case *)
219 :     end
220 : jhr 381 | PT.E_Slice(e, indices) => let
221 :     val (e', ty) = checkExpr (env, cxt, e)
222 :     fun checkIndex NONE = NONE
223 :     | checkIndex (SOME e) = let
224 :     val (e', ty) = checkExpr (env, cxt, e)
225 :     in
226 :     if U.matchType(ty, Ty.T_Int)
227 :     then (SOME e')
228 :     else err (cxt, [
229 :     S "type error in index expression\n",
230 :     S " expected int, but found: ", TY ty, S "\n"
231 :     ])
232 :     end
233 :     val indices' = List.map checkIndex indices
234 :     val order = List.length indices'
235 : jhr 399 val expectedTy = TU.mkTensorTy order
236 :     val resultTy = TU.slice(expectedTy, List.map Option.isSome indices')
237 : jhr 381 in
238 : jhr 399 if U.matchType(ty, expectedTy)
239 : jhr 381 then ()
240 :     else err (cxt, [
241 :     S "type error in slice operation\n",
242 :     S " expected: ", S(Int.toString order), S "-order tensor\n",
243 :     S " but found: ", TY ty, S "\n"
244 :     ]);
245 : jhr 399 (AST.E_Slice(e', indices', resultTy), resultTy)
246 : jhr 381 end
247 : jhr 81 | PT.E_Tuple args => let
248 :     val (args, tys) = checkExprList (env, cxt, args)
249 :     in
250 :     raise Fail "E_Tuple not yet implemented"
251 :     end
252 :     | PT.E_Apply(f, args) => let
253 :     val (args, tys) = checkExprList (env, cxt, args)
254 :     in
255 : jhr 169 case Env.findFunc (#env env, f)
256 :     of SOME f =>
257 : jhr 228 if (inActor env) andalso (Basis.isRestricted f)
258 :     then err(cxt, [S "use of restricted operation ", V f, S " in actor body"])
259 :     else (case Util.instantiate(Var.typeOf f)
260 : jhr 169 of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
261 :     if U.matchTypes(domTy, tys)
262 :     then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
263 :     else err(cxt, [
264 :     S "type error in application of ", V f, S "\n",
265 :     S " expected: ", TYS domTy, S "\n",
266 :     S " but found: ", TYS tys, S "\n"
267 :     ])
268 :     | _ => err(cxt, [S "application of non-function ", V f])
269 :     (* end case *))
270 : jhr 99 | NONE => err(cxt, [S "unknown function ", A f])
271 : jhr 81 (* end case *)
272 :     end
273 : jhr 86 | PT.E_Cons args => let
274 :     val (args, ty::tys) = checkExprList (env, cxt, args)
275 : jhr 81 in
276 : jhr 96 case TU.pruneHead ty
277 : jhr 86 of Ty.T_Tensor shape => let
278 :     fun chkTy ty' = U.matchType(ty, ty')
279 :     val resTy = Ty.T_Tensor(Ty.shapeExt(shape, Ty.DimConst(List.length args)))
280 : jhr 83 in
281 : jhr 86 if List.all chkTy tys
282 :     then (AST.E_Cons args, resTy)
283 : jhr 99 else err(cxt, [S "arguments of tensor construction must have same type"])
284 : jhr 83 end
285 : jhr 99 | _ => err(cxt, [S "Invalid argument type for tensor construction"])
286 : jhr 83 (* end case *)
287 : jhr 81 end
288 : jhr 86 | PT.E_Real e => (case checkExpr (env, cxt, e)
289 :     of (e', Ty.T_Int) =>
290 :     (AST.E_Apply(BasisVars.i2r, [], [e'], Ty.realTy), Ty.realTy)
291 : jhr 99 | _ => err(cxt, [S "argument of real conversion must be int"])
292 : jhr 86 (* end case *))
293 : jhr 70 (* end case *))
294 :    
295 : jhr 81 (* typecheck a list of expressions returning a list of AST expressions and a list
296 :     * of types of the expressions.
297 :     *)
298 :     and checkExprList (env, cxt, exprs) = let
299 :     fun chk (e, (es, tys)) = let
300 :     val (e, ty) = checkExpr (env, cxt, e)
301 :     in
302 :     (e::es, ty::tys)
303 :     end
304 :     in
305 :     List.foldr chk ([], []) exprs
306 :     end
307 :    
308 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
309 : jhr 86 of PT.VD_Mark m => checkVarDecl (env, (#1 cxt, #span m), kind, #tree m)
310 : jhr 72 | PT.VD_Decl(ty, x, e) => let
311 : jhr 81 val ty = checkTy (cxt, ty)
312 : jhr 72 val x' = Var.new (x, kind, ty)
313 :     val (e', ty') = checkExpr (env, cxt, e)
314 :     in
315 : jhr 99 (* FIXME: this check is not flexible enough; should allow lhs type to support
316 :     * fewer levels of differentiation than rhs provides.
317 :     *)
318 :     if U.matchType(ty, ty')
319 :     then (x, x', e')
320 :     else err(cxt, [
321 :     S "type of variable ", A x,
322 :     S " does not match type of initializer\n",
323 :     S " expected: ", TY ty, S "\n",
324 :     S " but found: ", TY ty', S "\n"
325 :     ])
326 : jhr 72 end
327 :     (* end case *))
328 :    
329 : jhr 70 (* typecheck a statement and translate it to AST *)
330 : jhr 71 fun checkStmt (env, cxt, s) = (case s
331 : jhr 86 of PT.S_Mark m => checkStmt (withEnvAndContext (env, cxt, m))
332 : jhr 72 | PT.S_Block stms => let
333 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
334 :     | chk (env, s::ss, stms) = let
335 :     val (s', env') = checkStmt (env, cxt, s)
336 :     in
337 : jhr 81 chk (env', ss, s'::stms)
338 : jhr 72 end
339 :     in
340 :     (chk (env, stms, []), env)
341 :     end
342 :     | PT.S_Decl vd => let
343 : jhr 228 val (x, x', e) = checkVarDecl (methodScope env, cxt, Var.LocalVar, vd)
344 : jhr 72 in
345 : jhr 228 (AST.S_Decl(AST.VD_Decl(x', e)), insertLocal(env, x, x'))
346 : jhr 72 end
347 :     | PT.S_IfThen(e, s) => let
348 : jhr 228 val (e', ty) = checkExpr (env, cxt, e)
349 : jhr 81 val (s', _) = checkStmt (env, cxt, s)
350 : jhr 72 in
351 :     (* check that condition has bool type *)
352 :     case ty
353 :     of Ty.T_Bool => ()
354 : jhr 99 | _ => err(cxt, [S "condition not boolean type"])
355 : jhr 72 (* end case *);
356 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
357 :     end
358 :     | PT.S_IfThenElse(e, s1, s2) => let
359 : jhr 228 val (e', ty) = checkExpr (env, cxt, e)
360 : jhr 81 val (s1', _) = checkStmt (env, cxt, s1)
361 :     val (s2', _) = checkStmt (env, cxt, s2)
362 : jhr 72 in
363 :     (* check that condition has bool type *)
364 :     case ty
365 :     of Ty.T_Bool => ()
366 : jhr 99 | _ => err(cxt, [S "condition not boolean type"])
367 : jhr 72 (* end case *);
368 :     (AST.S_IfThenElse(e', s1', s2'), env)
369 :     end
370 : jhr 228 | PT.S_Assign(x, e) => (case Env.findVar (#env env, x)
371 : jhr 99 of NONE => err(cxt, [
372 :     S "undefined variable ", A x
373 :     ])
374 : jhr 72 | SOME x' => let
375 : jhr 99 (* FIXME: check for polymorphic variables *)
376 :     val ([], ty) = Var.typeOf x'
377 : jhr 228 val (e', ty') = checkExpr (env, cxt, e)
378 : jhr 72 in
379 : jhr 99 if U.matchType(ty, ty')
380 :     then (x, x', e')
381 :     else err(cxt, [
382 :     S "type of assigned variable ", A x,
383 :     S " does not match type of rhs\n",
384 :     S " expected: ", TY ty, S "\n",
385 :     S " but found: ", TY ty', S "\n"
386 :     ]);
387 : jhr 72 (* check that x' is mutable *)
388 :     case Var.kindOf x'
389 :     of Var.ActorStateVar => ()
390 : jhr 173 | Var.ActorOutputVar => ()
391 : jhr 72 | Var.LocalVar => ()
392 : jhr 99 | _ => err(cxt, [
393 :     S "assignment to immutable variable ", A x
394 :     ])
395 : jhr 72 (* end case *);
396 :     (AST.S_Assign(x', e'), env)
397 :     end
398 :     (* end case *))
399 :     | PT.S_New(actor, args) => let
400 : jhr 228 val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
401 : jhr 81 val (args', tys') = ListPair.unzip argsAndTys'
402 : jhr 72 in
403 : jhr 228 case #scope env
404 :     of MethodScope => ()
405 :     | InitScope => ()
406 :     | _ => err(cxt, [S "invalid scope for new actor"])
407 :     (* end case *);
408 : jhr 72 (* FIXME: check that actor is defined and has the argument types match *)
409 : jhr 81 (AST.S_New(actor, args'), env)
410 : jhr 72 end
411 : jhr 228 | PT.S_Die => (
412 :     case #scope env
413 : jhr 235 of MethodScope => ()
414 :     | _ => err(cxt, [S "\"die\" statment outside of method"])
415 : jhr 228 (* end case *);
416 :     (AST.S_Die, env))
417 :     | PT.S_Stabilize => (
418 :     case #scope env
419 : jhr 235 of MethodScope => ()
420 :     | _ => err(cxt, [S "\"stabilize\" statment outside of method"])
421 : jhr 228 (* end case *);
422 :     (AST.S_Stabilize, env))
423 : jhr 70 (* end case *))
424 :    
425 : jhr 82 fun checkParams (env, cxt, params) = let
426 :     fun chkParam (env, cxt, param) = (case param
427 : jhr 86 of PT.P_Mark m => chkParam (withEnvAndContext (env, cxt, m))
428 : jhr 82 | PT.P_Param(ty, x) => let
429 :     val x' = Var.new(x, AST.ActorParam, checkTy (cxt, ty))
430 :     in
431 : jhr 228 (x', insertLocal(env, x, x'))
432 : jhr 82 end
433 :     (* end case *))
434 :     fun chk (param, (xs, env)) = let
435 :     val (x, env) = chkParam (env, cxt, param)
436 :     in
437 :     (x::xs, env)
438 :     end
439 :     in
440 :     (* FIXME: need to check for multiple occurences of the same parameter name! *)
441 :     List.foldr chk ([], env) params
442 :     end
443 :    
444 :     fun checkMethod (env, cxt, meth) = (case meth
445 : jhr 86 of PT.M_Mark m => checkMethod (withEnvAndContext (env, cxt, m))
446 : jhr 82 | PT.M_Method(name, body) => let
447 : jhr 228 val (body, _) = checkStmt(methodScope env, cxt, body)
448 : jhr 82 in
449 :     AST.M_Method(name, body)
450 :     end
451 :     (* end case *))
452 :    
453 :     fun checkActor (env, cxt, {name, params, state, methods}) = let
454 :     (* check the actor parameters *)
455 :     val (params, env) = checkParams (env, cxt, params)
456 :     (* check the actor state variable definitions *)
457 :     val (vds, env) = let
458 : jhr 164 fun checkStateVar ((isOut, vd), (vds, env)) = let
459 : jhr 173 val kind = if isOut then AST.ActorOutputVar else AST.ActorStateVar
460 : jhr 228 val (x, x', e') = checkVarDecl (env, cxt, kind, vd)
461 : jhr 82 in
462 : jhr 228 (* check that output variables have value types *)
463 :     if isOut andalso not(TU.isValueType(Var.monoTypeOf x'))
464 :     then err(cxt, [
465 :     S "output variable ", V x', S " has non-value type ",
466 :     TY(Var.monoTypeOf x')
467 :     ])
468 :     else ();
469 :     (AST.VD_Decl(x', e')::vds, insertLocal(env, x, x'))
470 : jhr 82 end
471 :     val (vds, env) = List.foldl checkStateVar ([], env) state
472 :     in
473 :     (List.rev vds, env)
474 :     end
475 :     (* check the actor methods *)
476 :     val methods = List.map (fn m => checkMethod (env, cxt, m)) methods
477 :     in
478 :     AST.D_Actor{name = name, params = params, state = vds, methods = methods}
479 :     end
480 :    
481 : jhr 89 fun checkCreate (env, cxt, PT.C_Mark m) = checkCreate (withEnvAndContext (env, cxt, m))
482 :     | checkCreate (env, cxt, PT.C_Create(actor, args)) = let
483 : jhr 228 val (args, tys) = checkExprList (env, cxt, args)
484 : jhr 89 in
485 :     (* FIXME: check against actor definition *)
486 :     AST.C_Create(actor, args)
487 :     end
488 :    
489 :     fun checkIter (env, cxt, PT.I_Mark m) = checkIter (withEnvAndContext (env, cxt, m))
490 :     | checkIter (env, cxt, PT.I_Range(x, e1, e2)) = let
491 : jhr 228 val (e1', ty1) = checkExpr (env, cxt, e1)
492 :     val (e2', ty2) = checkExpr (env, cxt, e2)
493 : jhr 89 val x' = Var.new(x, Var.LocalVar, Ty.T_Int)
494 : jhr 228 val env' = insertLocal(env, x, x')
495 : jhr 89 in
496 :     case (ty1, ty2)
497 :     of (Ty.T_Int, Ty.T_Int) => (AST.I_Range(x', e1', e2'), env')
498 :     | _ => err(cxt, [
499 :     S "range expressions must have integer type\n",
500 :     S " but found: ", TY ty1, S " .. ", TY ty2, S "\n"
501 :     ])
502 :     (* end case *)
503 :     end
504 :    
505 :     fun checkIters (env, cxt, iters) = let
506 :     fun chk (env, [], iters) = (List.rev iters, env)
507 :     | chk (env, iter::rest, iters) = let
508 :     val (iter, env) = checkIter (env, cxt, iter)
509 :     in
510 :     chk (env, rest, iter::iters)
511 :     end
512 :     in
513 :     chk (env, iters, [])
514 :     end
515 :    
516 : jhr 71 fun checkDecl (env, cxt, d) = (case d
517 : jhr 86 of PT.D_Mark m => checkDecl (withEnvAndContext (env, cxt, m))
518 : jhr 72 | PT.D_Input(ty, x, optExp) => let
519 : jhr 71 val ty = checkTy(cxt, ty)
520 :     val x' = Var.new(x, Var.InputVar, ty)
521 :     val dcl = (case optExp
522 :     of NONE => AST.D_Input(x', NONE)
523 :     | SOME e => let
524 : jhr 228 val (e', ty') = checkExpr (env, cxt, e)
525 : jhr 71 in
526 : jhr 89 if U.matchType (ty, ty')
527 :     then AST.D_Input(x', SOME e')
528 :     else err(cxt, [
529 :     S "definition of ", V x', S " has wrong type\n",
530 :     S " expected: ", TY ty, S "\n",
531 :     S " but found: ", TY ty', S "\n"
532 :     ])
533 : jhr 71 end
534 :     (* end case *))
535 :     in
536 : jhr 228 (* check that input variables have value types *)
537 :     if not(TU.isValueType ty)
538 :     then err(cxt, [S "input variable ", V x', S " has non-value type ", TY ty])
539 :     else ();
540 :     (dcl, insertGlobal(env, x, x'))
541 : jhr 71 end
542 : jhr 72 | PT.D_Var vd => let
543 : jhr 228 val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
544 : jhr 72 in
545 : jhr 228 (AST.D_Var(AST.VD_Decl(x', e')), insertGlobal(env, x, x'))
546 : jhr 72 end
547 : jhr 228 | PT.D_Actor arg => (checkActor(actorScope env, cxt, arg), env)
548 : jhr 89 | PT.D_InitialArray(create, iterators) => let
549 : jhr 228 val env = initScope env
550 : jhr 89 val (iterators, env') = checkIters (env, cxt, iterators)
551 :     val create = checkCreate (env', cxt, create)
552 :     in
553 :     (AST.D_InitialArray(create, iterators), env)
554 :     end
555 :     | PT.D_InitialCollection(create, iterators) => let
556 : jhr 228 val env = initScope env
557 : jhr 89 val (iterators, env') = checkIters (env, cxt, iterators)
558 :     val create = checkCreate (env', cxt, create)
559 :     in
560 :     (AST.D_InitialCollection(create, iterators), env)
561 :     end
562 : jhr 70 (* end case *))
563 :    
564 : jhr 86 fun check errStrm (PT.Program{span, tree}) = let
565 :     val cxt = (errStrm, span)
566 : jhr 81 fun chk (env, [], dcls') = AST.Program(List.rev dcls')
567 :     | chk (env, dcl::dcls, dcls') = let
568 : jhr 86 val (dcl', env) = checkDecl (env, cxt, dcl)
569 : jhr 81 in
570 :     chk (env, dcls, dcl'::dcls')
571 :     end
572 :     in
573 : jhr 228 chk ({scope=GlobalScope, env=Basis.env}, tree, [])
574 : jhr 81 end
575 : jhr 70
576 : jhr 69 end

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