eval.ml 10.3 KB
Newer Older
1
2
open Value
open Run_dispatch
3
open Ident
4
open Lambda
5

6
let ns_table = ref Ns.empty_table
7

8
9
10
11
let ops = Hashtbl.create 13
let register_op = Hashtbl.add ops
let eval_op = Hashtbl.find ops

12
13
14
15
16
17
18
let print_to_string f x =
  let b = Buffer.create 1024 in
  let ppf = Format.formatter_of_buffer b in
  f ppf x;
  Buffer.contents b


19
(* To write tail-recursive map-like iteration *)
20

21
let make_accu () = Value.Pair(nil,Absent)
22
let get_accu a = snd (Obj.magic a)
23
let map f v = let acc0 = make_accu () in set_cdr (f acc0 v) nil; get_accu acc0
24

25
26
let make_branches = Patterns.Compile.(*Auto_opt.*)make_branches

27
28
29
30
let dispatcher brs =
  match brs.brs_compiled with
    | Some d -> d
    | None ->
31
32
(*	Format.fprintf Format.std_formatter "Start compilation...@."; 
	let time = Unix.gettimeofday() in*)
33
	let x = make_branches brs.brs_input brs.brs in
34
35
36
37
38
(*	let time = Unix.gettimeofday() -. time in
	if time > 1.0 then 
	  Format.fprintf Format.std_formatter "%a@." 
	    Patterns.Compile.print_dispatcher (fst x); 
	Format.fprintf Format.std_formatter "(%f ms).@." time; *)
39
40
41
42
43
44
45
	brs.brs_compiled <- Some x;
	x

let stack = ref (Array.create 1024 Value.Absent)
let frame = ref 0
let sp = ref 0

46

47
48
49
let dump ppf =
  Format.fprintf ppf "sp = %i   frame = %i@." !sp !frame;
  for i = 0 to !sp - 1 do
50
51
    Format.fprintf ppf "[%i]: " i;
    if i = !frame then Format.fprintf ppf "(FRAME)";
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    Format.fprintf ppf "%a@." Value.print !stack.(i)
  done 

let ensure a i = 
  let n = Array.length !a in 
  if i = n then (
    let b = Array.create (max (n*2) i) Value.Absent in
    Array.blit !a 0 b 0 n;
    a := b
  )

let set a i x =
  ensure a i;
  !a.(i) <- x

let push x =
  set stack !sp x;
  incr sp

71
72
73
74
75
76
let pop () =
  decr sp;
  !stack.(!sp)

let get_global = ref (fun cu pos -> assert false)
let set_global = ref (fun cu pos -> assert false)
77
78
let get_external = ref (fun cu pos -> assert false)
let set_external = ref (fun cu pos -> assert false)
79

80
let get_slot cu pos = !get_global cu pos
81
let set_slot cu pos v = !set_global cu pos v
82

83
84
let get_builtin = ref (fun _ -> assert false)

85
86
87
88
let eval_var env = function
  | Env i -> env.(i)
  | Stack i -> !stack.(!frame + i) 
  | Dummy -> Value.Absent
89
  | Global i -> !stack.(i)
90
91
  | Ext (cu,pos) as x ->
      if pos < 0 then (Obj.magic cu : Value.t) else
92
	let v = !get_global cu pos in
93
94
95
96
	let x = Obj.repr x in
	Obj.set_field x 0 (Obj.repr v);
	Obj.set_field x 1 (Obj.repr (-1));
	v
97
98
99
100
101
102
103
  | External (cu,pos) as x ->
      if pos < 0 then (Obj.magic cu : Value.t) else
	let v = !get_external cu pos in
	let x = Obj.repr x in
	Obj.set_field x 0 (Obj.repr v);
	Obj.set_field x 1 (Obj.repr (-1));
	v
104
105
  | Builtin s ->
      !get_builtin s
106

107
108
let tag_op_resolved = Obj.tag (Obj.repr (OpResolved (Obj.repr 0, [])))

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
let rec eval env = function
  | Var x -> eval_var env x
  | Apply (false,e1,e2) -> 
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      eval_apply v1 v2
  | Apply (true,e1,e2) -> 
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      eval_apply_tail_rec v1 v2
  | Abstraction (slots,iface,body) -> eval_abstraction env slots iface body
  | Const c -> Value.const c
  | Pair (e1,e2) -> 
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      Value.Pair (v1,v2)
  | Xml (e1,e2,e3) -> 
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      let v3 = eval env e3 in
      Value.Xml (v1,v2,v3)
130
131
132
133
134
  | XmlNs (e1,e2,e3,ns) -> 
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      let v3 = eval env e3 in
      Value.XmlNs (v1,v2,v3,ns)
