ref: 0a55d7957a05211ff86d530d12fafa02e8a11abf
parent: a3f06577db37f23d89755dd66e1e6f6b803037d2
author: smazga <[email protected]>
date: Wed Sep 30 18:07:52 EDT 2020
macro
--- a/eval.ml
+++ b/eval.ml
@@ -13,9 +13,27 @@
;;
let rec eval_ast ast env =
+ (* print_endline ("EVAL_AST: " ^ Printer.print ast true); *)
match ast with
| T.Symbol s -> Env.get env ast
- | T.List { T.value = xs; T.meta } -> T.List { T.value = List.map (fun x -> eval x env) xs; T.meta }
+ (* | T.Symbol s -> let foo = Env.get env ast in(\* (match Env.get env ast with *\)
+ * print_endline ("EVAL_AST: " ^ Printer.print foo true);
+ * (match foo with
+ * | T.Macro { T.value = sym; meta } -> raise (Reader.Syntax_error ("EVAL_AST MACRO: ast: " ^ Printer.print ast true))
+ * | T.List { T.value = xs; meta } -> raise (Reader.Syntax_error "EVAL_AST LIST")
+ * | _ as x -> print_endline ("EVAL_AST UNKNOWN: " ^ Printer.print ast true ^ ":" ^ Printer.print x true); foo)
+ *)
+ | T.List { T.value = xs; T.meta } -> (match
+ try Env.get env (List.hd xs) with
+ | _ -> T.Nil
+ with
+ | T.Macro { T.value = sym; meta } -> print_endline (" EVAL_AST: the rest: " ^ Printer.dump (List.tl xs));
+ let foo = Macro.expand ast env (List.tl xs) sym meta in
+ print_endline (" expanded: " ^ Printer.print foo true);
+ eval foo env
+ (* raise (Reader.Syntax_error ("EVAL_AST MACRO: ast: " ^ Printer.print ast true)) *)
+ | _ -> T.List { T.value = List.map (fun x -> eval x env) xs; T.meta })
+ (* | T.List { T.value = xs; T.meta } -> T.List { T.value = List.map (fun x -> eval x env) xs; T.meta } *)
| T.Vector { T.value = xs; T.meta } -> T.Vector { T.value = List.map (fun x -> eval x env) xs; T.meta }
| _ -> ast
@@ -33,6 +51,7 @@
* eval foo env
* | _ -> ast)
* | _ -> ast *)
+
and eval ast env =
match ast with
| T.List { T.value = [] } -> ast
@@ -95,11 +114,11 @@
| T.List _ ->
(match eval_ast ast env with
| T.List { T.value = T.Proc { T.value = f } :: args } -> f args
- | T.List { T.value = T.Macro { T.value = sym; meta } :: args } ->
- (* eval (Macro.expand ast env args sym meta) env *)
- let foo = Macro.expand ast env args sym meta in
- print_endline (":::: " ^ Printer.print foo true);
- eval foo env
+ (* | T.List { T.value = T.Macro { T.value = sym; meta } :: args } ->
+ * (\* eval (Macro.expand ast env args sym meta) env *\)
+ * let foo = Macro.expand ast env args sym meta in
+ * print_endline (":::: " ^ Printer.print foo true);
+ * eval foo env *)
| _ as x -> raise (Reader.Syntax_error ("'" ^ Printer.print x true ^ "' not a function")))
| _ -> eval_ast ast env
;;
--- a/reader.ml
+++ b/reader.ml
@@ -102,7 +102,7 @@
| _ -> read_form tokens)
and read_form all_tokens =
- print_endline ("READ_FORM: " ^ String.concat " " all_tokens);
+ (* print_endline ("READ_FORM: " ^ String.concat " " all_tokens); *)
match all_tokens with
| [] -> raise End_of_file
| token :: tokens ->
@@ -111,16 +111,18 @@
| "`" -> read_quote "quasiquote" tokens
| "#" -> read_vector tokens
| "#|" ->
- let list_reader = read_list "|#" { list_form = []; tokens } in
+ let list_reader = read_list "|#" { list_form = []; tokens } in
+ print_endline ("block comment: " ^ (String.concat " " list_reader.tokens));
{ form = T.Unspecified; tokens = list_reader.tokens }
- (* | "define-syntax" ->
- * print_endline "MACRO";
- * read_macro tokens *)
| "(" ->
let list_reader = read_list ")" { list_form = []; tokens } in
{ form = Types.list list_reader.list_form; tokens = list_reader.tokens }
| "" | "\t" | "\n" -> read_form tokens
- | _ -> if token.[0] = ';' then read_form tokens else { form = read_atom token; tokens })
+ | _ -> if token.[0] = ';' then
+ let list_reader = read_list "\n" { list_form = []; tokens } in
+ print_endline ("line comment: " ^ (String.concat " " list_reader.tokens));
+ { form = T.Unspecified; tokens = list_reader.tokens }
+ else { form = read_atom token; tokens })
;;
let slurp filename =