shithub: martian9

ref: df79e7e4c7f40d06925385e94365b5cba554f1fe
dir: /macro.ml/

View raw version
module T = Types.Types

let rec is_matching_pattern sym pattern args matched =
  match pattern, args with
  (* literals not handled, yet *)
  | ph :: pt, ah :: at -> print_endline "    LIST <-> LIST";
                          if (ph = "_" || (ph = (Printer.print sym true) && ph = (Printer.print ah true))) then is_matching_pattern sym pt at matched && true else is_matching_pattern sym pt at matched
   | ph :: pt, [] -> print_endline "    LIST <-> []";
                     if (ph = "_" || ph = (Printer.print sym true)) then is_matching_pattern sym pt [] matched && true else false
   | _, _ -> matched

let rec expand ast env args sym meta =
  print_endline ("\n\nTHIS IS A MACRO: " ^ Printer.print sym true);
  print_endline ("   META: " ^ Printer.print meta true);
  print_endline ("   ARGS: " ^ Printer.dump args);
  print_endline ("  AST:   " ^ Printer.print ast true);

    (* and expand args env sym meta = *)
  (* let sub_env = Env.make (Some env) in
   * Env.set
   *   sub_env
   *   (Types.symbol "_")
   *   (Types.proc (function
   *       | [ ast ] -> eval ast sub_env
   *       | _ -> T.Nil)); *)
  
  match meta with
  | T.Map { T.value = m } ->
    (try
       let literals = Types.M9map.find Types.macro_literals m in
       let transformers = Types.M9map.find Types.macro_transformers m in
       print_endline
         ("  -- EVAL_MACRO: literals: "
         ^ Printer.print literals true
         ^ "     transformers: "
         ^ Printer.print transformers true);
       let rec match_transform transforms =
         match transforms with
         | hd :: tl ->
           print_endline ("   __ hd: " ^ Printer.print hd true);
           print_endline ("   __ args: " ^ Printer.dump args);
           (match hd with
           | T.List
               { T.value = [ T.List { T.value = pattern }; T.List { T.value = template } ] }
             ->
             print_endline ("     _ multi pattern: " ^ Printer.dump pattern); match_transform tl
           | T.List { T.value = [ T.List { T.value = pattern }; atom ] } ->
              print_endline ("     _ single pattern: " ^ Printer.dump pattern);
              print_endline ("matched?: " ^ (if is_matching_pattern sym (List.map (fun x -> Printer.print x true) pattern) args true then "yes" else "no"));
              if is_matching_pattern sym (List.map (fun x -> Printer.print x true) pattern) args true then atom else match_transform tl
           | _ -> ast)
         | [] -> ast
       in
       match_transform (Core.seq transformers)
     with
    | Not_found -> ast)
  | _ -> ast