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

# SCM Repository

[diderot] View of /trunk/src/typechecker/typechecker.sml
 [diderot] / trunk / src / typechecker / typechecker.sml

# View of /trunk/src/typechecker/typechecker.sml

Wed May 26 17:28:54 2010 UTC (10 years, 5 months ago) by jhr
File size: 8962 byte(s)
```  Working on typechecker
```
```(* typechecker.sml
*
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
*)

structure Typechecker : sig

val check : ParseTree.program -> AST.program

end = struct

structure PT = ParseTree
structure Ty = Types
structure U = Util

(* check a differentiation level, which muse be >= 0 *)
fun checkDiff (cxt, k) =
if (k < 0)
then raise Fail "differentiation must be >= 0"
else Ty.DiffConst(IntInf.toInt k)

(* check a dimension, which must be 2 or 3 *)
fun checkDim (cxt, d) =
if (d < 2) orelse (3 < d)
then raise Fail "invalid dimension; must be 2 or 3"
else Ty.DimConst(IntInf.toInt d)

(* check a shape *)
fun checkShape (cxt, shape) = let
fun chkDim d = if (d < 1)
then raise Fail "invalid shape dimension; must be >= 1"
else Ty.DimConst(IntInf.toInt d)
in
Ty.Shape(List.map chkDim shape)
end

(* check the well-formedness of a type and translate it to an AST type *)
fun checkTy (cxt, ty) = (case ty
of PT.T_Mark m => checkTy(#span m, #tree m)
| PT.T_Bool => Ty.T_Bool
| PT.T_Int => Ty.T_Int
| PT.T_Real => Ty.realTy
| PT.T_String => Ty.T_String
| PT.T_Vec n => (* NOTE: the parser guarantees that 2 <= n <= 4 *)
Ty.vecTy(IntInf.toInt n)
| PT.T_Kernel k => Ty.T_Kernel(checkDiff(cxt, k))
| PT.T_Field{diff, dim, shape} => Ty.T_Field{
diff = checkDiff (cxt, diff),
dim = checkDim (cxt, dim),
shape = checkShape (cxt, shape)
}
| PT.T_Tensor shape => Ty.T_Tensor(checkShape(cxt, shape))
| PT.T_Image{dim, shape} => Ty.T_Image{
dim = checkDim (cxt, dim),
shape = checkShape (cxt, shape)
}
| PT.T_Array(ty, dims) => raise Fail "Array type"
(* end case *))

fun checkLit lit = (case lit
of (Literal.Int _) => (AST.E_Lit lit, Ty.T_Int)
| (Literal.Float _) => (AST.E_Lit lit, Ty.realTy)
| (Literal.String s) => (AST.E_Lit lit, Ty.T_String)
| (Literal.Bool _) => (AST.E_Lit lit, Ty.T_Bool)
(* end case *))

(* typecheck an expression and translate it to AST *)
fun checkExpr (env, cxt, e) = (case e
of PT.E_Mark m => checkExpr (env, #span m, #tree m)
| PT.E_Var x => (case Env.findVar (env, x)
of SOME x' => let
val (args, ty) = Util.instantiate(Var.typeOf x')
in
(AST.E_Var(x', args, ty), ty)
end
| NONE => raise Fail "undefined variable"
(* end case *))
| PT.E_Lit lit => checkLit lit
| PT.E_OrElse(e1, e2) => let
val (e1', ty1) = checkExpr(env, cxt, e1)
val (e2', ty2) = checkExpr(env, cxt, e2)
in
case (ty1, ty2)
of (Ty.T_Bool, Ty.T_Bool) =>
(AST.E_Cond(e1', AST.E_Lit(Literal.Bool true), e2'), Ty.T_Bool)
| _ => raise Fail "arguments to \"||\" must have bool type"
(* end case *)
end
| PT.E_AndAlso(e1, e2) => let
val (e1', ty1) = checkExpr(env, cxt, e1)
val (e2', ty2) = checkExpr(env, cxt, e2)
in
case (ty1, ty2)
of (Ty.T_Bool, Ty.T_Bool) =>
(AST.E_Cond(e1', e2', AST.E_Lit(Literal.Bool false)), Ty.T_Bool)
| _ => raise Fail "arguments to \"||\" must have bool type"
(* end case *)
end
| PT.E_BinOp(e1, rator, e2) => let
val (e1', ty1) = checkExpr(env, cxt, e1)
val (e2', ty2) = checkExpr(env, cxt, e2)
in
case Basis.findOp rator
of [rator] => let
val (tyArgs, Ty.T_Fun(domTy, rngTy)) = Util.instantiate(Var.typeOf rator)
in
if U.matchTypes(domTy, [ty1, ty2])
then (AST.E_Apply(rator, tyArgs, [e1', e2'], rngTy), rngTy)
else raise Fail "type error for binary operator"
end
| ovldList => raise Fail "unimplemented" (* FIXME *)
(* end case *)
end
| PT.E_UnaryOp(rator, e) => let
val (e', ty) = checkExpr(env, cxt, e)
in
case Basis.findOp rator
of [rator] => let
val (tyArgs, Ty.T_Fun([domTy], rngTy)) = Util.instantiate(Var.typeOf rator)
in
if U.matchType(domTy, ty)
then (AST.E_Apply(rator, tyArgs, [e'], rngTy), rngTy)
else raise Fail "type error for binary operator"
end
| ovldList => raise Fail "unimplemented" (* FIXME *)
(* end case *)
end
| PT.E_Tuple args => let
val (args, tys) = checkExprList (env, cxt, args)
in
raise Fail "E_Tuple not yet implemented"
end
| PT.E_Apply(f, args) => let
val (args, tys) = checkExprList (env, cxt, args)
in
case Env.findVar (env, f)
of SOME f => (case Util.instantiate(Var.typeOf f)
of (tyArgs, Ty.T_Fun(domTy, rngTy)) =>
if U.matchTypes(domTy, tys)
then (AST.E_Apply(f, tyArgs, args, rngTy), rngTy)
else raise Fail "type error for application"
| _ => raise Fail "application of non-function"
(* end case *))
| NONE => raise Fail "unknown function"
(* end case *)
end
| PT.E_Cons(ty, args) => let
val ty = checkTy(cxt, ty)
val (args, tys) = checkExprList (env, cxt, args)
in
raise Fail "unimplemented" (* FIXME *)
end
(* end case *))

(* typecheck a list of expressions returning a list of AST expressions and a list
* of types of the expressions.
*)
and checkExprList (env, cxt, exprs) = let
fun chk (e, (es, tys)) = let
val (e, ty) = checkExpr (env, cxt, e)
in
(e::es, ty::tys)
end
in
List.foldr chk ([], []) exprs
end

fun checkVarDecl (env, cxt, kind, d) = (case d
of PT.VD_Mark m => checkVarDecl (env, #span m, kind, #tree m)
| PT.VD_Decl(ty, x, e) => let
val ty = checkTy (cxt, ty)
val x' = Var.new (x, kind, ty)
val (e', ty') = checkExpr (env, cxt, e)
in
(* FIXME: check types *)
(x, x', e')
end
(* end case *))

(* typecheck a statement and translate it to AST *)
fun checkStmt (env, cxt, s) = (case s
of PT.S_Mark m => checkStmt (env, #span m, #tree m)
| PT.S_Block stms => let
fun chk (_, [], stms) = AST.S_Block(List.rev stms)
| chk (env, s::ss, stms) = let
val (s', env') = checkStmt (env, cxt, s)
in
chk (env', ss, s'::stms)
end
in
(chk (env, stms, []), env)
end
| PT.S_Decl vd => let
val (x, x', e) = checkVarDecl (env, cxt, Var.LocalVar, vd)
in
(AST.S_Decl(AST.VD_Decl(x', e)), Env.insertLocal(env, x, x'))
end
| PT.S_IfThen(e, s) => let
val (e', ty) = checkExpr (env, cxt, e)
val (s', _) = checkStmt (env, cxt, s)
in
(* check that condition has bool type *)
case ty
of Ty.T_Bool => ()
| _ => raise Fail "condition not boolean type"
(* end case *);
(AST.S_IfThenElse(e', s', AST.S_Block[]), env)
end
| PT.S_IfThenElse(e, s1, s2) => let
val (e', ty) = checkExpr (env, cxt, e)
val (s1', _) = checkStmt (env, cxt, s1)
val (s2', _) = checkStmt (env, cxt, s2)
in
(* check that condition has bool type *)
case ty
of Ty.T_Bool => ()
| _ => raise Fail "condition not boolean type"
(* end case *);
(AST.S_IfThenElse(e', s1', s2'), env)
end
| PT.S_Assign(x, e) => (case Env.findVar (env, x)
of NONE => raise Fail "undefined variable"
| SOME x' => let
val (e', ty) = checkExpr (env, cxt, e)
in
(* FIXME: check types *)
(* check that x' is mutable *)
case Var.kindOf x'
of Var.ActorStateVar => ()
| Var.LocalVar => ()
| _ => raise Fail "assignment to immutable variable"
(* end case *);
(AST.S_Assign(x', e'), env)
end
(* end case *))
| PT.S_New(actor, args) => let
val argsAndTys' = List.map (fn e => checkExpr(env, cxt, e)) args
val (args', tys') = ListPair.unzip argsAndTys'
in
(* FIXME: check that actor is defined and has the argument types match *)
(AST.S_New(actor, args'), env)
end
| PT.S_Die => (AST.S_Die, env)
| PT.S_Stabilize => (AST.S_Stabilize, env)
(* end case *))

fun checkDecl (env, cxt, d) = (case d
of PT.D_Mark m => checkDecl (env, #span m, #tree m)
| PT.D_Input(ty, x, optExp) => let
val ty = checkTy(cxt, ty)
val x' = Var.new(x, Var.InputVar, ty)
val dcl = (case optExp
of NONE => AST.D_Input(x', NONE)
| SOME e => let
val (e', ty') = checkExpr (env, cxt, e)
in
(* FIXME: check types *)
AST.D_Input(x', SOME e')
end
(* end case *))
in
(dcl, Env.insertGlobal(env, x, x'))
end
| PT.D_Var vd => let
val (x, x', e') = checkVarDecl (env, cxt, Var.GlobalVar, vd)
in
(AST.D_Var(AST.VD_Decl(x', e')), Env.insertGlobal(env, x, x'))
end
| PT.D_Actor{name, params, state, methods} => raise Fail "unimplemented" (* FIXME *)
| PT.D_InitialArray(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
| PT.D_InitialCollection(bindings, iterators) => raise Fail "unimplemented" (* FIXME *)
(* end case *))

fun check (PT.Program{span, tree}) = let
fun chk (env, [], dcls') = AST.Program(List.rev dcls')
| chk (env, dcl::dcls, dcls') = let
val (dcl', env) = checkDecl (env, span, dcl)
in
chk (env, dcls, dcl'::dcls')
end
in
chk (Basis.env, tree, [])
end

end
```

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