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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 70 - (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 :     *)
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 :     of PT.T_Mark m => checkTy(cxt, #tree m) (* FIXME track context *)
40 :     | 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 70 (*
61 :     (* typecheck an expression and translate it to AST *)
62 :     fun checkExpr (env, e) = (case e
63 :     of PT.E_Mark of expr mark
64 :     | PT.E_Var of var
65 :     | PT.E_Lit of Literal.literal
66 :     | PT.E_BinOp of expr * var * expr
67 :     | PT.E_UnaryOp of var * expr
68 :     | PT.E_Tuple of expr list
69 :     | PT.E_Apply of var * expr list
70 :     | PT.E_Cons of ty * expr list
71 :     | PT.E_Diff of expr
72 :     | PT.E_Norm of expr
73 :     (* end case *))
74 :    
75 :     (* typecheck a statement and translate it to AST *)
76 :     fun checkStmt (env, s) = (case s
77 :     of PT.S_Mark of stmt mark
78 :     | PT.S_Block of stmt list
79 :     | PT.S_Decl of var_decl
80 :     | PT.S_IfThen of expr * stmt
81 :     | PT.S_IfThenElse of expr * stmt * stmt
82 :     | PT.S_Assign of var * expr
83 :     | PT.S_New of var * expr list
84 :     | PT.S_Die
85 :     | PT.S_Stabilize
86 :     (* end case *))
87 :    
88 :     fun checkDecl (env, d) = (case d
89 :     of PT.D_Mark of decl mark
90 :     | PT.D_Input of ty * var * expr option (* input variable decl with optional default *)
91 :     | PT.D_Var of var_decl (* global variable decl *)
92 :     | PT.D_Actor of { (* actor decl *)
93 :     name : var,
94 :     params : param list,
95 :     state : var_decl list,
96 :     methods : method list
97 :     }
98 :     | PT.D_InitialArray of create * iter list
99 :     | PT.D_InitialCollection of create * iter list
100 :     (* end case *))
101 :     *)
102 :    
103 :     fun check (PT.Program dcls) = AST.Program()
104 :    
105 : jhr 69 end

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