aboutsummaryrefslogtreecommitdiff
path: root/ocaml/env.ml
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-01-30 12:06:45 -0600
committerJoel Martin <github@martintribe.org>2015-01-30 12:06:45 -0600
commit644e5ff95e186094054221cf2f13048630f81fa0 (patch)
tree14f965eee633f4ea185f640c6130e54063056d6b /ocaml/env.ml
parentbf518367d0706b2fa727acc5326230ef8d3c812b (diff)
parentbc6448bce925d18ecb893e8e8ee10d2b2178b31a (diff)
downloadmal-644e5ff95e186094054221cf2f13048630f81fa0.tar.gz
mal-644e5ff95e186094054221cf2f13048630f81fa0.zip
Merge pull request #3 from Chouser/ocaml
Ocaml
Diffstat (limited to 'ocaml/env.ml')
-rw-r--r--ocaml/env.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/ocaml/env.ml b/ocaml/env.ml
new file mode 100644
index 0000000..cb32360
--- /dev/null
+++ b/ocaml/env.ml
@@ -0,0 +1,33 @@
+module T = Types.Types
+module Data = Map.Make (String)
+
+type env = {
+ outer : env option;
+ data : Types.mal_type Data.t ref;
+}
+
+let make outer = { 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 requires a Symbol for its key")
+
+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 requires a Symbol for its key")
+
+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 requires a Symbol for its key")