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 : |
|
|
of ([], ty) => (E_Var x', ty)
|
74 : |
|
|
| (tvs, ty) => raise Fail "unimplemented"
|
75 : |
|
|
(* end case *))
|
76 : |
|
|
| NONE => raise Fail "undefined variable"
|
77 : |
|
|
(* end case *))
|
78 : |
|
|
| PT.E_Lit lit => checkLit lit
|
79 : |
jhr |
70 |
| PT.E_BinOp of expr * var * expr
|
80 : |
|
|
| PT.E_UnaryOp of var * expr
|
81 : |
|
|
| PT.E_Tuple of expr list
|
82 : |
|
|
| PT.E_Apply of var * expr list
|
83 : |
|
|
| PT.E_Cons of ty * expr list
|
84 : |
|
|
| PT.E_Diff of expr
|
85 : |
|
|
| PT.E_Norm of expr
|
86 : |
|
|
(* end case *))
|
87 : |
|
|
|
88 : |
jhr |
72 |
fun checkVarDecl (env, cxt, kind, d) = (case d
|
89 : |
|
|
of PT.VD_Mark m => checkVarDecl (env, #span m, kind, #tree m)
|
90 : |
|
|
| PT.VD_Decl(ty, x, e) => let
|
91 : |
|
|
val ty = checkType ty
|
92 : |
|
|
val x' = Var.new (x, kind, ty)
|
93 : |
|
|
val (e', ty') = checkExpr (env, cxt, e)
|
94 : |
|
|
in
|
95 : |
|
|
(* FIXME: check types *)
|
96 : |
|
|
AST.VD_Decl(x', e')
|
97 : |
|
|
end
|
98 : |
|
|
(* end case *))
|
99 : |
|
|
|
100 : |
jhr |
70 |
(* typecheck a statement and translate it to AST *)
|
101 : |
jhr |
71 |
fun checkStmt (env, cxt, s) = (case s
|
102 : |
|
|
of PT.S_Mark m => checkStmt (env, #span m, #tree m)
|
103 : |
jhr |
72 |
| PT.S_Block stms => let
|
104 : |
|
|
fun chk (_, [], stms) = AST.S_Block(List.rev stms)
|
105 : |
|
|
| chk (env, s::ss, stms) = let
|
106 : |
|
|
val (s', env') = checkStmt (env, cxt, s)
|
107 : |
|
|
in
|
108 : |
|
|
chk (env', ss, s'::ss)
|
109 : |
|
|
end
|
110 : |
|
|
in
|
111 : |
|
|
(chk (env, stms, []), env)
|
112 : |
|
|
end
|
113 : |
|
|
| PT.S_Decl vd => let
|
114 : |
|
|
val vd as AST.VD_Decl(x, _) = checkVarDecl (env, cxt, Var.LocalVar, vd)
|
115 : |
|
|
in
|
116 : |
|
|
(AST.S_Decl vd, Env.insertLocal(env, x, x'))
|
117 : |
|
|
end
|
118 : |
|
|
| PT.S_IfThen(e, s) => let
|
119 : |
|
|
val (e', ty) = checkExpr (env, cxt, e)
|
120 : |
|
|
val s' = checkStmt (env, cxt, s)
|
121 : |
|
|
in
|
122 : |
|
|
(* check that condition has bool type *)
|
123 : |
|
|
case ty
|
124 : |
|
|
of Ty.T_Bool => ()
|
125 : |
|
|
| _ => raise Fail "condition not boolean type"
|
126 : |
|
|
(* end case *);
|
127 : |
|
|
(AST.S_IfThenElse(e', s', AST.S_Block[]), env)
|
128 : |
|
|
end
|
129 : |
|
|
| PT.S_IfThenElse(e, s1, s2) => let
|
130 : |
|
|
val (e', ty) = checkExpr (env, cxt, e)
|
131 : |
|
|
val s1' = checkStmt (env, cxt, s1)
|
132 : |
|
|
val s2' = checkStmt (env, cxt, s2)
|
133 : |
|
|
in
|
134 : |
|
|
(* check that condition has bool type *)
|
135 : |
|
|
case ty
|
136 : |
|
|
of Ty.T_Bool => ()
|
137 : |
|
|
| _ => raise Fail "condition not boolean type"
|
138 : |
|
|
(* end case *);
|
139 : |
|
|
(AST.S_IfThenElse(e', s1', s2'), env)
|
140 : |
|
|
end
|
141 : |
|
|
| PT.S_Assign(x, e) => (case Env.findVar (env, x)
|
142 : |
|
|
of NONE => raise Fail "undefined variable"
|
143 : |
|
|
| SOME x' => let
|
144 : |
|
|
val (e', ty) = checkExpr (env, cxt, e)
|
145 : |
|
|
in
|
146 : |
|
|
(* FIXME: check types *)
|
147 : |
|
|
(* check that x' is mutable *)
|
148 : |
|
|
case Var.kindOf x'
|
149 : |
|
|
of Var.ActorStateVar => ()
|
150 : |
|
|
| Var.LocalVar => ()
|
151 : |
|
|
| _ => raise Fail "assignment to immutable variable"
|
152 : |
|
|
(* end case *);
|
153 : |
|
|
(AST.S_Assign(x', e'), env)
|
154 : |
|
|
end
|
155 : |
|
|
(* end case *))
|
156 : |
|
|
| PT.S_New(actor, args) => let
|
157 : |
|
|
val argTys' = List.map (fn e => checkExpr(env, cxt, e)) args
|
158 : |
|
|
val (args', tys') = ListPair.unzip argTys'
|
159 : |
|
|
in
|
160 : |
|
|
(* FIXME: check that actor is defined and has the argument types match *)
|
161 : |
|
|
AST.S_New(actor, args')
|
162 : |
|
|
end
|
163 : |
|
|
| PT.S_Die => (AST.S_Die, env)
|
164 : |
|
|
| PT.S_Stabilize => (AST.S_Stabilize, env)
|
165 : |
jhr |
70 |
(* end case *))
|
166 : |
|
|
|
167 : |
jhr |
71 |
fun checkDecl (env, cxt, d) = (case d
|
168 : |
|
|
of PT.D_Mark m => checkDecl (env, #span m, #tree m)
|
169 : |
jhr |
72 |
| PT.D_Input(ty, x, optExp) => let
|
170 : |
jhr |
71 |
val ty = checkTy(cxt, ty)
|
171 : |
|
|
val x' = Var.new(x, Var.InputVar, ty)
|
172 : |
|
|
val dcl = (case optExp
|
173 : |
|
|
of NONE => AST.D_Input(x', NONE)
|
174 : |
|
|
| SOME e => let
|
175 : |
|
|
val (e', ty') = checkExpr (env, cxt, e)
|
176 : |
|
|
in
|
177 : |
|
|
(* FIXME: check types *)
|
178 : |
|
|
AST.D_Input(x', SOME e')
|
179 : |
|
|
end
|
180 : |
|
|
(* end case *))
|
181 : |
|
|
in
|
182 : |
|
|
(dcl, Env.insertGlobal(env, x, x'))
|
183 : |
|
|
end
|
184 : |
jhr |
72 |
| PT.D_Var vd => let
|
185 : |
|
|
val vd as AST.VD_Decl(x, _) = checkVarDecl (env, cxt, Var.GlobalVar, vd)
|
186 : |
|
|
in
|
187 : |
|
|
(AST.D_Var vd, Env.insertGlobal(env, x, x'))
|
188 : |
|
|
end
|
189 : |
|
|
| PT.D_Actor{nam, params, state, methods} => ??
|
190 : |
jhr |
70 |
| PT.D_InitialArray of create * iter list
|
191 : |
|
|
| PT.D_InitialCollection of create * iter list
|
192 : |
|
|
(* end case *))
|
193 : |
|
|
*)
|
194 : |
|
|
|
195 : |
jhr |
71 |
fun check (PT.Program dcls) = AST.Program[]
|
196 : |
jhr |
70 |
|
197 : |
jhr |
69 |
end
|