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

6
7
8
let eval_unary_op = ref (fun _ -> assert false)
let eval_binary_op = ref (fun _ _ -> assert false)

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

13
(* To write tail-recursive map-like iteration *)
14

15
let make_accu () = Value.Pair(nil,Absent)
16
let get_accu a = snd (Obj.magic a)
17
let map f v = let acc0 = make_accu () in set_cdr (f acc0 v) nil; get_accu acc0
18

19
20
21
22
let dispatcher brs =
  match brs.brs_compiled with
    | Some d -> d
    | None ->
23
24
(*	Format.fprintf Format.std_formatter "Start compilation...@."; 
	let time = Unix.gettimeofday() in*)
25
	let x = Patterns.Compile.make_branches brs.brs_input brs.brs in
26
27
28
29
30
(*	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; *)
31
32
33
34
35
36
37
38
	brs.brs_compiled <- Some x;
	x


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

39

40
41
42
let dump ppf =
  Format.fprintf ppf "sp = %i   frame = %i@." !sp !frame;
  for i = 0 to !sp - 1 do
43
44
    Format.fprintf ppf "[%i]: " i;
    if i = !frame then Format.fprintf ppf "(FRAME)";
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    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

64
65
66
67
68
69
let pop () =
  decr sp;
  !stack.(!sp)

let get_global = ref (fun cu pos -> assert false)
let set_global = ref (fun cu pos -> assert false)
70
71
let get_external = ref (fun cu pos -> assert false)
let set_external = ref (fun cu pos -> assert false)
72

73
let get_slot cu pos = !get_global cu pos
74
let set_slot cu pos v = !set_global cu pos v
75

76
77
78
79
let eval_var env = function
  | Env i -> env.(i)
  | Stack i -> !stack.(!frame + i) 
  | Dummy -> Value.Absent
80
  | Global i -> !stack.(i)
81
82
  | Ext (cu,pos) as x ->
      if pos < 0 then (Obj.magic cu : Value.t) else
83
	let v = !get_global cu pos in
84
85
86
87
	let x = Obj.repr x in
	Obj.set_field x 0 (Obj.repr v);
	Obj.set_field x 1 (Obj.repr (-1));
	v
88
89
90
91
92
93
94
  | 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

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)
  | Record r -> Value.Record (LabelMap.map (eval env) r)
118
  | String (i,j,s,q) -> Value.substring_utf8 i j s (eval env q)
119
120
121
122
123
124
125
126
127
128
129
130
131
  | 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)
  | UnaryOp (op,e) -> !eval_unary_op op (eval env e)
  | BinaryOp (op,e1,e2) ->  
      let v1 = eval env e1 in
      let v2 = eval env e2 in
      !eval_binary_op op v1 v2
132
  | Validate (e, kind, schema, name) -> eval_validate env e kind schema name
133
  | Ref (e,t) -> eval_ref env e t
134
  | Op (op,args) -> eval_op op (List.map (eval env) args)
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

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



and eval_branches env brs arg =
  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
170
171
	List.iter 
	  (fun (_,i) -> push (if (i == -1) then arg else bindings.(i)))
172
173
174
175
176
177
178
179
180
181
	  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=
182
  Value.mk_ref (Types.descr t) (eval env e)
183

184
and eval_validate env e kind schema_name name = 
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  let schema = Typer.get_schema schema_name in
  try
    let validate =
      match Schema_common.get_component kind name schema with
      | Schema_types.Type x -> Schema_validator.validate_type x schema
      | Schema_types.Element x -> Schema_validator.validate_element x schema
      | Schema_types.Attribute x ->
          assert false  (* TODO see schema/schema_validator.mli *)
(*           Schema_validator.validate_attribute x schema *)
      | Schema_types.Attribute_group x ->
          Schema_validator.validate_attribute_group x schema
      | Schema_types.Model_group x ->
          Schema_validator.validate_model_group x schema
    in
    validate (eval env e)
  with Schema_common.XSI_validation_error msg ->
    failwith' ("Schema validation failure: " ^ msg)
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

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)
224
225
226
  | Value.Concat (x,y) ->
      let acc = eval_map_aux env brs acc x in
      eval_map_aux env brs acc y
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  | _ -> 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
      then eval_transform_aux env brs acc v
      else eval_transform_aux env brs acc (normalize v)
241
242
243
  | Value.Concat (x,y) ->
      let acc = eval_transform_aux env brs acc x in
      eval_transform_aux env brs acc y
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  | _ -> 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)
265
266
267
  | Value.Concat (x,y) ->
      let acc = eval_xtrans_aux env brs acc x in
      eval_xtrans_aux env brs acc y
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  | 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)
		| 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
  | Value.Record r -> LabelMap.assoc l r
  | v -> 
      Value.print Format.std_formatter v;
      failwith ("Cannot find field " ^ (Label.to_string (LabelPool.value l)))

and eval_remove_field l = function
  | Value.Record r -> Value.Record (LabelMap.remove l r)
  | _ -> assert false




298
let eval_expr e = 
299
  assert (!frame = 0);
300
  ignore (eval [||] e)
301

302
let var v =
303
304
305
  assert (!frame = 0);
  eval_var [||] v

306
let eval_split p =
307
  assert (!frame = 0);
308

309
  let comp = Patterns.Compile.make_branches 
310
	       (Types.descr (Patterns.accept p)) [ p, () ] in
311
312
313
314
315
  let (disp, bind) = 
    match comp with
      | (disp, [| Patterns.Compile.Match (l, ()) |]) -> (disp,l)
      | _ -> assert false in
  
316
  let v = pop () in
317
  let (_, bindings) = Run_dispatch.run_dispatcher disp v in
318
  List.iter (fun (_,i) -> push (if (i == -1) then v else bindings.(i))) bind
319
  
320
let protect_eval f x =
321
  assert (!frame = 0);
322
323
324
325
326
  let old_sp = !sp in
  try f x
  with exn -> frame := 0; sp := old_sp; raise exn

let expr =
327
  protect_eval (eval [||])
328
329
330
331

let eval = 
  protect_eval
    (function
332
333
334
335
       | Push e -> push (eval [||] e)
       | Pop -> ignore (pop ())
       | Split p -> eval_split p
       | SetGlobal (cu,i) -> !set_global cu i (pop ())
336
    )
337

338
339
let code_items = 
  protect_eval (List.iter eval)
340
341


342
343
344
345
346
347
348
349
350
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