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 370 - (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 : 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 :     (AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2'), 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 :     (AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false)), 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 :     then (AST.E_Cond(cond', e1', e2'), ty1)
175 :     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 :     val (tyArgs, Ty.T_Fun([domTy], rngTy)) = Util.instantiate(Var.typeOf rator)
208 :     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 :     | PT.E_Tuple args => let
221 :     val (args, tys) = checkExprList (env, cxt, args)
222 :     in
223 :     raise Fail "E_Tuple not yet implemented"
224 :     end
225 :     | PT.E_Apply(f, args) => let
226 :     val (args, tys) = checkExprList (env, cxt, args)
227 :     in
228 : jhr 169 case Env.findFunc (#env env, f)
229 :     of SOME f =>
230 : jhr 228 if (inActor env) andalso (Basis.isRestricted f)
231 :     then err(cxt, [S "use of restricted operation ", V f, S " in actor body"])
232 :     else (case Util.instantiate(Var.typeOf f)
233 : jhr 169 of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
234 :     if U.matchTypes(domTy, tys)
235 :     then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
236 :     else err(cxt, [
237 :     S "type error in application of ", V f, S "\n",
238 :     S " expected: ", TYS domTy, S "\n",
239 :     S " but found: ", TYS tys, S "\n"
240 :     ])
241 :     | _ => err(cxt, [S "application of non-function ", V f])
242 :     (* end case *))
243 : jhr 99 | NONE => err(cxt, [S "unknown function ", A f])
244 : jhr 81 (* end case *)
245 :     end
246 : jhr 86 | PT.E_Cons args => let
247 :     val (args, ty::tys) = checkExprList (env, cxt, args)
248 : jhr 81 in
249 : jhr 96 case TU.pruneHead ty
250 : jhr 86 of Ty.T_Tensor shape => let
251 :     fun chkTy ty' = U.matchType(ty, ty')
252 :     val resTy = Ty.T_Tensor(Ty.shapeExt(shape, Ty.DimConst(List.length args)))
253 : jhr 83 in
254 : jhr 86 if List.all chkTy tys
255 :     then (AST.E_Cons args, resTy)
256 : jhr 99 else err(cxt, [S "arguments of tensor construction must have same type"])
257 : jhr 83 end
258 : jhr 99 | _ => err(cxt, [S "Invalid argument type for tensor construction"])
259 : jhr 83 (* end case *)
260 : jhr 81 end
261 : jhr 86 | PT.E_Real e => (case checkExpr (env, cxt, e)
262 :     of (e', Ty.T_Int) =>
263 :     (AST.E_Apply(BasisVars.i2r, [], [e'], Ty.realTy), Ty.realTy)
264 : jhr 99 | _ => err(cxt, [S "argument of real conversion must be int"])
265 : jhr 86 (* end case *))
266 : jhr 70 (* end case *))
267 :    
268 : jhr 81 (* typecheck a list of expressions returning a list of AST expressions and a list
269 :     * of types of the expressions.
270 :     *)
271 :     and checkExprList (env, cxt, exprs) = let
272 :     fun chk (e, (es, tys)) = let
273 :     val (e, ty) = checkExpr (env, cxt, e)
274 :     in
275 :     (e::es, ty::tys)
276 :     end
277 :     in
278 :     List.foldr chk ([], []) exprs
279 :     end
280 :    
281 : jhr 72 fun checkVarDecl (env, cxt, kind, d) = (case d
282 : jhr 86 of PT.VD_Mark m => checkVarDecl (env, (#1 cxt, #span m), kind, #tree m)
283 : jhr 72 | PT.VD_Decl(ty, x, e) => let
284 : jhr 81 val ty = checkTy (cxt, ty)
285 : jhr 72 val x' = Var.new (x, kind, ty)
286 :     val (e', ty') = checkExpr (env, cxt, e)
287 :     in
288 : jhr 99 (* FIXME: this check is not flexible enough; should allow lhs type to support
289 :     * fewer levels of differentiation than rhs provides.
290 :     *)
291 :     if U.matchType(ty, ty')
292 :     then (x, x', e')
293 :     else err(cxt, [
294 :     S "type of variable ", A x,
295 :     S " does not match type of initializer\n",
296 :     S " expected: ", TY ty, S "\n",
297 :     S " but found: ", TY ty', S "\n"
298 :     ])
299 : jhr 72 end
300 :     (* end case *))
301 :    
302 : jhr 70 (* typecheck a statement and translate it to AST *)
303 : jhr 71 fun checkStmt (env, cxt, s) = (case s
304 : jhr 86 of PT.S_Mark m => checkStmt (withEnvAndContext (env, cxt, m))
305 : jhr 72 | PT.S_Block stms => let
306 :     fun chk (_, [], stms) = AST.S_Block(List.rev stms)
307 :     | chk (env, s::ss, stms) = let
308 :     val (s', env') = checkStmt (env, cxt, s)
309 :     in
310 : jhr 81 chk (env', ss, s'::stms)
311 : jhr 72 end
312 :     in
313 :     (chk (env, stms, []), env)
314 :     end
315 :     | PT.S_Decl vd => let
316 : jhr 228 val (x, x', e) = checkVarDecl (methodScope env, cxt, Var.LocalVar, vd)
317 : jhr 72 in
318 : jhr 228 (AST.S_Decl(AST.VD_Decl(x', e)), insertLocal(env, x, x'))
319 : jhr 72 end
320 :     | PT.S_IfThen(e, s) => let
321 : jhr 228 val (e', ty) = checkExpr (env, cxt, e)
322 : jhr 81 val (s', _) = checkStmt (env, cxt, s)
323 : jhr 72 in
324 :     (* check that condition has bool type *)
325 :     case ty
326 :     of Ty.T_Bool => ()
327 : jhr 99 | _ => err(cxt, [S "condition not boolean type"])
328 : jhr 72 (* end case *);
329 :     (AST.S_IfThenElse(e', s', AST.S_Block[]), env)
330 :     end
331 :     | PT.S_IfThenElse(e, s1, s2) => let
332 : jhr 228 val (e', ty) = checkExpr (env, cxt, e)
333 : jhr 81 val (s1', _) = checkStmt (env, cxt, s1)
334 :     val (s2', _) = checkStmt (env, cxt, s2)
335 : jhr 72 in
336 :     (* check that condition has bool type *)
337 :     case ty
338 :     of Ty.T_Bool => ()
339 : jhr 99 | _ => err(cxt, [S "condition not boolean type"])
340 : jhr 72 (* end case *);
341 :     (AST.S_IfThenElse(e', s1', s2'), env)
342 :     end
343 : jhr 228 | PT.S_Assign(x, e) => (case Env.findVar (#env env, x)
344 : jhr 99 of NONE => err(cxt, [
345 :     S "undefined variable ", A x
346 :     ])
347 : jhr 72 | SOME x' => let
348 : jhr 99 (* FIXME: check for polymorphic variables *)
349 :     val ([], ty) = Var.typeOf x'
350 : jhr 228 val (e', ty') = checkExpr (env, cxt, e)
351 : jhr 72 in
352 : jhr 99 if U.matchType(ty, ty')
353 :     then (x, x', e')
354 :     else err(cxt, [
355 :     S "type of assigned variable ", A x,
356 :     S " does not match type of rhs\n",
357 :     S " expected: ", TY ty, S "\n",
358 :     S " but found: ", TY ty', S "\n"
359 :     ]);
360 : jhr 72 (* check that x' is mutable *)
361 :     case Var.kindOf x'
362 :     of Var.ActorStateVar => ()
363 : jhr 173 | Var.ActorOutputVar => ()
364 : jhr 72 | Var.LocalVar => ()
365 : jhr 99 | _ => err(cxt, [
366 :     S "assignment to immutable variable ", A x
367 :     ])
368 : jhr 72 (* end case *);
369 :     (AST.S_Assign(x', e'), env)
370 :     end
371 :     (* end case *))
372 :     | PT.S_New(actor, args) => let
373 : jhr 228 val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
374 : jhr 81 val (args', tys') = ListPair.unzip argsAndTys'
375 : jhr 72 in
376 : jhr 228 case #scope env
377 :     of MethodScope => ()
378 :     | InitScope => ()
379 :     | _ => err(cxt, [S "invalid scope for new actor"])
380 :     (* end case *);
381 : jhr 72 (* FIXME: check that actor is defined and has the argument types match *)
382 : jhr 81 (AST.S_New(actor, args'), env)
383 : jhr 72 end
384 : jhr 228 | PT.S_Die => (
385 :     case #scope env
386 : jhr 235 of MethodScope => ()
387 :     | _ => err(cxt, [S "\"die\" statment outside of method"])
388 : jhr 228 (* end case *);
389 :     (AST.S_Die, env))
390 :     | PT.S_Stabilize => (
391 :     case #scope env
392 : jhr 235 of MethodScope => ()
393 :     | _ => err(cxt, [S "\"stabilize\" statment outside of method"])
394 : jhr 228 (* end case *);
395 :     (AST.S_Stabilize, env))
396 : jhr 70 (* end case *))
397 :    
398 : jhr 82 fun checkParams (env, cxt, params) = let
399 :     fun chkParam (env, cxt, param) = (case param
400 : jhr 86 of PT.P_Mark m => chkParam (withEnvAndContext (env, cxt, m))
401 : jhr 82 | PT.P_Param(ty, x) => let
402 :     val x' = Var.new(x, AST.ActorParam, checkTy (cxt, ty))
403 :     in
404 : jhr 228 (x', insertLocal(env, x, x'))
405 : jhr 82 end
406 :     (* end case *))
407 :     fun chk (param, (xs, env)) = let
408 :     val (x, env) = chkParam (env, cxt, param)
409 :     in
410 :     (x::xs, env)
411 :     end
412 :     in
413 :     (* FIXME: need to check for multiple occurences of the same parameter name! *)
414 :     List.foldr chk ([], env) params
415 :     end
416 :    
417 :     fun checkMethod (env, cxt, meth) = (case meth
418 : jhr 86 of PT.M_Mark m => checkMethod (withEnvAndContext (env, cxt, m))
419 : jhr 82 | PT.M_Method(name, body) => let
420 : jhr 228 val (body, _) = checkStmt(methodScope env, cxt, body)
421 : jhr 82 in
422 :     AST.M_Method(name, body)
423 :     end
424 :     (* end case *))
425 :    
426 :     fun checkActor (env, cxt, {name, params, state, methods}) = let
427 :     (* check the actor parameters *)
428 :     val (params, env) = checkParams (env, cxt, params)
429 :     (* check the actor state variable definitions *)
430 :     val (vds, env) = let
431 : jhr 164 fun checkStateVar ((isOut, vd), (vds, env)) = let
432 : jhr 173 val kind = if isOut then AST.ActorOutputVar else AST.ActorStateVar
433 : jhr 228 val (x, x', e') = checkVarDecl (env, cxt, kind, vd)
434 : jhr 82 in
435 : jhr 228 (* check that output variables have value types *)
436 :     if isOut andalso not(TU.isValueType(Var.monoTypeOf x'))
437 :     then err(cxt, [
438 :     S "output variable ", V x', S " has non-value type ",
439 :     TY(Var.monoTypeOf x')
440 :     ])
441 :     else ();
442 :     (AST.VD_Decl(x', e')::vds, insertLocal(env, x, x'))
443 : jhr 82 end
444 :     val (vds, env) = List.foldl checkStateVar ([], env) state
445 :     in
446 :     (List.rev vds, env)
447 :     end
448 :     (* check the actor methods *)
449 :     val methods = List.map (fn m => checkMethod (env, cxt, m)) methods
450 :     in
451 :     AST.D_Actor{name = name, params = params, state = vds, methods = methods}
452 :     end
453 :    
454 : jhr 89 fun checkCreate (env, cxt, PT.C_Mark m) = checkCreate (withEnvAndContext (env, cxt, m))
455 :     | checkCreate (env, cxt, PT.C_Create(actor, args)) = let
456 : jhr 228 val (args, tys) = checkExprList (env, cxt, args)
457 : jhr 89 in
458 :     (* FIXME: check against actor definition *)
459 :     AST.C_Create(actor, args)
460 :     end
461 :    
462 :     fun checkIter (env, cxt, PT.I_Mark m) = checkIter (withEnvAndContext (env, cxt, m))
463 :     | checkIter (env, cxt, PT.I_Range(x, e1, e2)) = let
464 : jhr 228 val (e1', ty1) = checkExpr (env, cxt, e1)
465 :     val (e2', ty2) = checkExpr (env, cxt, e2)
466 : jhr 89 val x' = Var.new(x, Var.LocalVar, Ty.T_Int)
467 : jhr 228 val env' = insertLocal(env, x, x')
468 : jhr 89 in
469 :     case (ty1, ty2)
470 :     of (Ty.T_Int, Ty.T_Int) => (AST.I_Range(x', e1', e2'), env')
471 :     | _ => err(cxt, [
472 :     S "range expressions must have integer type\n",
473 :     S " but found: ", TY ty1, S " .. ", TY ty2, S "\n"
474 :     ])
475 :     (* end case *)
476 :     end
477 :    
478 :     fun checkIters (env, cxt, iters) = let
479 :     fun chk (env, [], iters) = (List.rev iters, env)
480 :     | chk (env, iter::rest, iters) = let
481 :     val (iter, env) = checkIter (env, cxt, iter)
482 :     in
483 :     chk (env, rest, iter::iters)
484 :     end
485 :     in
486 :     chk (env, iters, [])
487 :     end
488 :    
489 : jhr 71 fun checkDecl (env, cxt, d) = (case d
490 : jhr 86 of PT.D_Mark m => checkDecl (withEnvAndContext (env, cxt, m))
491 : jhr 72 | PT.D_Input(ty, x, optExp) => let
492 : jhr 71 val ty = checkTy(cxt, ty)
493 :     val x' = Var.new(x, Var.InputVar, ty)
494 :     val dcl = (case optExp
495 :     of NONE => AST.D_Input(x', NONE)
496 :     | SOME e => let
497 : jhr 228 val (e', ty') = checkExpr (env, cxt, e)
498 : jhr 71 in
499 : jhr 89 if U.matchType (ty, ty')
500 :     then AST.D_Input(x', SOME e')
501 :     else err(cxt, [
502 :     S "definition of ", V x', S " has wrong type\n",
503 :     S " expected: ", TY ty, S "\n",
504 :     S " but found: ", TY ty', S "\n"
505 :     ])
506 : jhr 71 end
507 :     (* end case *))
508 :     in
509 : jhr 228 (* check that input variables have value types *)
510 :     if not(TU.isValueType ty)
511 :     then err(cxt, [S "input variable ", V x', S " has non-value type ", TY ty])
512 :     else ();
513 :     (dcl, insertGlobal(env, x, x'))
514 : jhr 71 end
515 : jhr 72 | PT.D_Var vd => let
516 : jhr 228 val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
517 : jhr 72 in
518 : jhr 228 (AST.D_Var(AST.VD_Decl(x', e')), insertGlobal(env, x, x'))
519 : jhr 72 end
520 : jhr 228 | PT.D_Actor arg => (checkActor(actorScope env, cxt, arg), env)
521 : jhr 89 | PT.D_InitialArray(create, iterators) => let
522 : jhr 228 val env = initScope env
523 : jhr 89 val (iterators, env') = checkIters (env, cxt, iterators)
524 :     val create = checkCreate (env', cxt, create)
525 :     in
526 :     (AST.D_InitialArray(create, iterators), env)
527 :     end
528 :     | PT.D_InitialCollection(create, iterators) => let
529 : jhr 228 val env = initScope env
530 : jhr 89 val (iterators, env') = checkIters (env, cxt, iterators)
531 :     val create = checkCreate (env', cxt, create)
532 :     in
533 :     (AST.D_InitialCollection(create, iterators), env)
534 :     end
535 : jhr 70 (* end case *))
536 :    
537 : jhr 86 fun check errStrm (PT.Program{span, tree}) = let
538 :     val cxt = (errStrm, span)
539 : jhr 81 fun chk (env, [], dcls') = AST.Program(List.rev dcls')
540 :     | chk (env, dcl::dcls, dcls') = let
541 : jhr 86 val (dcl', env) = checkDecl (env, cxt, dcl)
542 : jhr 81 in
543 :     chk (env, dcls, dcl'::dcls')
544 :     end
545 :     in
546 : jhr 228 chk ({scope=GlobalScope, env=Basis.env}, tree, [])
547 : jhr 81 end
548 : jhr 70
549 : jhr 69 end

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