Merge pull request #118 from dubek/ocaml-fix-hash-equality
[jackhill/mal.git] / ocaml / step7_quote.ml
CommitLineData
efb850b5
C
1module T = Types.Types
2
3let repl_env = Env.make (Some Core.ns)
4
5let rec quasiquote ast =
6 match ast with
7 | T.List { T.value = [T.Symbol {T.value = "unquote"}; ast] } -> ast
8 | T.Vector { T.value = [T.Symbol {T.value = "unquote"}; ast] } -> ast
9 | T.List { T.value = T.List { T.value = [T.Symbol {T.value = "splice-unquote"}; head]} :: tail }
10 | T.Vector { T.value = T.List { T.value = [T.Symbol {T.value = "splice-unquote"}; head]} :: tail } ->
11 Types.list [Types.symbol "concat"; head; quasiquote (Types.list tail)]
12 | T.List { T.value = head :: tail }
13 | T.Vector { T.value = head :: tail } ->
14 Types.list [Types.symbol "cons"; quasiquote head; quasiquote (Types.list tail) ]
15 | ast -> Types.list [Types.symbol "quote"; ast]
16
17let rec eval_ast ast env =
18 match ast with
19 | T.Symbol s -> Env.get env ast
20 | T.List { T.value = xs; T.meta = meta }
ecd3b6d8 21 -> T.List { T.value = (List.map (fun x -> eval x env) xs);
2b8e0ea4 22 T.meta = meta }
efb850b5 23 | T.Vector { T.value = xs; T.meta = meta }
ecd3b6d8 24 -> T.Vector { T.value = (List.map (fun x -> eval x env) xs);
2b8e0ea4 25 T.meta = meta }
efb850b5
C
26 | T.Map { T.value = xs; T.meta = meta }
27 -> T.Map {T.meta = meta;
28 T.value = (Types.MalMap.fold
29 (fun k v m
30 -> Types.MalMap.add (eval k env) (eval v env) m)
31 xs
32 Types.MalMap.empty)}
33 | _ -> ast
34and eval ast env =
35 match ast with
36 | T.List { T.value = [(T.Symbol { T.value = "def!" }); key; expr] } ->
37 let value = (eval expr env) in
38 Env.set env key value; value
39 | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.Vector { T.value = bindings }); body] }
40 | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.List { T.value = bindings }); body] } ->
41 (let sub_env = Env.make (Some env) in
42 let rec bind_pairs = (function
43 | sym :: expr :: more ->
44 Env.set sub_env sym (eval expr sub_env);
45 bind_pairs more
46 | _::[] -> raise (Invalid_argument "let* bindings must be an even number of forms")
47 | [] -> ())
48 in bind_pairs bindings;
49 eval body sub_env)
50 | T.List { T.value = ((T.Symbol { T.value = "do" }) :: body) } ->
51 List.fold_left (fun x expr -> eval expr env) T.Nil body
52 | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr; else_expr] } ->
53 if Types.to_bool (eval test env) then (eval then_expr env) else (eval else_expr env)
54 | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr] } ->
55 if Types.to_bool (eval test env) then (eval then_expr env) else T.Nil
56 | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.Vector { T.value = arg_names }; expr] }
57 | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.List { T.value = arg_names }; expr] } ->
fb21afa7 58 Types.fn
efb850b5
C
59 (function args ->
60 let sub_env = Env.make (Some env) in
61 let rec bind_args a b =
62 (match a, b with
63 | [T.Symbol { T.value = "&" }; name], args -> Env.set sub_env name (Types.list args);
64 | (name :: names), (arg :: args) ->
65 Env.set sub_env name arg;
66 bind_args names args;
67 | [], [] -> ()
68 | _ -> raise (Invalid_argument "Bad param count in fn call"))
69 in bind_args arg_names args;
70 eval expr sub_env)
71 | T.List { T.value = [T.Symbol { T.value = "quote" }; ast] } -> ast
72 | T.List { T.value = [T.Symbol { T.value = "quasiquote" }; ast] } ->
73 eval (quasiquote ast) env
74 | T.List _ ->
75 (match eval_ast ast env with
ecd3b6d8 76 | T.List { T.value = ((T.Fn { T.value = f }) :: args) } -> f args
efb850b5
C
77 | _ -> raise (Invalid_argument "Cannot invoke non-function"))
78 | _ -> eval_ast ast env
79
80let read str = Reader.read_str str
81let print exp = Printer.pr_str exp true
82let rep str env = print (eval (read str) env)
83
84let rec main =
85 try
86 Core.init Core.ns;
87 Env.set repl_env (Types.symbol "*ARGV*")
88 (Types.list (if Array.length Sys.argv > 1
89 then (List.map (fun x -> T.String x) (List.tl (List.tl (Array.to_list Sys.argv))))
90 else []));
91 Env.set repl_env (Types.symbol "eval")
fb21afa7 92 (Types.fn (function [ast] -> eval ast repl_env | _ -> T.Nil));
efb850b5
C
93 let code = "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
94 in ignore (rep code repl_env);
95 ignore (rep "(def! not (fn* (a) (if a false true)))" repl_env);
96
97 if Array.length Sys.argv > 1 then
98 ignore (rep ("(load-file \"" ^ Sys.argv.(1) ^ "\")") repl_env)
99 else
100 while true do
101 print_string "user> ";
102 let line = read_line () in
103 try
104 print_endline (rep line repl_env);
105 with End_of_file -> ()
106 | Invalid_argument x ->
107 output_string stderr ("Invalid_argument exception: " ^ x ^ "\n");
108 flush stderr
109 done
110 with End_of_file -> ()