eval.ml 9.26 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
(* To write tail-recursive map-like iteration *)
10

11
let make_accu () = Value.Pair(nil,Absent)
12
let get_accu a = snd (Obj.magic a)
13
let map f v = let acc0 = make_accu () in set_cdr (f acc0 v) nil; get_accu acc0
14

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


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

35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
let dump ppf =
  Format.fprintf ppf "sp = %i   frame = %i@." !sp !frame;
  for i = 0 to !sp - 1 do
    if i = !frame then Format.fprintf ppf "FRAME@.";
    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

59
60
let from_comp_unit = ref (fun cu pos -> assert false)

61
62
63
64
65
66
67
68
let eval_var env = function
  | Env i -> env.(i)
  | Global i -> !stack.(i)
  | Stack i -> !stack.(!frame + i) 
  | Dummy -> Value.Absent

let rec eval env = function
  | Var x -> eval_var env x
69
  | ExtVar (cu,pos) -> !from_comp_unit cu pos
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
  | 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)
  | String (i,j,s,q) -> Value.String_utf8 (i,j,s,eval env q)
  | 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
104
  | Validate (e, kind, schema, name) -> eval_validate env e kind schema name
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
  | Ref (e,t) -> eval_ref env e t

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
141
142
	List.iter 
	  (fun (_,i) -> push (if (i == -1) then arg else bindings.(i)))
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	  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=
  let r = ref (eval env e) in
  let get = 
    Value.Abstraction ([Sequence.nil_type, Types.descr t], fun _ -> !r)
  and set = 
    Value.Abstraction 
      ([Types.descr t, Sequence.nil_type], fun x -> r := x; nil) in
  Value.Record (Builtin_defs.mk_ref ~get ~set)

161
and eval_validate env e kind schema_name name = 
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  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)
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

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)
201
202
203
  | Value.Concat (x,y) ->
      let acc = eval_map_aux env brs acc x in
      eval_map_aux env brs acc y
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  | _ -> 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)
218
219
220
  | Value.Concat (x,y) ->
      let acc = eval_transform_aux env brs acc x in
      eval_transform_aux env brs acc y
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  | _ -> 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)
242
243
244
  | Value.Concat (x,y) ->
      let acc = eval_xtrans_aux env brs acc x in
      eval_xtrans_aux env brs acc y
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  | 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




275
let eval_expr e = 
276
  assert (!frame = 0);
277
  ignore (eval [||] e)
278

279
let var v =
280
281
282
  assert (!frame = 0);
  eval_var [||] v

283
let eval_let_decl p e =
284
  assert (!frame = 0);
285

286
  let comp = Patterns.Compile.make_branches 
287
	       (Types.descr (Patterns.accept p)) [ p, () ] in
288
289
290
291
292
  let (disp, bind) = 
    match comp with
      | (disp, [| Patterns.Compile.Match (l, ()) |]) -> (disp,l)
      | _ -> assert false in
  
293
  let v = eval [||] e in
294
  let (_, bindings) = Run_dispatch.run_dispatcher disp v in
295
  List.iter (fun (_,i) -> push (if (i == -1) then v else bindings.(i))) bind
296
297
  
let eval_rec_funs funs =
298
299
300
  assert (!frame = 0);
  List.iter (fun e -> push (eval [||] e)) funs

301
302
303
304
305
let expr e =
  assert (!frame = 0);
  match e with
    | Eval e -> eval [||] e
    | _-> assert false
306
307
308
309
310

let eval = function
  | Eval e -> eval_expr e
  | Let_decl (p,e) -> eval_let_decl p e
  | Let_funs funs -> eval_rec_funs funs
311

312

313
let comp_unit init code =
314
(* Save the stack so as to be able to run a loaded comp_unit
315
316
317
318
319
320
321
322
323
324
325
326
   from the toplevel *)
  let old_stack = !stack in
  let old_sp = !sp in
  let restore () = stack := old_stack; sp := old_sp in
  stack := Array.create 1024 Value.Absent;
  sp := 0;
  try
    List.iter push init;
    List.iter eval code;
    let r = Array.sub !stack 0 !sp in 
    restore (); r
  with exn -> restore (); raise exn