We first introduce the high-level API we provide for users to do the transformation.
Before every time you use it, you will need to register a parser to parse the source code, as follows:
If you still feel confused, please refer to tree-sitter. https://tree-sitter.github.io/tree-sitter/
PY_LANGUAGE = Language('../build/my-languages.so', 'python')
parser = Parser()
parser.set_language(PY_LANGUAGE)
We provide an "Agent" class for users, and users can use it to do the transformation step by step, as follows:
Agent_test = Agent(ori_code=code,parser=parser)
Agent_test.step(mode='default',trans_id=1) # use pass 1
Agent_test.step(mode='default',trans_id=3) # use pass 3
return Agent_test.cur_code, Agent_test.trans_sequence # return the transformed code and pass sequence
Transform_singlestmt is the high-level API we provide, users can call this to get the transformed result with details.
transform_singlestmt(data_str,parser, transformid_list,need_debug=True)
data_str(str): the input code.
parser(parser class): standard parse registered before.
transformid_list(list): the list of target transformation passes.
need_debug(bool): Whether need print debug information or not.
To illustrate the promising extensibility of our CCTest framework, below, we show 2 sample passes that are easily extended from the current CCTest framework. They are performed toward different levels of code representation. Users can refer to these samples to implement their own transformations when needed.
For each pass, there are three key parts:
Pass name: The name for each pass.
Description: How it works.
Example: Code snippet transformation example
To finish this pass, the users need to first implement a splitif function, where a list of
fixed_nodes needs to be returned. Then, users need to call splitif when needed and pass the fixed_nodes to built-in function convert to get the transformed result.
We give a simplified code example (assume that trans_id is 1), as follows:
To finish this pass, the users need to first implement a wraptry function, where a list of
fixed_nodes needs to be returned. Then, users need to call wraptry when needed and pass the fixed_nodes to built-in function convert to get the transformed result.
We give a simplified code example (assume that trans_id is 2), as follows:
To finish this pass, the users need to first implement a ifelif function, where a list of
fixed_nodes needs to be returned. Then, users need to call ifelif when needed and pass the fixed_nodes to built-in function convert to get the transformed result.
We give a simplified code example (assume that trans_id is 3), as follows: