From 50e5fc48e1af6c72a03e91d1a4867344a267070d Mon Sep 17 00:00:00 2001 From: Linda Njau Date: Thu, 21 Nov 2024 15:50:37 +0300 Subject: [PATCH] Refactor `map_arg_to_mnemonic` to use direct iteration Replace the key concatenation assumption with a fold over the entire `mappings` Hashtbl, ensuring correctness across all cases. --- src/sail_json_backend/json.ml | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/sail_json_backend/json.ml b/src/sail_json_backend/json.ml index 3b1bed55d..0c945460c 100644 --- a/src/sail_json_backend/json.ml +++ b/src/sail_json_backend/json.ml @@ -482,16 +482,20 @@ let parse_funcl fcl = end | _ -> debug_print "FCL_funcl other" -let map_arg_to_mnemonic arg id = - List.find_map - (fun (enum, mnemonic) -> - if List.hd enum = arg then ( - debug_print ("Matched " ^ List.hd enum ^ " with mnemonic: " ^ List.hd mnemonic); - Some (List.hd mnemonic) - ) - else None +let map_arg_to_mnemonic arg = + Hashtbl.fold + (fun _ (enum, mnemonic) acc -> + match acc with + | Some _ -> acc + | None -> + if List.hd enum = arg then ( + debug_print ("Matched " ^ List.hd enum ^ " with mnemonic: " ^ List.hd mnemonic); + Some (List.hd mnemonic) + ) + else None ) - (Hashtbl.find_all mappings (String.lowercase_ascii (id ^ "_mnemonic"))) + mappings + None let get_index elem lst = List.find_index (fun x -> x = elem) lst @@ -509,7 +513,7 @@ let get_mnemonic id args_list = if Str.string_match str_in_parens str 0 then ( let param = Str.matched_group 1 str in debug_print ("param: " ^ param); - match map_param_to_arg id param args_list with Some arg -> map_arg_to_mnemonic arg id | None -> None + match map_param_to_arg id param args_list with Some arg -> map_arg_to_mnemonic arg | None -> None ) else ( match Hashtbl.find_opt assembly_clean id with