From 67736cf90b4f977b4b3ca3801e079040fc9fc0c9 Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 23 Jan 2015 08:17:35 -0500 Subject: Ocaml: Add step 3 --- ocaml/env.ml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 ocaml/env.ml (limited to 'ocaml/env.ml') diff --git a/ocaml/env.ml b/ocaml/env.ml new file mode 100644 index 0000000..d4388ad --- /dev/null +++ b/ocaml/env.ml @@ -0,0 +1,33 @@ +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 + | Types.Symbol 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 + | Types.Symbol 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 + | Types.Symbol key -> + (match find env sym with + | Some found_env -> Data.find key !(found_env.data) + | None -> raise (Invalid_argument ("Symbol '" ^ key ^ "' not found"))) + | _ -> raise (Invalid_argument "get requires a Symbol for its key") + -- cgit v1.2.3 From a878f3bb778513c0cc8bbeb1a8ff61664e43de29 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sun, 25 Jan 2015 23:30:37 -0500 Subject: Ocaml: Use a real map type T.Map is now a real OCaml binary-tree map, and supports arbitrary mal value types for both keys and values. Metadata support is provided in the data objects, but not yet in the printer, reader, or core library. --- ocaml/env.ml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'ocaml/env.ml') diff --git a/ocaml/env.ml b/ocaml/env.ml index d4388ad..97f9cc8 100644 --- a/ocaml/env.ml +++ b/ocaml/env.ml @@ -1,3 +1,4 @@ +module T = Types.Types module Data = Map.Make (String) type env = { @@ -9,12 +10,12 @@ let make outer = { outer = outer; data = ref Data.empty } let set env sym value = match sym with - | Types.Symbol key -> env.data := Data.add key value !(env.data) + | 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 - | Types.Symbol key -> + | T.Symbol { T.value = key } -> (if Data.mem key !(env.data) then Some env else @@ -25,9 +26,8 @@ let rec find env sym = let get env sym = match sym with - | Types.Symbol key -> + | T.Symbol { T.value = key } -> (match find env sym with | Some found_env -> Data.find key !(found_env.data) | None -> raise (Invalid_argument ("Symbol '" ^ key ^ "' not found"))) | _ -> raise (Invalid_argument "get requires a Symbol for its key") - -- cgit v1.2.3 From ecd3b6d8e551dd87934142b0323d9b75134bbea9 Mon Sep 17 00:00:00 2001 From: Chouser Date: Thu, 29 Jan 2015 23:29:54 -0500 Subject: OCaml: Add step 9 --- ocaml/env.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ocaml/env.ml') diff --git a/ocaml/env.ml b/ocaml/env.ml index 97f9cc8..cb32360 100644 --- a/ocaml/env.ml +++ b/ocaml/env.ml @@ -29,5 +29,5 @@ let get env sym = | T.Symbol { T.value = key } -> (match find env sym with | Some found_env -> Data.find key !(found_env.data) - | None -> raise (Invalid_argument ("Symbol '" ^ key ^ "' not found"))) + | None -> raise (Invalid_argument ("'" ^ key ^ "' not found"))) | _ -> raise (Invalid_argument "get requires a Symbol for its key") -- cgit v1.2.3