From baa258ac91df8a80209b322e8d42c5deb2ada536 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Fri, 15 Mar 2024 10:45:12 +0100 Subject: New test for duplicates evalutations in the code --- lib/syntax/tree.ml | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'lib/syntax/tree.ml') diff --git a/lib/syntax/tree.ml b/lib/syntax/tree.ml index 72ae754..0074df8 100644 --- a/lib/syntax/tree.ml +++ b/lib/syntax/tree.ml @@ -48,14 +48,31 @@ end module Expression : sig include S.Expression with type t' = S.pos Ast.expression - val eq : (S.pos -> S.pos -> bool) -> t -> t -> bool - val hash : (S.pos -> int) -> t -> int + val eq : (S.pos -> S.pos -> bool) -> t' -> t' -> bool + val hash : (S.pos -> int) -> t' -> int + val exists : f:(t' -> bool) -> t' -> bool end = struct type t = S.pos Ast.expression type t' = t let eq : (S.pos -> S.pos -> bool) -> t -> t -> bool = Ast.equal_expression + (* Add a way to filter an expression *) + let rec exists : f:(t -> bool) -> t -> bool = + fun ~f -> function + | BinaryOp (_, _, o1, o2) as op -> f op || exists ~f o1 || exists ~f o2 + | Op (_, _, expr) as op -> f op || exists ~f expr + | Function (_, _, exprs) as fn -> f fn || List.exists exprs ~f:(exists ~f) + | Literal (_, litts) as litt -> + f litt + || List.exists litts ~f:(function + | T.Text _ -> false + | T.Expression ex -> exists ~f ex) + | Ident { index; _ } as ident -> ( + f ident + || match index with None -> false | Some expr -> exists ~f expr) + | Integer _ as int -> f int + let rec hash : (S.pos -> int) -> t -> int = fun f -> function | Integer (pos, v) -> Hashtbl.hash (f pos, v) -- cgit v1.2.3