shithub: martian9

Download patch

ref: b34fba0e5b9e39fc5120f06b6c335033472f81ca
parent: a5def38ade71a84c23154fa395375c5132be4cbf
author: smazga <[email protected]>
date: Thu Aug 6 09:13:23 EDT 2020

added env and more filling out

--- /dev/null
+++ b/env.ml
@@ -1,0 +1,36 @@
+module T = Types.Types
+module Data = Map.Make (String)
+
+type env =
+  { outer : env option
+  ; data : Types.m9type Data.t ref
+  }
+
+let make outer = { outer; data = ref Data.empty }
+
+let set env sym value =
+  match sym with
+  | T.Symbol { T.value = key } -> env.data := Data.add key value !(env.data)
+  | _ -> raise (Invalid_argument "set: not a symbol")
+;;
+
+let rec find env sym =
+  match sym with
+  | T.Symbol { T.value = key } ->
+    if Data.mem key !(env.data)
+    then Some env
+    else (
+      match env.outer with
+      | Some outer -> find outer sym
+      | None -> None)
+  | _ -> raise (Invalid_argument "find: not a symbol")
+;;
+
+let get env sym =
+  match sym with
+  | T.Symbol { T.value = key } ->
+    (match find env sym with
+    | Some found_env -> Data.find key !(found_env.data)
+    | None -> raise (Invalid_argument ("'" ^ key ^ "' not found")))
+  | _ -> raise (Invalid_argument "get: not a symbol")
+;;