Source file macro_base.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
[%%prepare_logger]
module Xlist = Diffast_misc.Xlist
module Xstring = Diffast_misc.Xstring
module Loc = Astloc
type stat =
| Unresolved
| Resolved of Obj.t
let stat_to_string = function
| Unresolved -> "UNRESOLVED"
| Resolved _ -> "RESOLVED"
let stat_resolved = function
| Resolved _ -> true
| _ -> false
let tok_of_stat = function
| Resolved o -> Obj.obj o
| _ -> raise Not_found
type line = { ln_raw : string;
mutable ln_stat : stat;
mutable ln_conditional : bool;
ln_loc : Loc.t;
}
let mk_line ?(loc=Loc.dummy) ?(raw="<predefined>") ?(conditional=false) mktok id =
{ ln_raw=raw;
ln_stat=Resolved (Obj.repr (mktok id));
ln_conditional=conditional;
ln_loc=loc;
}
let line_to_string { ln_raw = raw;
ln_stat = stat;
ln_conditional = b;
ln_loc = loc;
} =
Printf.sprintf "{%s%s:%s%s}"
(stat_to_string stat)
(if b then ":conditional" else "")
raw
(if loc = Loc.dummy then "" else "@"^(Loc.to_string loc))
let tok_of_line ln = tok_of_stat ln.ln_stat
let _resolve_line ln x = ln.ln_stat <- Resolved x
let resolve_line ln x = _resolve_line ln (Obj.repr x)
let line_resolved ln = stat_resolved ln.ln_stat
type body =
| Object of line
| Function of string list * line
let mk_obj_body ?(loc=Loc.dummy) ?(stat=Unresolved) ?(conditional=false) s =
Object { ln_raw=Xstring.strip s;
ln_stat=stat;
ln_conditional=conditional;
ln_loc=loc;
}
let mk_fun_body ?(loc=Loc.dummy) ?(stat=Unresolved) ?(conditional=false) sl s =
Function(sl, { ln_raw=Xstring.strip s;
ln_stat=stat;
ln_conditional=conditional;
ln_loc=loc;
}
)
let line_of_body = function
| Object ln
| Function(_, ln) -> ln
let body_to_string = function
| Object ln -> Printf.sprintf "OBJ%s" (line_to_string ln)
| Function(sl, ln) -> Printf.sprintf "FUN(%s)%s" (String.concat "," sl) (line_to_string ln)
let body_to_rep = function
| Object ln -> Printf.sprintf "{%s}" ln.ln_raw
| Function(sl, ln) -> Printf.sprintf "(%s){%s}" (String.concat "," sl) ln.ln_raw
let body_length = function
| Object ln -> String.length ln.ln_raw
| Function(_, ln) -> String.length ln.ln_raw
let _resolve_body x = function
| Object ln
| Function(_, ln) -> _resolve_line ln x
let resolve_body x = _resolve_body (Obj.repr x)
let body_is_conditional = function
| Object ln
| Function(_, ln) -> ln.ln_conditional
let body_set_conditional = function
| Object ln
| Function(_, ln) -> ln.ln_conditional <- true
let body_clear_conditional = function
| Object ln
| Function(_, ln) -> ln.ln_conditional <- false
[%%capture_path
class table (tname : string) = object (self)
val tbl : (string, body) Hashtbl.t = Hashtbl.create 0
val mutable readonly_flag = false
val hidden : (string, int) Hashtbl.t = Hashtbl.create 0
method name = tname
method hide id =
let n =
try
Hashtbl.find hidden id
with
Not_found -> 0
in
let n' = n + 1 in
[%debug_log "[%s] %s: %d -> %d" tname id n n'];
Hashtbl.replace hidden id n'
method expose id =
let n =
try
Hashtbl.find hidden id
with
Not_found -> 0
in
let n' = n - 1 in
[%debug_log "[%s] %s: %d -> %d" tname id n n'];
if n' = 0 then
Hashtbl.remove hidden id
else
Hashtbl.replace hidden id n'
method readonly = readonly_flag
method set_readonly = readonly_flag <- true
method find id =
try
let n = Hashtbl.find hidden id in
let l = Hashtbl.find_all tbl id in
List.nth l n
with
| Not_found -> Hashtbl.find tbl id
| Failure _ -> raise Not_found
method find_all id =
try
let n = Hashtbl.find hidden id in
let l = Hashtbl.find_all tbl id in
let r = ref l in
try
for _ = 1 to n do
r := List.tl !r
done;
!r
with
Failure _ -> []
with
Not_found -> Hashtbl.find_all tbl id
method define ?(conditional=false) id body =
if not readonly_flag then begin
if conditional then
body_set_conditional body;
[%debug_log "[%s] \"%s\" --> %s%s"
tname id (body_to_string body) (if conditional then " (conditional)" else "")];
Hashtbl.add tbl id body
end
method undefine id =
if not readonly_flag then begin
[%debug_log "[%s] \"%s\"" tname id];
Hashtbl.remove tbl id
end
method clear =
Hashtbl.clear tbl
method is_defined s = Hashtbl.mem tbl s
method is_uniquely_defined s =
try
match self#find_all s with
| [_] -> true
| _ -> false
with
Not_found -> false
method is_unconditionally_defined id =
try
match self#find_all id with
| [] ->
[%debug_log "not found: %s" id];
false
| bodies ->
[%debug_log "found: %s ->\n%s" id (Xlist.to_string body_to_string "\n" bodies)];
List.exists (fun body -> not (body_is_conditional body)) bodies
with
Not_found -> false
method is_undefined s = not (Hashtbl.mem tbl s)
end
]