env cons-list instead of tree structure

This commit is contained in:
tali 2023-12-13 18:22:55 -05:00
parent 752d36d855
commit c986aa6ec0
1 changed files with 19 additions and 13 deletions

View File

@ -64,27 +64,33 @@ and lambda = {
module Env = struct
type t =
| Empty
| Obj of {
pred : t;
self : id;
elems : (string, string) Hashtbl.t;
}
| Fun of {
pred : t;
args : (string * id) list;
(* clos : ??? *)
}
| Cons of t * t
let rec find name = function
| Fun { args } ->
(* TODO: closure conversion *)
List.assoc name args, None
| Empty ->
raise Not_found
| Obj { self; elems } ->
self, Some (Hashtbl.find elems name)
| Fun { pred; args } ->
begin match List.assoc name args with
| id -> id, None
| exception Not_found -> find name pred
end
| Cons (e1, e2) ->
try find name e1 with
Not_found -> find name e2
| Obj { pred; self; elems } ->
begin match Hashtbl.find elems name with
| elem -> self, Some elem
| exception Not_found -> find name pred
end
end
let seq_r a b = Seq (b, a)
@ -179,8 +185,8 @@ let lower ~lib (modl : Ast.modl) =
compile_error "scope does not end in expression";
let self = new_id "obj" in
let env' = Env.Obj { self; elems } in
let env = Env.Cons (env', env) in
let env = Env.Obj { self; elems; pred = env } in
let env' = Env.Obj { self; elems; pred = Empty } in
let funs_r, vals_r, inits_r =
List.fold_left
@ -228,7 +234,7 @@ let lower ~lib (modl : Ast.modl) =
and lower_lambda self env args body =
let args = List.map (fun a -> a, new_id a) args in
let env = Env.Cons (Fun { args }, env) in
let env = Env.Fun { args; pred = env } in
(* TODO: closure conversion *)
let body = lower_exp env body in
let args = List.map snd args in
@ -239,7 +245,7 @@ let lower ~lib (modl : Ast.modl) =
let self = new_id "lib" in
let elems = Hashtbl.create 128 in
List.iter (fun (name, _) -> Hashtbl.add elems name name) lib;
let env = Env.Obj { self; elems } in
let env = Env.Obj { self; elems; pred = Empty } in
let args = [] in
let body = lower_block env modl.items in
{ self; args; body }