let compile (globals:env_frame) (env:environment) (ast:ast) (next:bytecode) : bytecode =

let rec bc_compile env ast next =

- printf "bc-compile: %a~~\n%!"~~ pp_env env;

+ printf "bc-compile: %a in env: %a\n%!" pp_ast ast pp_env env;

A_lit l -> B_lit (l, next)

with _ -> Blub_closure.VarSet.empty

+ (* We will definitely compile the bytecode version of the function

+ now. If conditions are right, we might compile the native

+ code version here as well. *)

+ vals = Array.map (fun _ -> Sunbound) params } in

+ let env = frame :: env in

+ let code = bc_compile env body B_ret in

(* NOTE that this is a completely deferred compilation, which will

allow us to compile it in terms of the *live* environment rather

B_abs ( { lam_params = params;

- lam_body = None }, next)

+ lam_jitcode = PMap.empty }, next)

| A_app (fn, args) -> begin

let c = bc_compile env fn B_apply in

let _, n = Array.fold_left

let next = if acc == Sfalse then falseb else trueb in

eval acc globals env rib stack next

| B_abs (abs, next) -> begin

- let () = match abs.lam_body with

- (* This compiles the lambda abstraction in order to create the

- closure; I believe it would be possible to delay the

- compilation even longer, under the application of the

- function, if that was beneficial.

- { vars = abs.lam_params;

- vals = Array.map (fun _ -> Sunbound) abs.lam_params } in

- let env = frame :: env in

- let body = bc_compile env abs.lam_ast B_ret in

- abs.lam_body <- Some body*)

let closure = Sclosure (env, abs) in

eval closure globals env rib stack next

let abs = { lam_params = [| var |];

lam_ast = A_lit (Sunbound);

- lam_body = Some (Bytecode body) } in

+ lam_jitcode = PMap.empty } in

let acc = Sclosure ([], abs) in

eval acc globals env rib stack x

let { lam_params = params;

- lam_bod~~y~~ = body } = abs in

+ lam_bytecode = body } = abs in

let frame = if varargs then

(* one param (the last) is the varargs, the rest are the

{ vars = params; vals = rib }

let env = frame :: env in

- let body = match body with

- (* This is playing around with deferring compilation of a

- function until it is applied. However, we can't memoize

- it since it is compiled with values of the params

- bound in the environment *)

- let body = compile globals env abs.lam_ast B_ret in

- (* abs.lam_body <- Some body; *)

- | Some Bytecode body -> body

eval acc globals env rib stack body