From 141db078408f94c410508970d07382d4a6087f17 Mon Sep 17 00:00:00 2001 From: Chimrod <> Date: Fri, 15 Mar 2024 10:20:31 +0100 Subject: Added eq and hash functions in the Ast.Expression --- lib/syntax/tree.ml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/syntax/tree.ml') diff --git a/lib/syntax/tree.ml b/lib/syntax/tree.ml index b7d9d15..72ae754 100644 --- a/lib/syntax/tree.ml +++ b/lib/syntax/tree.ml @@ -45,10 +45,31 @@ 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 +end = struct type t = S.pos Ast.expression type t' = t + let eq : (S.pos -> S.pos -> bool) -> t -> t -> bool = Ast.equal_expression + + 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) -- cgit v1.2.3