Beispiel #1
0
def add_if_statement(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply add_if_statement transformation", "with", numbermax
    p = deepcopy(program_tree)
    arg = {"numbermax": numbermax}
    arg["size"] = count_object(p, [Statements])
    walker(p, postfunction=add_if_statement_func_post, arg=arg)
    return p
Beispiel #2
0
def duplicate_function(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply duplicate_function transformation", "with", numbermax
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    arg = {"ident_present": ret, "numbermax": numbermax}
    arg["size"] = count_object(p, [Statements])
    walker(p, postfunction=duplicate_function_func_post, arg=arg)
    return p
Beispiel #3
0
def change_str(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply change_str transformation", "with", numbermax
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    arg = {"numbermax": numbermax, "ident_present": ret}
    arg["size"] = count_object(p, [String])
    walker(p, postfunction=change_str_func, arg=arg)
    return p
Beispiel #4
0
def outlining(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply outlining transformation"
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    arg = {"ident_present": ret, "encountered": [], "numbermax": numbermax}
    arg["size"] = count_object(p, [Statements])
    walker(p, postfunction=outlining_post_func, arg=arg)
    return p
Beispiel #5
0
def modify_data_flow_1(program_tree,verbose=0,numbermax=NUMBERMAX):
    if verbose >1:
        print "apply modify_data_flow_1 transformation"
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    arg={"ident_present":ret,"numbermax":numbermax}
    arg["size"]=count_object(p,[Assignment])
    walker(p,postfunction=modify_data_flow_1_post_func,arg=arg)
    return p
Beispiel #6
0
def try_eval(program_tree, verbose=False, numbermax=NUMBERMAX):
    if verbose:
        print "apply try_eval transformation"
    p = deepcopy(program_tree)
    memory = []
    walker(p,
           function=try_eval_func,
           postfunction=try_eval_post_func,
           arg=memory)
    return p
Beispiel #7
0
def evalification(program_tree, verbose=False, numbermax=NUMBERMAX):
    if verbose:
        print "apply evalification transformation"
    p = deepcopy(program_tree)
    arg = {"numbermax": numbermax}
    arg["size"] = count_object(p, [Statements])

    #~ ret =get_all_ident(program_tree)
    walker(p, evalification_func, arg=arg)
    return p
Beispiel #8
0
def rename_variables(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply rename_variables transformation"
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    if "eval" in ret:
        return program_tree
    arg = {"idents": ret, "switcher": [], "numbermax": numbermax}
    arg["size"] = count_object(p, [Ident])
    walker(p, rename_variables_func, rename_variables_post_func, arg=arg)
    return p
Beispiel #9
0
def add_dummy_exprs(parse_tree,verbose=0,numbermax=NUMBERMAX):
    """This function takes in arpument parse_tree as a parse tree, and will add some dummy exprs,
    it will return a new parse tree, with these dummy variables
    the verbose argument allows to increase verbosity"""
    if verbose >1:
        print "apply add_dummy_exprs transformation" ,"with", numbermax
    p = deepcopy(parse_tree)
  #~ ret = get_all_ident_assigned(parse_tree)
    arg={"ident_ass":[],"dummyvars":[],"numbermax":numbermax}
    arg["size"]=count_object(p,[Statements])
    walker(p,add_dummy_exprs_func,postfunction=add_dummy_exprs_func_post,arg=arg)
    return p
Beispiel #10
0
def aggregate_data(program_tree, verbose=0, numbermax=NUMBERMAX):
    if verbose > 1:
        print "apply aggregate_data transformation"
    p = deepcopy(program_tree)
    ret = get_all_ident(program_tree)
    arg = {
        "ident_present": ret,
        "numbermax": numbermax,
        "list_aggregation": []
    }
    arg["size"] = count_object(p, [Number])
    walker(p, aggregate_data_func, aggregate_data_post_func, arg=arg)
    return p
Beispiel #11
0
def henry(program_tree):
    arg = [{"global_access_read":[],"global_access_write":[] , "return":[] , "program_arg":["arguments"] ,"local_var": [] }]
    l,a = walker(program_tree,henry_func,henry_post_func,arg=arg)
    oarg = arg[0]
    (fanin,fanout) = (len(  oarg ["global_access_read"] +  oarg ["program_arg"] ),len(  oarg ["global_access_write"] +  oarg ["return"] ))
    s = sum(l)  + fanin*fanout
    return s
Beispiel #12
0
def find_ref_and_out_var(statements, encountered):
    if len(statements) > 0:
        function = get_upper_function(statements[0])
        (decbefore,
         decafter) = find_var_dec_in_function(function, statements[0])
        arg = {
            "this_present": False,
            "eval_present": False,
            "vardec": decbefore,
            "var_dec_in": [],
            "assignment": [],
            "ref": [],
            "encountered": encountered
        }
        for s in statements:
            ret, arg = walker(s, function=findrefvar, arg=arg)
        if arg == None:
            return None, None, None
        varout = list(set(arg["assignment"]))
        nameargs = list(set(arg["ref"]))
        vardecout = list(set(arg["var_dec_in"]))
        varout = filter(lambda x: not x in vardecout, varout)
        nameargs = filter(lambda x: x in arg["vardec"], nameargs)
        return nameargs, varout, vardecout
    return None, None
Beispiel #13
0
def harrison(program_tree):
    l, a = walker(program_tree, harrison_func, harrison_post_func, arg=[0])
    if len(l) > 0:
        s = max(l)
    else:
        s = 0
    return s
Beispiel #14
0
def get_types(statements, types):
    ret = []
    if not isinstance(statements, list):
        statements = [statements]
    for s in statements:
        r, dummy = walker(s, function=get_types_func, arg=types)
        ret += r
    return ret
Beispiel #15
0
def find_var_dec_in_function(function, untill):
    walk_condition = lambda x, y: not isinstance(x, Function) and y[
        "function_init"] != x
    decbefore = []
    if isinstance(function, Function):
        decbefore = [a.name for a in function.args_dec.args_list]
    ret, arg = walker(program=function,
                      function=find_var_dec_in_function_func,
                      arg={
                          "function_init": function,
                          "untill": untill,
                          "decbefore": decbefore,
                          "decafter": []
                      },
                      walk_condition=walk_condition)
    return arg["decbefore"], arg["decafter"]
Beispiel #16
0
def count_object(p, list_obj):
    arg = {}
    arg["size"] = 0
    arg["tocount"] = list_obj
    walker(p, function=count_object_func, arg=arg)
    return arg["size"]
Beispiel #17
0
def mccabe(program_tree):
    l,a = walker(program_tree,mccabe_func,arg=None)
    s = sum (l)
    return s
Beispiel #18
0
def simplify_if(program_tree, verbose=False, numbermax=NUMBERMAX):
    if verbose:
        print "apply simplify_if transformation"
    p = deepcopy(program_tree)
    walker(p, simplify_if_func, arg=[])
    return p
Beispiel #19
0
def halstead(program_tree):
    l, a = walker(program_tree, halstead_func, arg=None)
    s = sum(l)
    return s
Beispiel #20
0
def get_funcs(program):
    ret, arg = walker(program, function=get_funcs_func)
    return ret
Beispiel #21
0
def get_all_ident(program):
    ret, arg = walker(program, function=get_all_ident_func, arg=None)
    ret = list(set(ret))
    return ret
Beispiel #22
0
def find_ident(program, name):
    ret, arg = walker(program, function=find_ident_func, arg=name)
    return ret
Beispiel #23
0
def find_func(program, funcname):
    ret, arg = walker(program, function=find_func_func, arg=funcname)
    return ret
Beispiel #24
0
def undeclare_var(statements, variables):
    walker(statements,
           postfunction=undeclare_var_func_post,
           arg=variables,
           walk_condition=lambda x, y: not isinstance(x, Function))
Beispiel #25
0
def oviedo(program_tree):
    arg = [{"block_var": [], "non_block_var_usage": 0}]
    l, a = walker(program_tree, oviedo_func, oviedo_post_func, arg=arg)
    s = sum(l)
    return s
Beispiel #26
0
def gen_dot(program_tree):
    p = copy.deepcopy(program_tree)
    g=pydot.Graph (name = "Tree")
    nodelist=[]
    walker(p,function=tree_dumper,arg=(g,nodelist))
    return g.to_string()
def remove_empty_statement(program_tree, verbose=False, numbermax=NUMBERMAX):
    if verbose:
        print "apply remove_empty_statement transformation"
    p = deepcopy(program_tree)
    walker(p, remove_empty_statement_func, arg=[])
    return p
Beispiel #28
0
def munson(program_tree):
    l,a = walker(program_tree,munson_func,munson_post_func,arg=[])
    s = 0
    return s