Skip to content

Commit

Permalink
refactor functioninfo construction and usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0mz committed May 30, 2024
1 parent 6eecad5 commit cf9b413
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
29 changes: 11 additions & 18 deletions lib/ast/structures/grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ module rec FunctionsInfo : sig
context : FunctionsInfo.t;
}

type context = string list
type t = info HashTable.t

(* ---- primitive functions ----- *)
val create : int -> t
val get_info : t -> string -> info
val find_opt : t -> string -> info option
val add : t -> string -> int -> string list -> t
val iter : (string -> info -> unit) -> t -> unit

val get_func_names : t -> string list
val get_param_name_opt : t -> string -> int -> string option
val get_info : t list -> string -> info
val get_param_name : t list -> string -> int -> string

end = struct
Expand All @@ -57,12 +53,10 @@ end = struct
context : FunctionsInfo.t;
}

type context = string list
type t = info HashTable.t

(* ------- S T R U C T U R E F U N C T I O N S ------- *)
let create = HashTable.create
let get_info : t -> string -> info = HashTable.find
let find_opt : t -> string -> info option = HashTable.find_opt

let add (info : t) (func : string) (id' : int) (params' : string list) : t =
Expand All @@ -80,20 +74,19 @@ end = struct


(* ------- I N F O M A N I P U L A T I O N ------- *)
let get_func_names (info : t) : string list = List.of_seq (HashTable.to_seq_keys info)

let get_param_name_opt (functions : t) (identifier : string) (index : int) : string option =
map_default (fun {params; _} -> List.nth_opt params index) None (find_opt functions identifier)

let rec get_param_name (functions : t list) (identifier : string) (index : int) : string =
let rec get_info (functions : t list) (func_name : string) : info =
match functions with
| [] -> failwith "function name wasn't found"
| [] -> failwith "function not defined in the given context"
| context::rest ->
let info = find_opt context identifier in
let info = find_opt context func_name in
if Option.is_some info
then let info = Option.get info in
List.nth info.params index
else get_param_name rest identifier index
then Option.get info
else get_info rest func_name

let get_param_name (functions : t list) (func_name : string) (index : int) : string =
let info = get_info functions func_name in
List.nth info.params index

end

module Operator = struct
Expand Down
20 changes: 9 additions & 11 deletions lib/mdg/analyse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module Graph = Graph'
open Ast.Grammar
open Auxiliary.Functions
open Auxiliary.Structures
open Structures
open State

Expand All @@ -15,15 +14,12 @@ let rec program (is_verbose : bool) ((_, program) : m Program.t) : Graph.t * Sto
let state = empty_state in
let state' = initialize_functions state program.functions in

print_endline (string_of_int (HashTable.length state'.functions));

analyse_sequence state' program.body;
state'.graph, state'.store

and analyse (state : state) (statement : m Statement.t) : unit =
let graph = state.graph in
let store = state.store in
let funcs = state.functions in
let contx = state.context in

(* aliases *)
Expand All @@ -41,9 +37,12 @@ and analyse (state : state) (statement : m Statement.t) : unit =
let lookup = Graph.lookup graph in
let new_version = Graph.staticNewVersion graph in
let new_version' = Graph.dynamicNewVersion graph in
let get_info = FunctionsInfo.get_info contx in
let get_param_name = FunctionsInfo.get_param_name contx in


print_string (Ast.Pp.Js.print_stmt statement 0);
Store.print store;
print_endline "--------------";
(match statement with
(* -------- A S S I G N - E X P R -------- *)
| _, AssignSimple {left; right} ->
Expand All @@ -52,7 +51,7 @@ and analyse (state : state) (statement : m Statement.t) : unit =

| _, AssignFunction {id; left; body; _} ->
let func_name = Identifier.get_name left in
let info = FunctionsInfo.get_info funcs func_name in
let info = get_info func_name in
if (info.id = id) then (
let param_locs = Graph.get_param_locations graph func_name in
let new_store = Store.empty in
Expand Down Expand Up @@ -245,17 +244,16 @@ and property_lookup_name (left : m Identifier.t) (_object : m Expression.t) (pro
if Identifier.is_generated left then obj_prop else Identifier.get_name left ^ ", " ^ obj_prop

and initialize_functions (state : state) (funcs_info : FunctionsInfo.t) : state =

let rec initialize_functions' (state : state) (to_process : FunctionsInfo.t list) : unit =
match to_process with
| [] -> ()
| context::rest ->
let to_process = ref rest in
FunctionsInfo.iter (fun func info ->
initilize_function' state func info;
to_process := info.context :: !to_process;
) context;
initialize_functions' state !to_process
| [] -> ()
) context;
initialize_functions' state !to_process

and initilize_function' (state : state) (func_name : string) (info : FunctionsInfo.info) : unit =
let graph = state.graph in
Expand All @@ -278,4 +276,4 @@ and initialize_functions (state : state) (funcs_info : FunctionsInfo.t) : state
in

initialize_functions' state [funcs_info];
{state with functions = funcs_info; context = [funcs_info] }
{state with context = [funcs_info] }
7 changes: 6 additions & 1 deletion lib/mdg/structures/graph'.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ let get_params (graph : t) (location : location) : location list =
(* ------- M A I N F U N C T I O N S -------*)
let lub (graph : t) (graph' : t) : unit =
(* least upper bound *)
iter_edges (fun from edges' ->
iter (fun from edges' node' ->
let edges = get_edges graph from in
replace_edges graph from (EdgeSet.union edges edges');

(* also update node info *)
if Option.is_none node' then
let node = find_node_opt graph from in
option_may (replace_node graph from) node
) graph'

let alloc (_ : t) (id : int) : location =
Expand Down
2 changes: 0 additions & 2 deletions lib/mdg/structures/state.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type state = {
store : Store.t;
this : LocationSet.t;
(* function information *)
functions : FunctionsInfo.t;
context : FunctionsInfo.t list;
}

Expand All @@ -31,7 +30,6 @@ let empty_state = {
store = Store.empty;
this = Store.this_loc;
(* function information *)
functions = FunctionsInfo.create 1;
context = [];
}

Expand Down

0 comments on commit cf9b413

Please sign in to comment.