diff --git a/example.lsp b/example.lsp index be8088e..2942415 100644 --- a/example.lsp +++ b/example.lsp @@ -4,7 +4,7 @@ (:t "- " hd)) (:macro seq (hd .. tl) - (:seq hd) (:seq tl)) + (':seq hd) (':seq tl)) (:macro @ (k) (:t k ":")) diff --git a/sexpr.ml b/sexpr.ml index 59ae482..9b91245 100644 --- a/sexpr.ml +++ b/sexpr.ml @@ -702,7 +702,10 @@ module SExprMacroExp = struct Cons(Id(l), Cons(a,b)) -> l, (a,b) | _ -> raise @@ Misformated_Macro s in - Nil, false, (a :: ctx) + let ctx = (a :: ctx) + |> List.sort (fun a b -> macro_req_score (fst (snd b)) - macro_req_score (fst (snd a))) + in + Nil, false, ctx end | Cons(Id(i), r) when String.starts_with ~prefix:":" i -> let i = String.sub i 1 ((String.length i) - 1) in @@ -736,6 +739,13 @@ module SExprMacroExp = struct | Cons(_), Cons(Id(".."), Cons(Id(rem),Nil)) -> [rem, args] | Cons(v,rl), Cons(Id(k), rr) -> [k,li1 v] @ match_macro_arg rl rr | _ -> raise Macro_Doesnt_App + and macro_req_score args = + let open SExpr in + match args with + Nil -> 0 + | Cons(Id(".."), Cons(Id(rem),Nil)) -> 1 + | Cons(Id _, rr) -> 100 + macro_req_score rr + | _ -> raise Not_SExpr_List and expand_macro_eval expr defs = let open SExpr in let perfm expr = @@ -746,7 +756,7 @@ module SExprMacroExp = struct Some x -> x | None -> li1 expr end - | x when is_list x -> expand_macro_eval x defs + | x when is_list x -> li1 (expand_macro_eval x defs) | x -> li1 x end in