mirror of
https://github.com/kanaka/mal.git
synced 2024-10-27 14:52:16 +03:00
033892777a
See issue #587. * Merge eval-ast and eval into a single conditional. * Expand macros during the apply phase, removing lots of duplicate tests, and increasing the overall consistency by allowing the macro to be computed instead of referenced by name (`((defmacro! cond (...)))` is currently illegal for example). * Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the MAL environment. * Remove macroexpand and quasiquoteexpand special forms. * Use pattern-matching style in process/step*.txt. Unresolved issues: c.2: unable to reproduce with gcc 11.12.0. elm: the directory is unchanged. groovy: sometimes fail, but not on each rebuild. nasm: fails some new soft tests, but the issue is unreproducible when running the interpreter manually. objpascal: unreproducible with fpc 3.2.2. ocaml: unreproducible with 4.11.1. perl6: unreproducible with rakudo 2021.09. Unrelated changes: Reduce diff betweens steps. Prevent defmacro! from mutating functions: c forth logo miniMAL vb. dart: fix recent errors and warnings ocaml: remove metadata from symbols. Improve the logo implementation. Encapsulate all representation in types.lg and env.lg, unwrap numbers. Replace some manual iterations with logo control structures. Reduce the diff between steps. Use native iteration in env_get and env_map Rewrite the reader with less temporary strings. Reduce the number of temporary lists (for example, reverse iteration with butlast requires O(n^2) allocations). It seems possible to remove a few exceptions: GC settings (Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES (Makefile.impls) .
78 lines
2.8 KiB
OCaml
78 lines
2.8 KiB
OCaml
module T = Types.Types
|
|
|
|
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"))
|
|
|
|
let repl_env = Env.make None
|
|
|
|
let init_repl env = begin
|
|
Env.set env "+" (num_fun ( + ));
|
|
Env.set env "-" (num_fun ( - ));
|
|
Env.set env "*" (num_fun ( * ));
|
|
Env.set env "/" (num_fun ( / ));
|
|
end
|
|
|
|
let rec eval ast env =
|
|
(match Env.get env "DEBUG-EVAL" with
|
|
| None -> ()
|
|
| Some T.Nil -> ()
|
|
| Some (T.Bool false) -> ()
|
|
| Some _ ->
|
|
output_string stderr ("EVAL: " ^ (Printer.pr_str ast true) ^ "\n");
|
|
flush stderr);
|
|
match ast with
|
|
| T.Symbol s -> (match Env.get env s with
|
|
| Some v -> v
|
|
| None -> raise (Invalid_argument ("'" ^ s ^ "' not found")))
|
|
| 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 k (eval v env) m)
|
|
xs
|
|
Types.MalMap.empty)}
|
|
| T.List { T.value = [T.Symbol "def!"; T.Symbol key; expr] } ->
|
|
let value = (eval expr env) in
|
|
Env.set env key value; value
|
|
| T.List { T.value = [T.Symbol "let*"; (T.Vector { T.value = bindings }); body] }
|
|
| T.List { T.value = [T.Symbol "let*"; (T.List { T.value = bindings }); body] } ->
|
|
(let sub_env = Env.make (Some env) in
|
|
let rec bind_pairs = (function
|
|
| T.Symbol sym :: expr :: more ->
|
|
Env.set sub_env sym (eval expr sub_env);
|
|
bind_pairs more
|
|
| _ :: _ :: _ -> raise (Invalid_argument "let* keys must be symbols")
|
|
| _::[] -> raise (Invalid_argument "let* bindings must be an even number of forms")
|
|
| [] -> ())
|
|
in bind_pairs bindings;
|
|
eval body sub_env)
|
|
| T.List { T.value = (a0 :: args) } ->
|
|
(match eval a0 env with
|
|
| T.Fn { T.value = f } -> f (List.map (fun x -> eval x env) args)
|
|
| _ -> raise (Invalid_argument "Cannot invoke non-function"))
|
|
| _ -> ast
|
|
|
|
let read str = Reader.read_str str
|
|
let print exp = Printer.pr_str exp true
|
|
let rep str env = print (eval (read str) env)
|
|
|
|
let rec main =
|
|
try
|
|
init_repl repl_env;
|
|
while true do
|
|
print_string "user> ";
|
|
let line = read_line () in
|
|
try
|
|
print_endline (rep line repl_env);
|
|
with End_of_file -> ()
|
|
| Invalid_argument x ->
|
|
output_string stderr ("Invalid_argument exception: " ^ x ^ "\n");
|
|
flush stderr
|
|
done
|
|
with End_of_file -> ()
|