135
136
  | Record r -> 
      Value.Record (Imap.map (eval env) r)
137
  | String (i,j,s,q) -> Value.substring_utf8 i j s (eval env q)
138
139
140
141
142
143
144
145
  | Match (e,brs) -> eval_branches env brs (eval env e)

  | Map (arg,brs) -> eval_map env brs (eval env arg)
  | Xtrans (arg,brs) -> eval_xtrans env brs (eval env arg)
  | Try (arg,brs) -> eval_try env arg brs
  | Transform (arg,brs) -> eval_transform env brs (eval env arg) 
  | Dot (e, l) -> eval_dot l (eval env e)
  | RemoveField (e, l) -> eval_remove_field l (eval env e)
146
  | Validate (e, schema, name) -> eval_validate env e schema name
147
  | Ref (e,t) -> eval_ref env e t
148
  | Op (op,args) as e -> 
149
150
151
152
153
154
      let eval_fun = eval_op op in
      Obj.set_field (Obj.repr e) 0 (Obj.repr eval_fun);
      Obj.set_tag (Obj.repr e) tag_op_resolved;
      eval_fun (List.map (eval env) args)
  | OpResolved (f,args) ->
      (Obj.magic f) (List.map (eval env) args)
155
  | NsTable (ns,e) -> ns_table := ns; eval env e
156
  | Check (t0,e,t) -> eval_check env t0 e t
157

158
and eval_check env t0 e t =
159
  let v = eval env e in
160
  match Explain.explain t0 (Types.descr t) v with
161
162
    | None -> v
    | Some p -> 
163
	let p = Explain.simplify p in
164
	let s = print_to_string Explain.print p in
165
	raise (CDuceExn (string_latin1 s))
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

and eval_abstraction env slots iface body =
  let local_env = Array.map (eval_var env) slots in
  let a = Value.Abstraction2 (local_env,iface,body) in
  local_env.(0) <- a;
  a

and eval_apply f arg =
  match f with
    | Value.Abstraction2 (local_env,_,body) -> 
	let saved_frame = !frame and saved_sp = !sp in 
	frame := !sp;
	let v = eval_branches local_env body arg in
	frame := saved_frame;
	sp := saved_sp;
	v
    | Value.Abstraction (_,f) -> f arg
    | _  -> assert false

and eval_apply_tail_rec f arg =
  match f with
    | Value.Abstraction2 (local_env,_,body) -> 
	sp := !frame;
	eval_branches local_env body arg
    | Value.Abstraction (_,f) -> f arg
    | _  -> assert false



195
and eval_branches env brs arg =
196
197
198
199
200
  let (disp, rhs) = dispatcher brs in
  let (code, bindings) = Run_dispatch.run_dispatcher disp arg in
  match rhs.(code) with 
    | Patterns.Compile.Match (bind,e) ->
	let saved_sp = !sp in
201
202
	List.iter 
	  (fun (_,i) -> push (if (i == -1) then arg else bindings.(i)))
203
204
205
206
207
208
209
210
211
212
	  bind;
 	if brs.brs_tail 
	then eval env e 
	else
	  let v = eval env e in
	  sp := saved_sp;
	  v
    | Patterns.Compile.Fail -> Value.Absent

and eval_ref env e t=
213
  Value.mk_ref (Types.descr t) (eval env e)
214

215
216
217
218
and eval_validate env e uri name = 
(* TODO: compute the validator when loading the lambda code *)
  let validate = Typer.get_schema_validator uri name in
  try validate (eval env e)
219
220
  with Schema_common.XSI_validation_error msg ->
    failwith' ("Schema validation failure: " ^ msg)
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

and eval_try env arg brs =
  let saved_frame = !frame and saved_sp = !sp in
  try eval env arg
  with (CDuceExn v) as exn ->
    frame := saved_frame;
    sp := saved_sp;
    match eval_branches env brs v with
      | Value.Absent -> raise exn
      | x -> x

and eval_map env brs v =
  map (eval_map_aux env brs) v

and eval_map_aux env brs acc = function
  | Value.Pair (x,y) -> 
      let x = eval_branches env brs x in
      let acc' = Value.Pair (x, Absent) in
      set_cdr acc acc';
      eval_map_aux env brs acc' y
  | Value.String_latin1 (_,_,_,_) | Value.String_utf8 (_,_,_,_) as v -> 
      eval_map_aux env brs acc (normalize v)
243
244
245
  | Value.Concat (x,y) ->
      let acc = eval_map_aux env brs acc x in
      eval_map_aux env brs acc y
246
247
248
249
250
251
252
253
254
255
256
257
  | _ -> acc

and eval_transform env brs v =
  map (eval_transform_aux env brs) v

