-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_cfg.py
70 lines (49 loc) · 1.57 KB
/
random_cfg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import click
import random
MAX_NODES = 10
class CFGNode(object):
node_number = 0
def __init__(self):
self.name = CFGNode.node_number
self.neighbors = []
self.instrs = []
CFGNode.node_number += 1
def get_neighbors(self):
return self.neighbors
def set_neighbors(self, neighbors):
self.neighbors = neighbors
def get_name(self):
return self.name
def get_instrs(self):
return self.instrs
def set_instrs(self, instrs):
self.instrs = instrs
def __str__(self) -> str:
stringified_neighbors = ', '.join(
list(map(lambda node: str(node.get_name()), self.neighbors)))
return f"This is node {self.name} and it has neighbors [{stringified_neighbors}]."
def __repr__(self) -> str:
return self.__str__()
def gen_cfg(num_nodes):
assert type(num_nodes) == int
assert 0 < num_nodes
# create nodes
entry_node = CFGNode()
nodes_list = [entry_node]
for _ in range(num_nodes - 1):
new_node = CFGNode()
nodes_list.append(new_node)
assert len(nodes_list) == num_nodes
# add edges
for node in nodes_list:
num_neighbors = random.randint(0, min(len(nodes_list), 2))
new_neighbors = random.sample(nodes_list, num_neighbors)
node.set_neighbors(new_neighbors)
return (entry_node, nodes_list)
@click.command()
@click.option("--nodes", default=MAX_NODES, help="Number of CFG nodes.")
def main(nodes):
_, new_nodes = gen_cfg(nodes)
print(new_nodes)
if __name__ == "__main__":
main()