comparison lab3/check.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab3/check.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Tree
5 open Dict
6 open Print
7
8 (* |err_line| -- line number for error messages *)
9 let err_line = ref 1
10
11 (* |Semantic_error| -- exception raised if error detected *)
12 exception Semantic_error of string * Print.arg list * int
13
14 (* |sem_error| -- issue error message by raising exception *)
15 let sem_error fmt args =
16 raise (Semantic_error (fmt, args, !err_line))
17
18 (* |accum| -- fold_left with arguments swapped *)
19 let rec accum f xs a =
20 match xs with
21 [] -> a
22 | y::ys -> accum f ys (f y a)
23
24 (* |lookup_def| -- find definition of a name, give error is none *)
25 let lookup_def x env =
26 err_line := x.x_line;
27 try let d = lookup x.x_name env in x.x_def <- Some d; d with
28 Not_found -> sem_error "$ is not declared" [fStr x.x_name]
29
30 (* |add_def| -- add definition to env, give error if already declared *)
31 let add_def d env =
32 try define d env with
33 Exit -> sem_error "$ is already declared" [fStr d.d_tag]
34
35 (* |check_expr| -- check and annotate an expression *)
36 let rec check_expr e env =
37 match e with
38 Constant n -> ()
39 | Variable x ->
40 let d = lookup_def x env in
41 begin
42 match d.d_kind with
43 VarDef -> ()
44 | ProcDef _ ->
45 sem_error "$ is not a variable" [fStr x.x_name]
46 end
47 | Monop (w, e1) ->
48 check_expr e1 env
49 | Binop (w, e1, e2) ->
50 check_expr e1 env;
51 check_expr e2 env
52 | Call (p, args) ->
53 let d = lookup_def p env in
54 begin
55 match d.d_kind with
56 VarDef ->
57 sem_error "$ is not a procedure" [fStr p.x_name]
58 | ProcDef nargs ->
59 if List.length args <> nargs then
60 sem_error "procedure $ needs $ arguments"
61 [fStr p.x_name; fNum nargs];
62 end;
63 List.iter (fun e1 -> check_expr e1 env) args
64
65 (* |check_stmt| -- check and annotate a statement *)
66 let rec check_stmt s inproc env =
67 match s with
68 Skip -> ()
69 | Seq ss ->
70 List.iter (fun s1 -> check_stmt s1 inproc env) ss
71 | Assign (x, e) ->
72 let d = lookup_def x env in
73 begin
74 match d.d_kind with
75 VarDef -> check_expr e env
76 | ProcDef _ ->
77 sem_error "$ is not a variable" [fStr x.x_name]
78 end
79 | Return e ->
80 if not inproc then
81 sem_error "return statement only allowed in procedure" [];
82 check_expr e env
83 | IfStmt (test, thenpt, elsept) ->
84 check_expr test env;
85 check_stmt thenpt inproc env;
86 check_stmt elsept inproc env
87 | WhileStmt (test, body) ->
88 check_expr test env;
89 check_stmt body inproc env
90 | Print e ->
91 check_expr e env
92 | Newline ->
93 ()
94
95 (* |serialize| -- number a list, starting from 0 *)
96 let serialize xs =
97 let rec count i =
98 function
99 [] -> []
100 | x :: xs -> (i, x) :: count (i+1) xs in
101 count 0 xs
102
103 (*
104 Frame layout
105
106 arg n
107 ...
108 fp+16: arg 1
109 fp+12: static link
110 fp+8: current cp
111 fp+4: return addr
112 fp: dynamic link
113 fp-4: local 1
114 ...
115 local m
116 *)
117
118 let arg_base = 16
119 let loc_base = 0
120
121 (* |declare_local| -- declare a formal parameter or local *)
122 let declare_local x lev off env =
123 let d = { d_tag = x; d_kind = VarDef; d_level = lev;
124 d_lab = ""; d_off = off } in
125 add_def d env
126
127 (* |declare_global| -- declare a global variable *)
128 let declare_global x env =
129 let d = { d_tag = x; d_kind = VarDef; d_level = 0;
130 d_lab = sprintf "_$" [fStr x]; d_off = 0 } in
131 add_def d env
132
133 (* |declare_proc| -- declare a procedure *)
134 let declare_proc (Proc (p, formals, body)) lev env =
135 let lab = sprintf "$_$" [fStr p.x_name; fNum (label ())] in
136 let d = { d_tag = p.x_name;
137 d_kind = ProcDef (List.length formals); d_level = lev;
138 d_lab = lab; d_off = 0 } in
139 p.x_def <- Some d; add_def d env
140
141 (* |check_proc| -- check a procedure body *)
142 let rec check_proc (Proc (p, formals, Block (vars, procs, body))) lev env =
143 err_line := p.x_line;
144 let env' =
145 accum (fun (i, x) -> declare_local x lev (arg_base + 4*i))
146 (serialize formals) (new_block env) in
147 let env'' =
148 accum (fun (i, x) -> declare_local x lev (loc_base - 4*(i+1)))
149 (serialize vars) env' in
150 let env''' =
151 accum (fun d -> declare_proc d (lev+1)) procs env'' in
152 List.iter (fun d -> check_proc d (lev+1) env''') procs;
153 check_stmt body true env'''
154
155 (* |annotate| -- check and annotate a program *)
156 let annotate (Program (Block (vars, procs, body))) =
157 let env = accum declare_global vars empty in
158 let env' = accum (fun d -> declare_proc d 1) procs env in
159 List.iter (fun d -> check_proc d 1 env') procs;
160 check_stmt body false env'