and eval_transform_aux env brs acc = function
  | Value.Pair (x,y) -> 
      (match eval_branches env brs x with 
	 | Value.Absent -> eval_transform_aux env brs acc y
	 | x -> eval_transform_aux env brs (append_cdr acc x) y)
  | Value.String_latin1 (_,_,_,q) | Value.String_utf8 (_,_,_,q) as v -> 
      if not brs.brs_accept_chars
258
      then eval_transform_aux env brs acc q
259
      else eval_transform_aux env brs acc (normalize v)
260
261
262
  | Value.Concat (x,y) ->
      let acc = eval_transform_aux env brs acc x in
      eval_transform_aux env brs acc y
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  | _ -> acc


and eval_xtrans env brs v =
  map (eval_xtrans_aux env brs) v

and eval_xtrans_aux env brs acc = function
  | Value.String_utf8 (s,i,j,q) as v ->
      if not brs.brs_accept_chars
      then 
	let acc' = Value.String_utf8 (s,i,j, Absent) in
	set_cdr acc acc';
	eval_xtrans_aux env brs acc' q
      else eval_xtrans_aux env brs acc (normalize v)
  | Value.String_latin1 (s,i,j,q) as v ->
      if not brs.brs_accept_chars
      then 
	let acc' = Value.String_latin1 (s,i,j, Absent) in
	set_cdr acc acc';
	eval_xtrans_aux env brs acc' q
      else eval_xtrans_aux env brs acc (normalize v)
284
285
286
  | Value.Concat (x,y) ->
      let acc = eval_xtrans_aux env brs acc x in
      eval_xtrans_aux env brs acc y
287
288
289
290
291
292
293
294
  | Value.Pair (x,y) -> 
      let acc = 
	match eval_branches env brs x with
	  | Value.Absent -> 
	      let x = match x with
		| Value.Xml (tag, attr, child) -> 
		    let child = eval_xtrans env brs child in
		    Value.Xml (tag, attr, child)
295
296
297
		| Value.XmlNs (tag, attr, child, ns) ->
		    let child = eval_xtrans env brs child in
		    Value.XmlNs (tag, attr, child, ns)
298
299
300
301
302
303
304
305
306
307
		| x -> x in
	      let acc' = Value.Pair (x, Absent) in
	      set_cdr acc acc';
	      acc'
	  | x -> append_cdr acc x
      in
      eval_xtrans_aux env brs acc y
  | _ -> acc

and eval_dot l = function
308
309
  | Value.Record r 
  | Value.Xml (_,Value.Record r,_)
310
  | Value.XmlNs (_,Value.Record r,_,_) -> Imap.find_lower r l
311
312
313
314
315
  | v -> 
      Value.print Format.std_formatter v;
      failwith ("Cannot find field " ^ (Label.to_string (LabelPool.value l)))

and eval_remove_field l = function
316
  | Value.Record r -> Value.Record (Imap.remove r l)
317
318
319
320
321
  | _ -> assert false




322
let eval_expr e = 
323
  assert (!frame = 0);
324
  ignore (eval [||] e)
325

326
let var v =
327
328
329
  assert (!frame = 0);
  eval_var [||] v

330
let eval_split p =
331
  assert (!frame = 0);
332

333
  let comp = make_branches (Types.descr (Patterns.accept p)) [ p, () ] in
334
335
336
337
338
  let (disp, bind) = 
    match comp with
      | (disp, [| Patterns.Compile.Match (l, ()) |]) -> (disp,l)
      | _ -> assert false in
  
339
  let v = pop () in
340
  let (_, bindings) = Run_dispatch.run_dispatcher disp v in
341
  List.iter (fun (_,i) -> push (if (i == -1) then v else bindings.(i))) bind
342
  
343
let protect_eval f x =
344
  assert (!frame = 0);
345
346
347
348
349
  let old_sp = !sp in
  try f x
  with exn -> frame := 0; sp := old_sp; raise exn

let expr =
350
  protect_eval (eval [||])
351
352
353
354

let eval = 
  protect_eval
    (function
355
356
357
358
       | Push e -> push (eval [||] e)
       | Pop -> ignore (pop ())
       | Split p -> eval_split p
       | SetGlobal (cu,i) -> !set_global cu i (pop ())
359
    )
360

361
362
let code_items = 
  protect_eval (List.iter eval)
363
364


365
366
367
368
369
370
371
372
373
let new_stack f x =
  let old_stack = !stack and old_frame = !frame and old_sp = !sp in
  stack := Array.create 1024 Value.Absent;
  frame := 0;
  sp := 0;
  let restore () = stack := old_stack; frame := old_frame; sp := old_sp in
  try let v = f x in  restore (); v
  with exn -> restore (); raise exn