compile.ml 11.5 KB
Newer Older
1
2
3
4
open Ident
open Lambda

type env = {
5
  cu: Compunit.t option;  (* None: toplevel *)
6
  vars: var_loc Env.t;
7
  sigma : sigma; (* symbolic substitutions (Lambda.sigma) *)
8
  gamma : Types.Node.t IdMap.map; (* map of type variables to types *)
9
  stack_size: int;
10
  max_stack: int ref;
11
  global_size: int
12
13
}

14
15
let global_size env = env.global_size

16
17
18
19
let mk cu = { 
  cu = cu; 
  vars = Env.empty;
  sigma = `List [];
20
  gamma = IdMap.empty;
21
22
23
24
  stack_size = 0; 
  max_stack = ref 0; 
  global_size = 0 
}
25
26
let empty_toplevel = mk None
let empty x = mk (Some x)
27

28
29
30
31
32
let find x env =
  try Env.find x env.vars
  with Not_found -> 
    failwith ("Compile: cannot find " ^ (Ident.to_string x))

33
34
let find_slot x env =
  match find x env with
35
    | Ext (_,slot) -> slot
36
37
    | _ -> assert false
 
38
39
40
41
let from_comp_unit = ref (fun cu -> assert false)

let find_ext cu x =
  let env = !from_comp_unit cu in
42
43
  find x env

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
let enter_local env x =
  let new_size = env.stack_size + 1 in
  if new_size > !(env.max_stack) then (env.max_stack) := new_size;
  { env with 
      vars = Env.add x (Local env.stack_size) env.vars;
      stack_size = new_size }

let enter_global_toplevel env x =
  { env with 
      vars = Env.add x (Global env.global_size) env.vars;
      global_size = env.global_size + 1 }

let enter_global_cu cu env x =
  { env with 
      vars = Env.add x (Ext (cu,env.global_size)) env.vars;
      global_size = env.global_size + 1 }

61
62
63
64
65
let rec domain = function 
  |`List l -> Types.Tallying.domain l
  |`Comp (s1,s2) -> Var.Set.union (domain s1) (domain s2)
  |`Sel(x,t,s) -> (domain s)

66
(* from intermediate explicitely typed language to Evaluation language (lambda) *)
67
68
69
70
71
72
let rec compile env e = compile_aux env e.Typed.exp_descr
and compile_aux env = function
  | Typed.Forget (e,_) -> compile env e
  | Typed.Check (t0,e,t) -> 
      let d = Patterns.Compile.make_checker !t0 (Types.descr t) in
      Check (compile env e, d)
73
  | Typed.Var x -> Var (find x env)
74
75
  | Typed.TVar x ->
      let v = find x env in
Pietro Abate's avatar
Pietro Abate committed
76
77
78
79
80
      let polyvars = 
        Var.Set.inter 
        (domain(env.sigma)) 
        (Types.all_vars(Types.descr (IdMap.assoc x env.gamma))) 
      in
81
      if Var.Set.is_empty polyvars then Var (v)
82
83
      else TVar(v,env.sigma)
  | Typed.Subst(e,sl) -> compile { env with sigma = `Comp(env.sigma,`List sl) } e
84
  | Typed.ExtVar (cu,x,_) -> Var (find_ext cu x)
85
  | Typed.Apply (e1,e2) -> Apply (compile env e1, compile env e2)
86
  | Typed.Abstraction a -> compile_abstr env a
87
88
  | Typed.Cst c -> Const (Value.const c)
  | Typed.Pair (e1,e2) -> Pair(compile env e1, compile env e2)
89
  | Typed.Xml (e1, { Typed.exp_descr = Typed.Pair (e2,e3) }, None) -> 
90
      Xml (compile env e1, compile env e2, compile env e3)
91
  | Typed.Xml (e1, { Typed.exp_descr = Typed.Pair (e2,e3) }, Some t) -> 
92
      XmlNs (compile env e1, compile env e2, compile env e3,t)
93
  | Typed.Xml _ -> assert false
94
  | Typed.RecordLitt r -> 
