Skip to content

Commit

Permalink
Refactor map_arg_to_mnemonic to use direct iteration
Browse files Browse the repository at this point in the history
Replace the key concatenation assumption with a fold over the entire `mappings` Hashtbl, ensuring correctness across all cases.
  • Loading branch information
Linda-Njau committed Nov 21, 2024
1 parent b95e0f2 commit 67fa077
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/sail_json_backend/json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,19 @@ 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

Expand All @@ -509,7 +512,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
Expand Down

0 comments on commit 67fa077

Please sign in to comment.