shithub: martian9

Download patch

ref: b6f4824d97a68ecfa763e1edcbef629ff3ba1cfc
parent: 271c7847be7a17ea20b84720a4b740255877da6c
author: McKay Marston <mckay.marston@greymanlabs.com>
date: Tue Oct 20 19:34:11 EDT 2020

more progress

--- a/eval.ml
+++ b/eval.ml
@@ -62,6 +62,7 @@
  *     | _ -> ast)
  *   | _ -> ast *)
 and eval ast env =
+  print_endline ("AST: " ^ Printer.print ast true);
   match ast with
   | T.List { T.value = [] } -> ast
   (* Can this be replaced with a define-syntax thing? *)
@@ -95,7 +96,7 @@
             Env.set sub_env name arg;
             bind_args names args
           | [], [] -> ()
-          | _ -> raise (Utils.Syntax_error "wrong parameter count for lambda")
+          | _ -> raise (Utils.Syntax_error ("wrong parameter count for lambda: " ^ Printer.dump arg_names))
         in
         bind_args arg_names args;
         eval expr sub_env)
--- a/macro.ml
+++ b/macro.ml
@@ -244,29 +244,27 @@
 ;;
 
 let match_variant macro args =
-  match macro with
-  | T.Map { T.value = meta } ->
-    (match Types.M9map.find Types.macro_variants meta with
-    | T.Map { T.value = variant_list } ->
-      Types.M9map.iter
-        (fun k v ->
-          print_endline (Printer.print k true ^ ": " ^ Printer.print v true);
-          match v with
-          | T.List { T.value = T.List { T.value = x } :: z } ->
-            print_endline
-              (" !!! ["
-              ^ string_of_int (List.length args)
-              ^ "]("
-              ^ string_of_int (List.length x)
-              ^ ") "
-              ^ Printer.dump x
-              ^ " :: "
-              ^ Printer.dump z);
-            if List.length args = List.length x then print_endline "MATCH!" else print_endline "no match"
-          | _ -> ())
-        variant_list
-    | _ -> ())
-  | _ -> ()
+  let vmatch = ref "" in
+  (match macro with
+   | T.Map { T.value = meta } ->
+      (match Types.M9map.find Types.macro_variants meta with
+       | T.Map { T.value = variant_list } ->
+          Types.M9map.iter
+            (fun k v ->
+              print_endline (Printer.print k true ^ ": " ^ Printer.print v true);
+              match v with
+              | T.List { T.value = T.List { T.value = x } :: z } ->
+                 print_endline
+                   (" >>>> [" ^ string_of_int (List.length args) ^ "|"
+                    ^ string_of_int (List.length x) ^ "] "
+                    ^ Printer.dump x ^ " :: " ^ Printer.dump z);
+                 if List.length args = List.length x
+                 then vmatch := (Printer.print (List.hd x) true)
+              | _ -> ())
+            variant_list
+       | _ -> ())
+   | _ -> ());
+  !vmatch
 ;;
 
 (* match meta with
--- a/notes.org
+++ b/notes.org
@@ -13,6 +13,7 @@
 Right now you need to use lambda
 ** DONE (cons) doesn't work
 This appears to work, now, but not with a pair
+** TODO make a "macro_reader" like a "list_reader"?
 * Read
 ** macro "transformers" should be "clauses"
 Which themselves consist of "pattern" -> "template"
--- a/reader.ml
+++ b/reader.ml
@@ -36,10 +36,7 @@
         | "\\n" -> "\n"
         | x -> String.sub x 1 1)
       without_quotes)
-  else (
-    output_string stderr "expected '\"', got EOF\n";
-    flush stderr;
-    raise End_of_file)
+  else raise (Utils.Syntax_error "unterminated string")
 ;;
 
 let fix_pattern sym pattern =
@@ -57,7 +54,7 @@
       replace_token rest
     | _ -> raise (Utils.Syntax_error "unable to fix pattern")
   in
-  replace_token tokenized_pattern
+  replace_token (List.hd tokenized_pattern :: "define" :: List.tl tokenized_pattern)
 ;;
 
 let read_atom token =
@@ -91,19 +88,20 @@
       | _ -> T.Nil
     with
     | T.Macro { T.value = sym; meta } ->
-      (* print_endline ("\nFOUND A MACRO! " ^ Printer.print sym true);
-       * print_endline ("  tokens: " ^ String.concat " " list_reader.tokens); *)
-      let rec collect_args tokens args =
-        match tokens with
-        | [ t ] -> args @ [ t ]
-        | t :: ts -> if t = eol then args else collect_args ts args @ [ t ]
-        | _ -> []
-      in
-      let args = collect_args (List.tl list_reader.tokens) [] in
-      Macro.match_variant meta args
+       print_endline ("XXXX MACRO FOUND");
+       let rec collect_args tokens args =
+         match tokens with
+         | [ t ] -> args @ [ t ]
+         | t :: ts -> if t = eol then args else collect_args ts args @ [ t ]
+         | _ -> []
+       in
+       let args = collect_args (List.tl list_reader.tokens) [] in
+       print_endline ("<><><><>: " ^ Macro.match_variant meta args)
     | _ -> ());
   match list_reader.tokens with
-  | [] -> raise (Utils.Syntax_error ("unterminated '" ^ eol ^ "'"))
+  | [] -> print_endline ("ERROR: " ^ Printer.dump list_reader.list_form);
+          raise (Utils.Syntax_error ("unterminated '" ^ eol ^ "'"))
+  | token :: [] -> { list_form = list_reader.list_form; tokens = [")"] }
   | token :: tokens ->
     if Str.string_match (Str.regexp eol) token 0
     then { list_form = list_reader.list_form; tokens }
@@ -126,6 +124,7 @@
     | _ -> read_form tokens)
 
 and read_macro tokens =
+  let macro = ref [] in
   let list_reader = read_list ")" { list_form = []; tokens } in
   print_endline ("MACRO: " ^ Printer.dump list_reader.list_form);
   (match list_reader.list_form with
@@ -139,12 +138,12 @@
       Env.set registered_macros sym macro_entry;
       Types.M9map.iter
         (fun k v ->
-          print_endline ("   >>> " ^ String.concat " " (fix_pattern k (Printer.print v true)));
-          print_endline ("  >> " ^ Printer.print k true ^ ":  " ^ Printer.print v true))
+          print_endline ("   >>> " ^ Printer.print k true ^ ":  " ^ String.concat " " (fix_pattern k (Printer.print v true)));
+          macro := !macro @ (fix_pattern k (Printer.print v true)))
         variants
     | _ -> raise (Utils.Syntax_error "read_macro botch"))
-  | _ as x -> print_endline ("  rest: " ^ Printer.dump x));
-  { form = Types.list list_reader.list_form; tokens = list_reader.tokens }
+  | _ as x -> print_endline ("  last rest: " ^ Printer.dump x));
+  read_form !macro
 
 and read_form all_tokens =
   (* print_endline ("READ_FORM: " ^ String.concat " " all_tokens); *)