95
96
      let r = List.map (fun (l,e) -> (Upool.int l, compile env e)) 
	(LabelMap.get r)
97
98
      in
      Record (Imap.create (Array.of_list r))
99
100
101
102
103
104
105
106
107
108
  | Typed.String (i,j,s,q) -> String (i,j,s,compile env q)
  | Typed.Match (e,brs) -> Match (compile env e, compile_branches env brs)
  | Typed.Map (e,brs) -> Map (compile env e, compile_branches env brs)
  | Typed.Transform (e,brs) -> Transform (compile env e, compile_branches env brs)
  | Typed.Xtrans (e,brs) -> Xtrans (compile env e, compile_branches env brs)
  | Typed.Validate (e,_,validator) -> Validate (compile env e, validator)
  | Typed.RemoveField (e,l) -> RemoveField (compile env e,l)
  | Typed.Dot (e,l) -> Dot (compile env e, l)
  | Typed.Try (e,brs) -> Try (compile env e, compile_branches env brs)
  | Typed.Ref (e,t) ->  Ref (compile env e, t)
109
  | Typed.External (t,`Ext i) -> 
110
      (match env.cu with
111
	 | Some cu -> Var (External (cu,i))
112
	 | None -> failwith "Cannot compile externals in the toplevel")
113
114
  | Typed.External (t,`Builtin s) -> 
      Var (Builtin s)
115
  | Typed.Op (op,_,args) -> 
116
      let rec aux = function
117
118
	| [arg] -> [ compile env arg ]
	| arg::l -> (compile env arg) :: (aux l)
119
120
	| [] -> [] in
      Op (op, aux args)
121
  | Typed.NsTable (ns,e) ->
122
      NsTable (ns, compile_aux env e)
123
124

and compile_abstr env a =
125
  let fun_env, fun_name = 
126
    match a.Typed.fun_name with
127
128
      | Some x -> Env.add x (Env 0) Env.empty, [x, Types.cons a.Typed.fun_typ]
      | None -> Env.empty, [] in
129

130
131
132
  let (slots,nb_slots,fun_env) = 
    List.fold_left 
      (fun (slots,nb_slots,fun_env) x ->
133
	 match find x env with
134
	   | (Local _ | Env _) as p -> 
135
136
137
	       p::slots,
	       succ nb_slots,
	       Env.add x (Env nb_slots) fun_env;
138
	   | Global _ | Ext _ | External _ | Builtin _ as p -> 
139
140
141
142
143
	       slots,
	       nb_slots,
	       Env.add x p fun_env
	   | Dummy -> assert false
      )
144
      ([Dummy],1,fun_env) (IdSet.get a.Typed.fun_fv) in
145
146
147


  let slots = Array.of_list (List.rev slots) in  
148
  let env = { env with vars = fun_env; gamma = IdMap.merge (fun _ v2 -> v2) env.gamma fun_name;
149
    stack_size = 0; max_stack = ref 0 } in
150
  let body = compile_branches env a.Typed.fun_body in
