comparison lab2/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 (* lab2/check.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5 open Keiko
6 open Tree
7 open Dict
8
9 (* |err_line| -- line number for error messages *)
10 let err_line = ref 1
11
12 (* |Semantic_error| -- exception raised if error detected *)
13 exception Semantic_error of string * Print.arg list * int
14
15 (* |sem_error| -- issue error message by raising exception *)
16 let sem_error fmt args =
17 raise (Semantic_error (fmt, args, !err_line))
18
19 (* |accum| -- fold_left with arguments swapped *)
20 let rec accum f xs a =
21 match xs with
22 [] -> a
23 | y::ys -> accum f ys (f y a)
24
25 (* |lookup_def| -- find definition of a name, give error is none *)
26 let lookup_def x env =
27 err_line := x.x_line;
28 try let d = lookup x.x_name env in x.x_def <- Some d; d.d_type with
29 Not_found -> sem_error "$ is not declared" [fStr x.x_name]
30
31 (* |add_def| -- add definition to env, give error if already declared *)
32 let add_def d env =
33 try define d env with
34 Exit -> sem_error "$ is already declared" [fStr d.d_tag]
35
36 (* |type_error| -- report a type error. The message could be better. *)
37 let type_error () = sem_error "type mismatch in expression" []
38
39 (* |check_monop| -- check a unary operator and return its type *)
40 let check_monop w t =
41 match w with
42 Uminus ->
43 if t <> Integer then type_error ();
44 Integer
45 | Not ->
46 if t <> Boolean then type_error ();
47 Boolean
48 | _ -> failwith "bad monop"
49
50 (* |check_binop| -- check a binary operator and return its type *)
51 let check_binop w ta tb =
52 match w with
53 Plus | Minus | Times | Div | Mod ->
54 if ta <> Integer || tb <> Integer then type_error ();
55 Integer
56 | Eq | Lt | Gt | Leq | Geq | Neq ->
57 if ta <> tb then type_error ();
58 Boolean
59 | And | Or ->
60 if ta <> Boolean || tb <> Boolean then type_error ();
61 Boolean
62 | _ -> failwith "bad binop"
63
64 (* |check_expr| -- check and annotate an expression *)
65 let rec check_expr e env =
66 let t = expr_type e env in
67 (e.e_type <- t; t)
68
69 (* |expr_type| -- check an expression and return its type *)
70 and expr_type e env =
71 match e.e_guts with
72 Variable x ->
73 lookup_def x env
74 | Sub (v, e) ->
75 failwith "subscripts not implemented"
76 | Constant (n, t) -> t
77 | Monop (w, e1) ->
78 let t = check_expr e1 env in
79 check_monop w t
80 | Binop (w, e1, e2) ->
81 let ta = check_expr e1 env
82 and tb = check_expr e2 env in
83 check_binop w ta tb
84
85 (* |check_stmt| -- check and annotate a statement *)
86 let rec check_stmt s env =
87 match s with
88 Skip -> ()
89 | Seq ss ->
90 List.iter (fun s1 -> check_stmt s1 env) ss
91 | Assign (lhs, rhs) ->
92 let ta = check_expr lhs env
93 and tb = check_expr rhs env in
94 if ta <> tb then sem_error "type mismatch in assignment" []
95 | Print e ->
96 let t = check_expr e env in
97 if t <> Integer then sem_error "print needs an integer" []
98 | Newline ->
99 ()
100 | IfStmt (cond, thenpt, elsept) ->
101 let t = check_expr cond env in
102 if t <> Boolean then
103 sem_error "boolean needed in if statement" [];
104 check_stmt thenpt env;
105 check_stmt elsept env
106 | WhileStmt (cond, body) ->
107 let t = check_expr cond env in
108 if t <> Boolean then
109 sem_error "need boolean after while" [];
110 check_stmt body env
111
112 (* |make_def| -- construct definition of variable *)
113 let make_def x t a = { d_tag = x; d_type = t; d_lab = a }
114
115 (* |check_decl| -- check declaration and return extended environment *)
116 let check_decl (Decl (vs, t)) env0 =
117 let declare x env =
118 let lab = sprintf "_$" [fStr x.x_name] in
119 let d = make_def x.x_name t lab in
120 x.x_def <- Some d; add_def d env in
121 accum declare vs env0
122
123 (* |check_decls| -- check a sequence of declarations *)
124 let check_decls ds env0 =
125 accum check_decl ds env0
126
127 (* |annotate| -- check and annotate a program *)
128 let annotate (Program (ds, ss)) =
129 let env = check_decls ds init_env in
130 check_stmt ss env
131
132