-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_context.ml
93 lines (61 loc) · 2.08 KB
/
type_context.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
open Objlang
open Error_type
(** types for various environments *)
module Env = Map.Make (String)
type type_context =
{ variables: typ Env.t
; functions: untyped_function Env.t
; classes: untyped_class Env.t }
let make_empty_context () =
{variables= Env.empty; functions= Env.empty; classes= Env.empty}
let add_variable context id t =
{context with variables= Env.add id t context.variables}
let reduce_list context op l = List.fold_left op context l
let remove_variable context id =
{context with variables= Env.remove id context.variables}
let find_variable context id =
match Env.find_opt id context.variables with
| Some t ->
t
| None ->
raise (UndefinedError (Variable id))
let add_function context id f =
{context with functions= Env.add id f context.functions}
let remove_function context id =
{context with functions= Env.remove id context.functions}
let find_function context id =
match Env.find_opt id context.functions with
| Some f ->
f
| None ->
raise (UndefinedError (Function id))
let add_class context id c = {context with classes= Env.add id c context.classes}
let remove_class context id =
{context with classes= Env.remove id context.classes}
let find_class context id =
match Env.find_opt id context.classes with
| Some f ->
f
| None ->
raise (UndefinedError (Class id))
type method_identifier = {classname: string; methodname: string}
let find_method context id =
let class_def = find_class context id.classname in
match
List.find_opt
(fun (def : untyped_function) -> def.name = id.methodname)
class_def.methods
with
| Some d ->
d
| None ->
raise (UndefinedError (Method (class_def.name, id.methodname)))
let add_variables context =
reduce_list context (fun ctx (id, t) -> add_variable ctx id t)
let add_functions context =
reduce_list context (fun ctx (def : untyped_function) ->
add_function ctx def.name def )
let add_classes context =
reduce_list context (fun ctx (def : untyped_class) ->
add_class ctx def.name def )
let bind_1 arg f = f arg