151
  let sigma = `Sel(a.Typed.fun_fv,a.Typed.fun_iface,env.sigma) in
152
153
154
155
156
157
158
159
160
161
162
  let polyvars =
    let vs =
      List.fold_left (fun acc (t1,t2) ->
        let ts1 = Types.all_vars t1 in
        let ts2 = Types.all_vars t2 in
        (Var.Set.union acc (Var.Set.union ts1 ts2))
      ) Var.Set.empty a.Typed.fun_iface
    in
    Var.Set.inter (domain(env.sigma)) vs 
  in
  if Var.Set.is_empty polyvars then
163
164
165
    Abstraction (slots, a.Typed.fun_iface, body, !(env.max_stack), false, sigma)
  else 
    Abstraction (slots, a.Typed.fun_iface, body, !(env.max_stack), true, sigma)
166

167
and compile_branches env (brs : Typed.branches) =
168
  (* Don't compile unused branches, because they have not been type checked. *)
169
  let used = List.filter (fun br -> br.Typed.br_used) brs.Typed.br_branches in
170
  let b = List.map (compile_branch env) used in
171
172
173
  (* here I need to pull type information from each pattern and then
   * compute for each variable gamma(x) . I should be able to compute gamma(x) 
   * using the information computed in (disp,rhs) *)
174
175
  let (disp,rhs) = Patterns.Compile.make_branches brs.Typed.br_typ b in
  { brs_stack_pos = env.stack_size;
176
    brs_accept_chars = not (Types.Char.is_empty brs.Typed.br_accept);
177
    brs_disp = disp;
178
179
    brs_rhs = rhs 
  }
180

181
182
(* p_i / t_i -> br.Typed.br_pat / br.Typed.br_type 
 * p_i / t_i is used here to add elements to env.gamma *)
183
and compile_branch env br =
184
  (* We add a fresh variable "pat<nb>:x" for each pattern *)
185
  let t, fv, d = br.Typed.br_pat.Patterns.descr in
186
187
  let fv, d = incr Patterns.counter;
    let freshname = "pat" ^ (string_of_int !Patterns.counter) ^ ":x" in
188
189
    let fv = fv @ [!Patterns.counter, freshname] in
    fv, Patterns.Cap((Types.any, fv, Patterns.Capture(!Patterns.counter, freshname)), (t, fv, d))
190
  in
191
192
193
  let pat = { br.Typed.br_pat with Patterns.descr=(t,fv,d); Patterns.fv=fv } in
  let env = List.fold_left enter_local env fv in
  let m = Patterns.filter (Types.descr (Patterns.accept pat)) pat in
194
  let env = { env with gamma = IdMap.union_disj m env.gamma } in
195
  (pat, compile env br.Typed.br_body )
196
197
198
199
200
201
202
203
204

let enter_globals env n =  match env.cu with
  | None -> List.fold_left enter_global_toplevel env n
  | Some cu -> List.fold_left (enter_global_cu cu) env n

let compile_expr env e =
  let env = { env with max_stack = ref 0; stack_size = 0 } in
  let e = compile env e in
  (e,!(env.max_stack))
205

206
207
let compile_let_decl env decl =
  let pat = decl.Typed.let_pat in
208
209
210
  let e,lsize = compile_expr env decl.Typed.let_body in
  let env = enter_globals env (Patterns.fv pat) in

211
  let te = decl.Typed.let_body.Typed.exp_typ in 
212
  let comp = Patterns.Compile.make_branches te [ pat, () ] in
213
214
215
216
217
  let (disp, n) = 
    match comp with
      | (disp, [| Auto_pat.Match (n, ()) |]) -> (disp,n)
      | _ -> assert false in
  (env, [ LetDecls (e,lsize,disp,n) ])
218
219
220
221
222

let compile_rec_funs env funs =
  let fun_name = function
    | { Typed.exp_descr=Typed.Abstraction{Typed.fun_name = Some x}} -> x
    | _ -> assert false in
223
224
225
226
227
228
  let fun_a env e =
    let e,lsize = compile_expr env e in
    LetDecl (e,lsize) in
  let env = enter_globals env (List.map fun_name funs) in
  let code= List.map (fun_a env) funs in
  (env, code)
229
230
231
232


(****************************************)

233
open Cduce_loc
234

235
236
let eval ~run ~show (tenv,cenv,codes) e =
  let (e,t) = Typer.type_expr tenv e in
237
  let e,lsize = compile_expr cenv e in
238
  if run then
239
    let v = Eval.expr e lsize in
240
241
242
    show None t (Some v)
  else
    show None t None;
243
  (tenv,cenv, Eval (e,lsize) :: codes)
244

245
let run_show ~run ~show tenv cenv codes ids =
246
  if run then
247
    let () = Eval.eval_toplevel codes in
248
249
250
    List.iter 
      (fun (id,_) -> show (Some id) 
	 (Typer.find_value id tenv)
251
	 (Some (Eval.eval_var (find id cenv)))) ids
252
253
254
255
256
  else
    List.iter 
      (fun (id,_) -> show (Some id) 
	 (Typer.find_value id tenv)
	 None) ids
257
  
258
259
let let_decl ~run ~show (tenv,cenv,codes) p e =
  let (tenv,decl,ids) = Typer.type_let_decl tenv p e in
260
  let (cenv,code) = compile_let_decl cenv decl in
261
  run_show ~run ~show tenv cenv code ids;
262
  (tenv,cenv,List.rev_append code codes)
263
  
264
265
let let_funs ~run ~show (tenv,cenv,codes) funs =
  let (tenv,funs,ids) = Typer.type_let_funs tenv funs in
266
  let (cenv,code) = compile_rec_funs cenv funs in
267
  run_show ~run ~show tenv cenv code ids;
268
  (tenv,cenv,List.rev_append code codes)
269
270
  
let type_defs (tenv,cenv,codes) typs =
271
  let tenv = Typer.type_defs tenv typs in
272
273
  (tenv,cenv,codes)

274
275
let namespace (tenv,cenv,codes) loc pr ns =
  let tenv = Typer.type_ns tenv loc pr ns in
276
277
  (tenv,cenv,codes)

278
279
280
281
let keep_ns (tenv,cenv,codes) k =
  let tenv = Typer.type_keep_ns tenv k in
  (tenv,cenv,codes)

282
283
let schema (tenv,cenv,codes) loc x sch =
  let tenv = Typer.type_schema tenv loc x sch in
284
285
  (tenv,cenv,codes)

286
287
let using (tenv,cenv,codes) loc x cu =
  let tenv = Typer.type_using tenv loc x cu in
288
289
  (tenv,cenv,codes)

290
291
292
293
let do_open (tenv,cenv,codes) loc path =
  let tenv = Typer.type_open tenv loc path in
  (tenv,cenv,codes)

294
295
let rec collect_funs accu = function
  | { descr = Ast.FunDecl e } :: rest -> collect_funs (e::accu) rest
296
  | rest -> (List.rev accu,rest)
297
298

let rec collect_types accu = function
299
300
  | { descr = Ast.TypeDecl ((loc,x),t) } :: rest -> 
      collect_types ((loc,x,t) :: accu) rest
301
302
  | rest -> (accu,rest)

303
let rec phrases ~run ~show ~directive =
304
305
306
307
308
309
310
311
  let rec loop accu phs =
    match phs with
      | { descr = Ast.FunDecl _ } :: _ -> 
	  let (funs,rest) = collect_funs [] phs in
	  loop (let_funs ~run ~show accu funs) rest
      | { descr = Ast.TypeDecl (_,_) } :: _ ->
	  let (typs,rest) = collect_types [] phs in
	  loop (type_defs accu typs) rest
312
313
314
315
      | { descr = Ast.SchemaDecl (name, uri); loc = loc } :: rest ->
	  loop (schema accu loc name uri) rest
      | { descr = Ast.Namespace (pr,ns); loc = loc } :: rest ->
	  loop (namespace accu loc pr ns) rest
316
317
      | { descr = Ast.KeepNs b } :: rest ->
	  loop (keep_ns accu b) rest
318
319
      | { descr = Ast.Using (x,cu); loc = loc } :: rest ->
	  loop (using accu loc x cu) rest
320
321
      | { descr = Ast.Open path; loc = loc } :: rest ->
	  loop (do_open accu loc path) rest
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
      | { descr = Ast.EvalStatement e } :: rest ->
	  loop (eval ~run ~show accu e) rest
      | { descr = Ast.LetDecl (p,e) } :: rest ->
	  loop (let_decl ~run ~show accu p e) rest
      | { descr = Ast.Directive d } :: rest ->
	  let (tenv,cenv,_) = accu in
	  directive tenv cenv d;
	  loop accu rest
      | [] -> 
	  accu
  in
  loop

let comp_unit ?(run=false) 
  ?(show=fun _ _ _ -> ()) 
  ?(directive=fun _ _ _ -> ())  tenv cenv phs =
338
  let (tenv,cenv,codes) = phrases ~run ~show ~directive (tenv,cenv,[]) phs in
339
  (tenv,cenv,List.rev codes)
340
341
342
343
344


let compile_eval_expr env e =
  let e,lsize = compile_expr env e in
  Eval.expr e lsize