2015-01-26 07:30:37 +03:00
|
|
|
module T = Types.Types
|
2015-01-24 02:11:45 +03:00
|
|
|
let ns = Env.make None
|
|
|
|
|
2015-01-26 07:30:37 +03:00
|
|
|
let num_fun t f = T.Fn
|
2015-01-24 02:11:45 +03:00
|
|
|
(function
|
2015-01-26 07:30:37 +03:00
|
|
|
| [(T.Int a); (T.Int b)] -> t (f a b)
|
2015-01-24 02:11:45 +03:00
|
|
|
| _ -> raise (Invalid_argument "Numeric args required for this Mal builtin"))
|
|
|
|
|
2015-01-26 07:30:37 +03:00
|
|
|
let mk_int x = T.Int x
|
|
|
|
let mk_bool x = T.Bool x
|
2015-01-24 02:11:45 +03:00
|
|
|
|
|
|
|
let init env = begin
|
2015-01-26 07:30:37 +03:00
|
|
|
Env.set env (Types.symbol "+") (num_fun mk_int ( + ));
|
|
|
|
Env.set env (Types.symbol "-") (num_fun mk_int ( - ));
|
|
|
|
Env.set env (Types.symbol "*") (num_fun mk_int ( * ));
|
|
|
|
Env.set env (Types.symbol "/") (num_fun mk_int ( / ));
|
|
|
|
Env.set env (Types.symbol "<") (num_fun mk_bool ( < ));
|
|
|
|
Env.set env (Types.symbol "<=") (num_fun mk_bool ( <= ));
|
|
|
|
Env.set env (Types.symbol ">") (num_fun mk_bool ( > ));
|
|
|
|
Env.set env (Types.symbol ">=") (num_fun mk_bool ( >= ));
|
2015-01-24 02:11:45 +03:00
|
|
|
|
2015-01-26 07:30:37 +03:00
|
|
|
Env.set env (Types.symbol "list") (T.Fn (function xs -> Types.list xs));
|
|
|
|
Env.set env (Types.symbol "list?")
|
|
|
|
(T.Fn (function [T.List _] -> T.Bool true | _ -> T.Bool false));
|
|
|
|
Env.set env (Types.symbol "empty?")
|
|
|
|
(T.Fn (function [T.List {T.value = []}] -> T.Bool true | _ -> T.Bool false));
|
|
|
|
Env.set env (Types.symbol "count")
|
|
|
|
(T.Fn (function [T.List {T.value = xs}] -> T.Int (List.length xs) | _ -> T.Int 0));
|
|
|
|
Env.set env (Types.symbol "=")
|
2015-01-27 07:05:13 +03:00
|
|
|
(T.Fn (function
|
|
|
|
| [T.List a; T.Vector b] -> T.Bool (a = b)
|
|
|
|
| [T.Vector a; T.List b] -> T.Bool (a = b)
|
|
|
|
| [a; b] -> T.Bool (a = b)
|
|
|
|
| _ -> T.Bool false));
|
2015-01-24 02:11:45 +03:00
|
|
|
|
2015-01-26 07:30:37 +03:00
|
|
|
Env.set env (Types.symbol "pr-str")
|
|
|
|
(T.Fn (function xs ->
|
2015-01-28 16:24:52 +03:00
|
|
|
T.String (String.concat " " (List.map (fun s -> Printer.pr_str s true) xs))));
|
2015-01-26 07:30:37 +03:00
|
|
|
Env.set env (Types.symbol "str")
|
|
|
|
(T.Fn (function xs ->
|
2015-01-28 16:24:52 +03:00
|
|
|
T.String (String.concat "" (List.map (fun s -> Printer.pr_str s false) xs))));
|
2015-01-26 07:30:37 +03:00
|
|
|
Env.set env (Types.symbol "prn")
|
|
|
|
(T.Fn (function xs ->
|
2015-01-28 16:24:52 +03:00
|
|
|
print_endline (String.concat " " (List.map (fun s -> Printer.pr_str s true) xs));
|
2015-01-26 07:30:37 +03:00
|
|
|
T.Nil));
|
|
|
|
Env.set env (Types.symbol "println")
|
|
|
|
(T.Fn (function xs ->
|
2015-01-28 16:24:52 +03:00
|
|
|
print_endline (String.concat " " (List.map (fun s -> Printer.pr_str s false) xs));
|
2015-01-26 07:30:37 +03:00
|
|
|
T.Nil));
|
|
|
|
|
|
|
|
Env.set env (Types.symbol "compare")
|
2015-01-27 03:16:23 +03:00
|
|
|
(T.Fn (function [a; b] -> T.Int (compare a b) | _ -> T.Nil));
|
|
|
|
Env.set env (Types.symbol "with-meta")
|
|
|
|
(T.Fn (function [a; b] -> Reader.with_meta a b | _ -> T.Nil));
|
|
|
|
Env.set env (Types.symbol "meta")
|
|
|
|
(T.Fn (function [x] -> Printer.meta x | _ -> T.Nil));
|
2015-01-24 02:11:45 +03:00
|
|
|
end
|
|
|
|
|