Commit | Line | Data |
---|---|---|
fd3adc52 C |
1 | module T = Types.Types |
2 | ||
3 | let repl_env = Env.make (Some Core.ns) | |
4 | ||
5 | let 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 | ||
2b8e0ea4 | 17 | let is_macro_call ast env = |
fd3adc52 C |
18 | match ast with |
19 | | T.List { T.value = s :: args } -> | |
20 | (match (try Env.get env s with _ -> T.Nil) with | |
2b8e0ea4 | 21 | | T.Fn { T.meta = T.Map { T.value = meta } } |
d90be1a9 | 22 | -> Types.MalMap.mem Core.kw_macro meta && Types.to_bool (Types.MalMap.find Core.kw_macro meta) |
2b8e0ea4 C |
23 | | _ -> false) |
24 | | _ -> false | |
25 | ||
26 | let rec macroexpand ast env = | |
27 | if is_macro_call ast env | |
28 | then match ast with | |
29 | | T.List { T.value = s :: args } -> | |
30 | (match (try Env.get env s with _ -> T.Nil) with | |
31 | | T.Fn { T.value = f } -> macroexpand (f args) env | |
32 | | _ -> ast) | |
33 | | _ -> ast | |
34 | else ast | |
fd3adc52 C |
35 | |
36 | let rec eval_ast ast env = | |
37 | match ast with | |
38 | | T.Symbol s -> Env.get env ast | |
39 | | T.List { T.value = xs; T.meta = meta } | |
40 | -> T.List { T.value = (List.map (fun x -> eval x env) xs); | |
2b8e0ea4 | 41 | T.meta = meta } |
fd3adc52 C |
42 | | T.Vector { T.value = xs; T.meta = meta } |
43 | -> T.Vector { T.value = (List.map (fun x -> eval x env) xs); | |
2b8e0ea4 | 44 | T.meta = meta } |
fd3adc52 C |
45 | | T.Map { T.value = xs; T.meta = meta } |
46 | -> T.Map {T.meta = meta; | |
fd3adc52 C |
47 | T.value = (Types.MalMap.fold |
48 | (fun k v m | |
49 | -> Types.MalMap.add (eval k env) (eval v env) m) | |
50 | xs | |
51 | Types.MalMap.empty)} | |
52 | | _ -> ast | |
53 | and eval ast env = | |
54 | match macroexpand ast env with | |
127b36c1 | 55 | | T.List { T.value = [] } -> ast |
fd3adc52 C |
56 | | T.List { T.value = [(T.Symbol { T.value = "def!" }); key; expr] } -> |
57 | let value = (eval expr env) in | |
58 | Env.set env key value; value | |
59 | | T.List { T.value = [(T.Symbol { T.value = "defmacro!" }); key; expr] } -> | |
60 | (match (eval expr env) with | |
61 | | T.Fn { T.value = f; T.meta = meta } -> | |
d90be1a9 | 62 | let fn = T.Fn { T.value = f; meta = Core.assoc [meta; Core.kw_macro; (T.Bool true)]} |
2b8e0ea4 | 63 | in Env.set env key fn; fn |
fd3adc52 C |
64 | | _ -> raise (Invalid_argument "devmacro! value must be a fn")) |
65 | | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.Vector { T.value = bindings }); body] } | |
66 | | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.List { T.value = bindings }); body] } -> | |
67 | (let sub_env = Env.make (Some env) in | |
68 | let rec bind_pairs = (function | |
69 | | sym :: expr :: more -> | |
70 | Env.set sub_env sym (eval expr sub_env); | |
71 | bind_pairs more | |
72 | | _::[] -> raise (Invalid_argument "let* bindings must be an even number of forms") | |
73 | | [] -> ()) | |
74 | in bind_pairs bindings; | |
75 | eval body sub_env) | |
76 | | T.List { T.value = ((T.Symbol { T.value = "do" }) :: body) } -> | |
77 | List.fold_left (fun x expr -> eval expr env) T.Nil body | |
78 | | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr; else_expr] } -> | |
79 | if Types.to_bool (eval test env) then (eval then_expr env) else (eval else_expr env) | |
80 | | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr] } -> | |
81 | if Types.to_bool (eval test env) then (eval then_expr env) else T.Nil | |
82 | | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.Vector { T.value = arg_names }; expr] } | |
83 | | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.List { T.value = arg_names }; expr] } -> | |
84 | Types.fn | |
85 | (function args -> | |
86 | let sub_env = Env.make (Some env) in | |
87 | let rec bind_args a b = | |
88 | (match a, b with | |
89 | | [T.Symbol { T.value = "&" }; name], args -> Env.set sub_env name (Types.list args); | |
90 | | (name :: names), (arg :: args) -> | |
91 | Env.set sub_env name arg; | |
92 | bind_args names args; | |
93 | | [], [] -> () | |
94 | | _ -> raise (Invalid_argument "Bad param count in fn call")) | |
95 | in bind_args arg_names args; | |
96 | eval expr sub_env) | |
97 | | T.List { T.value = [T.Symbol { T.value = "quote" }; ast] } -> ast | |
98 | | T.List { T.value = [T.Symbol { T.value = "quasiquote" }; ast] } -> | |
99 | eval (quasiquote ast) env | |
100 | | T.List { T.value = [T.Symbol { T.value = "macroexpand" }; ast] } -> | |
101 | macroexpand ast env | |
dd7a4f55 JM |
102 | | T.List { T.value = [T.Symbol { T.value = "try*" }; scary]} -> |
103 | (eval scary env) | |
fd3adc52 C |
104 | | T.List { T.value = [T.Symbol { T.value = "try*" }; scary ; |
105 | T.List { T.value = [T.Symbol { T.value = "catch*" }; | |
106 | local ; handler]}]} -> | |
107 | (try (eval scary env) | |
108 | with exn -> | |
109 | let value = match exn with | |
110 | | Types.MalExn value -> value | |
111 | | Invalid_argument msg -> T.String msg | |
dd7a4f55 | 112 | | e -> (T.String (Printexc.to_string e)) in |
fd3adc52 C |
113 | let sub_env = Env.make (Some env) in |
114 | Env.set sub_env local value; | |
115 | eval handler sub_env) | |
116 | | T.List _ as ast -> | |
117 | (match eval_ast ast env with | |
118 | | T.List { T.value = ((T.Fn { T.value = f }) :: args) } -> f args | |
119 | | _ -> raise (Invalid_argument "Cannot invoke non-function")) | |
120 | | ast -> eval_ast ast env | |
121 | ||
122 | let read str = Reader.read_str str | |
123 | let print exp = Printer.pr_str exp true | |
124 | let rep str env = print (eval (read str) env) | |
125 | ||
126 | let rec main = | |
127 | try | |
128 | Core.init Core.ns; | |
129 | Env.set repl_env (Types.symbol "*ARGV*") | |
130 | (Types.list (if Array.length Sys.argv > 1 | |
131 | then (List.map (fun x -> T.String x) (List.tl (List.tl (Array.to_list Sys.argv)))) | |
132 | else [])); | |
133 | Env.set repl_env (Types.symbol "eval") | |
134 | (Types.fn (function [ast] -> eval ast repl_env | _ -> T.Nil)); | |
135 | ||
136 | ignore (rep "(def! *host-language* \"ocaml\")" repl_env); | |
137 | ignore (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" repl_env); | |
138 | ignore (rep "(def! not (fn* (a) (if a false true)))" repl_env); | |
139 | ignore (rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))" repl_env); | |
fd3adc52 C |
140 | |
141 | if Array.length Sys.argv > 1 then | |
19950b9f DM |
142 | try |
143 | ignore (rep ("(load-file \"" ^ Sys.argv.(1) ^ "\")") repl_env); | |
144 | with | |
145 | | Types.MalExn exc -> | |
146 | output_string stderr ("Exception: " ^ (print exc) ^ "\n"); | |
147 | flush stderr | |
1ac37587 C |
148 | else begin |
149 | ignore (rep "(println (str \"Mal [\" *host-language* \"]\"))" repl_env); | |
150 | while true do | |
151 | print_string "user> "; | |
152 | let line = read_line () in | |
153 | try | |
154 | print_endline (rep line repl_env); | |
155 | with End_of_file -> () | |
19950b9f DM |
156 | | Types.MalExn exc -> |
157 | output_string stderr ("Exception: " ^ (print exc) ^ "\n"); | |
158 | flush stderr | |
1ac37587 C |
159 | | Invalid_argument x -> |
160 | output_string stderr ("Invalid_argument exception: " ^ x ^ "\n"); | |
161 | flush stderr | |
dd7a4f55 JM |
162 | | e -> |
163 | output_string stderr ((Printexc.to_string e) ^ "\n"); | |
1ac37587 C |
164 | flush stderr |
165 | done | |
166 | end | |
fd3adc52 | 167 | with End_of_file -> () |