Commit | Line | Data |
---|---|---|
efb850b5 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 | ||
17 | let 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 | |
34 | and eval ast env = | |
35 | match ast with | |
127b36c1 | 36 | | T.List { T.value = [] } -> ast |
efb850b5 C |
37 | | T.List { T.value = [(T.Symbol { T.value = "def!" }); key; expr] } -> |
38 | let value = (eval expr env) in | |
39 | Env.set env key value; value | |
40 | | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.Vector { T.value = bindings }); body] } | |
41 | | T.List { T.value = [(T.Symbol { T.value = "let*" }); (T.List { T.value = bindings }); body] } -> | |
42 | (let sub_env = Env.make (Some env) in | |
43 | let rec bind_pairs = (function | |
44 | | sym :: expr :: more -> | |
45 | Env.set sub_env sym (eval expr sub_env); | |
46 | bind_pairs more | |
47 | | _::[] -> raise (Invalid_argument "let* bindings must be an even number of forms") | |
48 | | [] -> ()) | |
49 | in bind_pairs bindings; | |
50 | eval body sub_env) | |
51 | | T.List { T.value = ((T.Symbol { T.value = "do" }) :: body) } -> | |
52 | List.fold_left (fun x expr -> eval expr env) T.Nil body | |
53 | | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr; else_expr] } -> | |
54 | if Types.to_bool (eval test env) then (eval then_expr env) else (eval else_expr env) | |
55 | | T.List { T.value = [T.Symbol { T.value = "if" }; test; then_expr] } -> | |
56 | if Types.to_bool (eval test env) then (eval then_expr env) else T.Nil | |
57 | | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.Vector { T.value = arg_names }; expr] } | |
58 | | T.List { T.value = [T.Symbol { T.value = "fn*" }; T.List { T.value = arg_names }; expr] } -> | |
fb21afa7 | 59 | Types.fn |
efb850b5 C |
60 | (function args -> |
61 | let sub_env = Env.make (Some env) in | |
62 | let rec bind_args a b = | |
63 | (match a, b with | |
64 | | [T.Symbol { T.value = "&" }; name], args -> Env.set sub_env name (Types.list args); | |
65 | | (name :: names), (arg :: args) -> | |
66 | Env.set sub_env name arg; | |
67 | bind_args names args; | |
68 | | [], [] -> () | |
69 | | _ -> raise (Invalid_argument "Bad param count in fn call")) | |
70 | in bind_args arg_names args; | |
71 | eval expr sub_env) | |
72 | | T.List { T.value = [T.Symbol { T.value = "quote" }; ast] } -> ast | |
73 | | T.List { T.value = [T.Symbol { T.value = "quasiquote" }; ast] } -> | |
74 | eval (quasiquote ast) env | |
75 | | T.List _ -> | |
76 | (match eval_ast ast env with | |
ecd3b6d8 | 77 | | T.List { T.value = ((T.Fn { T.value = f }) :: args) } -> f args |
efb850b5 C |
78 | | _ -> raise (Invalid_argument "Cannot invoke non-function")) |
79 | | _ -> eval_ast ast env | |
80 | ||
81 | let read str = Reader.read_str str | |
82 | let print exp = Printer.pr_str exp true | |
83 | let rep str env = print (eval (read str) env) | |
84 | ||
85 | let rec main = | |
86 | try | |
87 | Core.init Core.ns; | |
88 | Env.set repl_env (Types.symbol "*ARGV*") | |
89 | (Types.list (if Array.length Sys.argv > 1 | |
90 | then (List.map (fun x -> T.String x) (List.tl (List.tl (Array.to_list Sys.argv)))) | |
91 | else [])); | |
92 | Env.set repl_env (Types.symbol "eval") | |
fb21afa7 | 93 | (Types.fn (function [ast] -> eval ast repl_env | _ -> T.Nil)); |
e6d41de4 | 94 | ignore (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" repl_env); |
efb850b5 C |
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 -> () |