36 lines
643 B
OCaml
36 lines
643 B
OCaml
module Value = Ast.Value
|
|
|
|
type t = {
|
|
vars : (string, Value.t) Hashtbl.t;
|
|
parent : t option;
|
|
}
|
|
|
|
let init_global () = {
|
|
vars = Hashtbl.create 100;
|
|
parent = None;
|
|
}
|
|
|
|
let make parent = {
|
|
vars = Hashtbl.create 100;
|
|
parent = Some parent;
|
|
}
|
|
|
|
exception Not_found
|
|
|
|
let rec get e name =
|
|
match Hashtbl.find_opt e.vars name with
|
|
| None -> begin match e.parent with
|
|
| None -> raise Not_found
|
|
| Some p -> get p name
|
|
end
|
|
| Some value -> value
|
|
|
|
let get_opt e name =
|
|
try Some (get e name)
|
|
with Not_found -> None
|
|
|
|
let set e name value =
|
|
Hashtbl.replace e.vars name value
|
|
|
|
let add_seq e seq =
|
|
Hashtbl.add_seq e.vars seq
|