-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (50 loc) · 1.85 KB
/
main.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
from datatypes import Program, BaseFunc
from expressions import ConstantExpr, VariableExpr, add, mul, sub, leq
from commands import FunctionCallCommand, BlockCommand, IfCommand, PassCommand
from utils import fresh_var
def orderify(expr):
c0 = VariableExpr(fresh_var("c0_"))
c1 = VariableExpr(fresh_var("c1_"))
return add(mul(c0, expr), c1)
single_loop_func = BaseFunc(
name="loop1",
input_names=["n"],
T=orderify(VariableExpr("n")),
body=IfCommand(
condition=leq(VariableExpr("n"), ConstantExpr(0)),
true=PassCommand(),
false=FunctionCallCommand(func_name="loop1", args=[sub(VariableExpr("n"), ConstantExpr(1))])
)
)
double_loop_func = BaseFunc(
name="loop2",
input_names=["n", "m"],
T=orderify(mul(VariableExpr("n"), VariableExpr("m"))),
body=IfCommand(
condition=leq(VariableExpr("n"), ConstantExpr(0)),
true=PassCommand(),
false=BlockCommand(
FunctionCallCommand(func_name="loop2",
args=[sub(VariableExpr("n"), ConstantExpr(1)), VariableExpr("m")]),
FunctionCallCommand(func_name="loop1", args=[VariableExpr("m")])
)
)
)
triple_loop_func = BaseFunc(
name="loop3",
input_names=["n", "m", "w"],
T=orderify(mul(mul(VariableExpr("n"), VariableExpr("m")), VariableExpr("w"))),
body=IfCommand(
condition=leq(VariableExpr("n"), ConstantExpr(0)),
true=PassCommand(),
false=BlockCommand(
FunctionCallCommand(func_name="loop3",
args=[sub(VariableExpr("n"), ConstantExpr(1)), VariableExpr("m"), VariableExpr("w")]),
FunctionCallCommand(func_name="loop2", args=[VariableExpr("m"), VariableExpr("w")])
)
)
)
program = Program(funcs=[
single_loop_func, double_loop_func, triple_loop_func
])
print(program.validate_complexities())