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 71 - (view) (download)
Original Path: trunk/src/typechecker/typechecker.sml

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 :     else Ty.NatConst(IntInf.toInt k)
21 :    
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 :     else Ty.NatConst(IntInf.toInt d)
27 :    
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 :     else Ty.NatConst(IntInf.toInt d)
33 :     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 :     (* typecheck a statement and translate it to AST *)
89 : jhr 71 fun checkStmt (env, cxt, s) = (case s
90 :     of PT.S_Mark m => checkStmt (env, #span m, #tree m)
91 : jhr 70 | PT.S_Block of stmt list
92 :     | PT.S_Decl of var_decl
93 :     | PT.S_IfThen of expr * stmt
94 :     | PT.S_IfThenElse of expr * stmt * stmt
95 :     | PT.S_Assign of var * expr
96 :     | PT.S_New of var * expr list
97 :     | PT.S_Die
98 :     | PT.S_Stabilize
99 :     (* end case *))
100 :    
101 : jhr 71 fun checkDecl (env, cxt, d) = (case d
102 :     of PT.D_Mark m => checkDecl (env, #span m, #tree m)
103 :     | PT.D_Input(ty, x, optExp) = let
104 :     val ty = checkTy(cxt, ty)
105 :     val x' = Var.new(x, Var.InputVar, ty)
106 :     val dcl = (case optExp
107 :     of NONE => AST.D_Input(x', NONE)
108 :     | SOME e => let
109 :     val (e', ty') = checkExpr (env, cxt, e)
110 :     in
111 :     (* FIXME: check types *)
112 :     AST.D_Input(x', SOME e')
113 :     end
114 :     (* end case *))
115 :     in
116 :     (dcl, Env.insertGlobal(env, x, x'))
117 :     end
118 : jhr 70 | PT.D_Var of var_decl (* global variable decl *)
119 :     | PT.D_Actor of { (* actor decl *)
120 :     name : var,
121 :     params : param list,
122 :     state : var_decl list,
123 :     methods : method list
124 :     }
125 :     | PT.D_InitialArray of create * iter list
126 :     | PT.D_InitialCollection of create * iter list
127 :     (* end case *))
128 :     *)
129 :    
130 : jhr 71 fun check (PT.Program dcls) = AST.Program[]
131 : jhr 70
132 : jhr 69 end

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