Compare commits

..

2 Commits

Author SHA1 Message Date
5eba1c8ce1 simplify and fix 2025-08-30 00:52:30 +02:00
1a85ca03f7 still broken 2025-08-30 00:29:04 +02:00

View File

@@ -491,6 +491,16 @@ module SExpr = struct
assert (flat_map (fun x -> Cons(x, Cons((match x with Int(n) -> Int(n + 1) | x -> x) , Nil))) a = p) assert (flat_map (fun x -> Cons(x, Cons((match x with Int(n) -> Int(n + 1) | x -> x) , Nil))) a = p)
end end
let rec aggr_flat_map fn acc li =
match li with
Nil -> Nil, acc
| Cons(x, rem) ->
let x, acc = fn acc x in
let vv, acc = aggr_flat_map fn acc rem in
append vv x, acc
| _ -> raise Not_Sexpr_List
(* TODO: test aggr_flat_map *)
(* a "list" here is: (* a "list" here is:
* ( e1 . ( e2 . ( e3 . NIL ) ) ) * ( e1 . ( e2 . ( e3 . NIL ) ) )
@@ -670,63 +680,52 @@ module SExprMacroExp = struct
assert (assoc_all "key" li = ["v1";"v3"]) assert (assoc_all "key" li = ["v1";"v3"])
end end
let rec eval (ctx: (string * (SExpr.t * SExpr.t)) list ref) s = let rec eval (ctx: (string * (SExpr.t * SExpr.t)) list) s =
let open SExpr in let open SExpr in
let sli = match s with let s, ctx = if is_list s then
Cons(l,r) -> aggr_flat_map eval ctx s
(* Cons(1, Cons(2, Nil)) else
l: Cons(1, Nil) s, ctx
r: Cons(Cons(2, Nil), Nil)
append r l: Cons(1, Cons(Cons(2, Nil), Nil))
*)
let l = eval ctx l in
let r = eval ctx r in
begin match append r l with
Cons(l, Cons(r, Nil)) -> li1 @@ Cons(l,r)
| Nil -> Nil
| x -> raise (FIXME x)
end
| s -> li1 s
in in
flat_map (check_expands_while ctx) sli let s, ctx = check_expands_while ctx s in
(* expr -> list(new exprs) * bool:changed *) s, ctx
(* expr -> list(new exprs) * bool:changed * ctx *)
and check_expands ctx s = and check_expands ctx s =
let open SExpr in let open SExpr in
(* Format.printf "checking: %a\n%!" pp_t s; *) Format.printf "checking: %a\n%!" pp_t s;
match s with match s with
Cons(Id(":macro"), r) -> begin Cons(Id(":macro"), r) -> begin
let a = match r with let a = match r with
Cons(Id(l), Cons(a,b)) -> l, (a,b) Cons(Id(l), Cons(a,b)) -> l, (a,b)
| _ -> raise @@ Misformated_Macro s | _ -> raise @@ Misformated_Macro s
in in
(* Format.printf "define: %s%a -> %a\n%!" (fst a) pp_t (fst (snd a)) pp_t (snd (snd a)); *) Format.printf "define: %s%a -> %a\n%!" (fst a) pp_t (fst (snd a)) pp_t (snd (snd a));
ctx := a :: !ctx; Nil, false, (a :: ctx)
Nil, false
end end
| Cons(Id(i), r) -> begin | Cons(Id(i), r) -> begin
let i = if String.starts_with ~prefix:":" i then let i = if String.starts_with ~prefix:":" i then
String.sub i 1 ((String.length i) - 1) String.sub i 1 ((String.length i) - 1)
else "@" in else "@" in
(* Format.printf "consider expand %s %a\n%!" i pp_t r; *) Format.printf "consider expand %s%a\n%!" i pp_t r;
let rec test opt = let rec test opt ctx =
begin match opt with begin match opt with
[] -> li1 s, false [] -> li1 s, false, ctx
| hd :: tl -> | hd :: tl ->
try try
expand_macro r hd, true expand_macro r hd, true, ctx
with Macro_Doesnt_App -> with Macro_Doesnt_App ->
test tl test tl ctx
end end
in in
test (assoc_all i !ctx) test (assoc_all i ctx) ctx
end end
| s -> li1 s, false | s -> li1 s, false, ctx
and check_expands_while ctx s = and check_expands_while ctx s =
let v, c = check_expands ctx s in let v, c, ctx = check_expands ctx s in
if c then if c then
check_expands_while ctx v check_expands_while ctx v
else else
v v, ctx
and match_macro_arg args margs = and match_macro_arg args margs =
let open SExpr in let open SExpr in
match args, margs with match args, margs with
@@ -740,7 +739,8 @@ module SExprMacroExp = struct
raise @@ Macro_App_TODO (defs, mbody); raise @@ Macro_App_TODO (defs, mbody);
args args
let do_eval s = let do_eval s =
eval (ref []) s eval [] s
|> fst
let () = test "SExprMacroExp.do_eval.unchanged_num" @@ begin fun () -> let () = test "SExprMacroExp.do_eval.unchanged_num" @@ begin fun () ->
let open SExpr in let open SExpr in