diff options
author | Chimrod <> | 2024-03-27 15:50:56 +0100 |
---|---|---|
committer | Chimrod <> | 2024-03-27 15:50:56 +0100 |
commit | 246d3c93e6c628e333c047e225edd284ed156ecb (patch) | |
tree | b0e017132fdf587b83269441d1c54b2446cf0c02 /lib/syntax/tree.ml | |
parent | 14504f36b603984c14b05995a8928cbd40dfa670 (diff) | |
parent | baa258ac91df8a80209b322e8d42c5deb2ada536 (diff) |
Added the check (I should have had order my commit betters)
Diffstat (limited to 'lib/syntax/tree.ml')
-rw-r--r-- | lib/syntax/tree.ml | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/lib/syntax/tree.ml b/lib/syntax/tree.ml index b7d9d15..0074df8 100644 --- a/lib/syntax/tree.ml +++ b/lib/syntax/tree.ml @@ -45,10 +45,48 @@ module Ast = struct end (** Default implementation for the expression *) -module Expression : S.Expression with type t' = S.pos Ast.expression = struct +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 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) + | Literal (pos, l) -> + let litt = List.map ~f:(T.map_litteral ~f:(hash f)) l in + Hashtbl.hash (f pos, litt) + | Ident { pos; name; index } -> + Hashtbl.hash (f pos, name, Option.map (hash f) index) + | BinaryOp (pos, op, o1, o2) -> + Hashtbl.hash (f pos, op, hash f o1, hash f o2) + | Op (pos, op, o1) -> Hashtbl.hash (f pos, op, hash f o1) + | Function (pos, name, args) -> + Hashtbl.hash (f pos, name, List.map ~f:(hash f) args) + let v : t -> t' = fun t -> t let integer : S.pos -> string -> t = fun pos i -> Ast.Integer (pos, i) |