From 921a951fe4d088e60ce25866344bd534420f9ec6 Mon Sep 17 00:00:00 2001 From: Chouser Date: Thu, 22 Jan 2015 02:59:48 -0500 Subject: Ocaml: Add step 2, nothing optional --- ocaml/step2_eval.ml | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ocaml/step2_eval.ml (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml new file mode 100644 index 0000000..4337a11 --- /dev/null +++ b/ocaml/step2_eval.ml @@ -0,0 +1,50 @@ +module Env = + Map.Make ( + String + (*(struct + type t = Types.Symbol + let compare (Types.Symbol a) (Types.Symbol b) = compare a b + end)*) + ) + +let num_fun f = Types.Fn + (function + | [(Types.Int a); (Types.Int b)] -> Types.Int (f a b) + | _ -> raise (Invalid_argument "Numeric args required for this Mal builtin")) + +let env = ref (List.fold_left (fun a b -> b a) Env.empty + [ Env.add "+" (num_fun ( + )); + Env.add "-" (num_fun ( - )); + Env.add "*" (num_fun ( * )); + Env.add "/" (num_fun ( / )) ]) + +let rec eval_ast ast env = + match ast with + | Types.Symbol s -> + (try Env.find s !env + with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) + | Types.MalList xs -> Types.MalList (List.map (fun x -> eval x env) xs) + | _ -> ast +and eval ast env = + let result = eval_ast ast env in + match result with + | Types.MalList ((Types.Fn f) :: args) -> (f args) + | _ -> result + +let read str = Reader.read_str str +let print exp = Printer.pr_str exp +let rep str env = print (eval (read str) env) + +let rec main = + try + while true do + print_string "user> "; + let line = read_line () in + try + print_endline (rep line env); + with End_of_file -> () + | Invalid_argument x -> + output_string stderr ("Invalid_argument exception: " ^ x ^ "\n"); + flush stderr + done + with End_of_file -> () -- cgit v1.2.3 From 81e073cf2044d0e3cfbcc03a81dcba605a945fe5 Mon Sep 17 00:00:00 2001 From: Chouser Date: Thu, 22 Jan 2015 15:55:22 -0500 Subject: Ocaml: made minor fixes, mostly to Makefile --- ocaml/step2_eval.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index 4337a11..02866eb 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -12,7 +12,7 @@ let num_fun f = Types.Fn | [(Types.Int a); (Types.Int b)] -> Types.Int (f a b) | _ -> raise (Invalid_argument "Numeric args required for this Mal builtin")) -let env = ref (List.fold_left (fun a b -> b a) Env.empty +let repl_env = ref (List.fold_left (fun a b -> b a) Env.empty [ Env.add "+" (num_fun ( + )); Env.add "-" (num_fun ( - )); Env.add "*" (num_fun ( * )); @@ -41,7 +41,7 @@ let rec main = print_string "user> "; let line = read_line () in try - print_endline (rep line env); + print_endline (rep line repl_env); with End_of_file -> () | Invalid_argument x -> output_string stderr ("Invalid_argument exception: " ^ x ^ "\n"); -- cgit v1.2.3 From de04357cd5f2954e2d682abb97ca2b3b90ea75d1 Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 23 Jan 2015 20:05:03 -0500 Subject: Ocaml: Add string functions --- ocaml/step2_eval.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index 02866eb..af8667c 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -32,7 +32,7 @@ and eval ast env = | _ -> result let read str = Reader.read_str str -let print exp = Printer.pr_str exp +let print exp = Printer.pr_str exp true let rep str env = print (eval (read str) env) let rec main = -- cgit v1.2.3 From f2f11f6279e1b242ba75136cd037fabdd176118a Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 23 Jan 2015 22:54:15 -0500 Subject: Ocaml: rename Types.MalList to Types.List --- ocaml/step2_eval.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index af8667c..d5ec9a3 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -23,12 +23,12 @@ let rec eval_ast ast env = | Types.Symbol s -> (try Env.find s !env with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) - | Types.MalList xs -> Types.MalList (List.map (fun x -> eval x env) xs) + | Types.List xs -> Types.List (List.map (fun x -> eval x env) xs) | _ -> ast and eval ast env = let result = eval_ast ast env in match result with - | Types.MalList ((Types.Fn f) :: args) -> (f args) + | Types.List ((Types.Fn f) :: args) -> (f args) | _ -> result let read str = Reader.read_str str -- 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/step2_eval.ml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index d5ec9a3..7be4a3e 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -1,3 +1,5 @@ +module T = Types.Types + module Env = Map.Make ( String @@ -7,9 +9,9 @@ module Env = end)*) ) -let num_fun f = Types.Fn +let num_fun f = T.Fn (function - | [(Types.Int a); (Types.Int b)] -> Types.Int (f a b) + | [(T.Int a); (T.Int b)] -> T.Int (f a b) | _ -> raise (Invalid_argument "Numeric args required for this Mal builtin")) let repl_env = ref (List.fold_left (fun a b -> b a) Env.empty @@ -20,15 +22,15 @@ let repl_env = ref (List.fold_left (fun a b -> b a) Env.empty let rec eval_ast ast env = match ast with - | Types.Symbol s -> + | T.Symbol { T.value = s } -> (try Env.find s !env with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) - | Types.List xs -> Types.List (List.map (fun x -> eval x env) xs) + | T.List { T.value = xs } -> Types.list (List.map (fun x -> eval x env) xs) | _ -> ast and eval ast env = let result = eval_ast ast env in match result with - | Types.List ((Types.Fn f) :: args) -> (f args) + | T.List { T.value = ((T.Fn f) :: args) } -> (f args) | _ -> result let read str = Reader.read_str str -- cgit v1.2.3 From 04e33074cc516fe4b79a6319c7a211002902a846 Mon Sep 17 00:00:00 2001 From: Chouser Date: Mon, 26 Jan 2015 23:05:13 -0500 Subject: Ocaml: All optional tests passing up thru step 4 --- ocaml/step2_eval.ml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index 7be4a3e..50751f9 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -23,9 +23,19 @@ let repl_env = ref (List.fold_left (fun a b -> b a) Env.empty let rec eval_ast ast env = match ast with | T.Symbol { T.value = s } -> - (try Env.find s !env - with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) - | T.List { T.value = xs } -> Types.list (List.map (fun x -> eval x env) xs) + (try Env.find s !env + with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) + | T.List { T.value = xs; T.meta = meta } + -> T.List { T.value = (List.map (fun x -> eval x env) xs); T.meta = meta } + | T.Vector { T.value = xs; T.meta = meta } + -> T.Vector { T.value = (List.map (fun x -> eval x env) xs); T.meta = meta } + | T.Map { T.value = xs; T.meta = meta } + -> T.Map {T.meta = meta; + T.value = (Types.MalMap.fold + (fun k v m + -> Types.MalMap.add (eval k env) (eval v env) m) + xs + Types.MalMap.empty)} | _ -> ast and eval ast env = let result = eval_ast ast env in -- cgit v1.2.3 From fb21afa71b4f73fa9c05c47e6b1c0f45d2144069 Mon Sep 17 00:00:00 2001 From: Chouser Date: Wed, 29 Jan 2014 20:05:05 -0500 Subject: OCaml: Add Step 8 --- ocaml/step2_eval.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index 50751f9..62de875 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -9,7 +9,7 @@ module Env = end)*) ) -let num_fun f = T.Fn +let num_fun f = Types.fn (function | [(T.Int a); (T.Int b)] -> T.Int (f a b) | _ -> raise (Invalid_argument "Numeric args required for this Mal builtin")) @@ -40,7 +40,7 @@ let rec eval_ast ast env = and eval ast env = let result = eval_ast ast env in match result with - | T.List { T.value = ((T.Fn f) :: args) } -> (f args) + | T.List { T.value = ((T.Fn { T.f = f }) :: args) } -> (f args) | _ -> result let read str = Reader.read_str str -- 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/step2_eval.ml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index 62de875..b7f0793 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -26,11 +26,16 @@ let rec eval_ast ast env = (try Env.find s !env with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) | T.List { T.value = xs; T.meta = meta } - -> T.List { T.value = (List.map (fun x -> eval x env) xs); T.meta = meta } + -> T.List { T.value = (List.map (fun x -> eval x env) xs); + T.meta = meta; + T.is_macro = false} | T.Vector { T.value = xs; T.meta = meta } - -> T.Vector { T.value = (List.map (fun x -> eval x env) xs); T.meta = meta } + -> T.Vector { T.value = (List.map (fun x -> eval x env) xs); + T.meta = meta; + T.is_macro = false} | T.Map { T.value = xs; T.meta = meta } -> T.Map {T.meta = meta; + T.is_macro = false; T.value = (Types.MalMap.fold (fun k v m -> Types.MalMap.add (eval k env) (eval v env) m) @@ -40,7 +45,7 @@ let rec eval_ast ast env = and eval ast env = let result = eval_ast ast env in match result with - | T.List { T.value = ((T.Fn { T.f = f }) :: args) } -> (f args) + | T.List { T.value = ((T.Fn { T.value = f }) :: args) } -> (f args) | _ -> result let read str = Reader.read_str str -- cgit v1.2.3 From 2b8e0ea42046505635e8f4c9d96e594c595948c2 Mon Sep 17 00:00:00 2001 From: Chouser Date: Fri, 30 Jan 2015 09:10:24 -0500 Subject: OCaml: put macro flag in metadata rather than special type field --- ocaml/step2_eval.ml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'ocaml/step2_eval.ml') diff --git a/ocaml/step2_eval.ml b/ocaml/step2_eval.ml index b7f0793..3778292 100644 --- a/ocaml/step2_eval.ml +++ b/ocaml/step2_eval.ml @@ -27,15 +27,12 @@ let rec eval_ast ast env = with Not_found -> raise (Invalid_argument ("Symbol '" ^ s ^ "' not found"))) | T.List { T.value = xs; T.meta = meta } -> T.List { T.value = (List.map (fun x -> eval x env) xs); - T.meta = meta; - T.is_macro = false} + T.meta = meta } | T.Vector { T.value = xs; T.meta = meta } -> T.Vector { T.value = (List.map (fun x -> eval x env) xs); - T.meta = meta; - T.is_macro = false} + T.meta = meta } | T.Map { T.value = xs; T.meta = meta } -> T.Map {T.meta = meta; - T.is_macro = false; T.value = (Types.MalMap.fold (fun k v m -> Types.MalMap.add (eval k env) (eval v env) m) -- cgit v1.2.3