-
Notifications
You must be signed in to change notification settings - Fork 4
/
if_to_phi.py
71 lines (58 loc) · 1.93 KB
/
if_to_phi.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
71
import typing as tp
import warnings
import libcst as cst
from . import Pass
from . import PASS_ARGS_T
from ast_tools.common import gen_free_name
from ast_tools.stack import SymbolTable
__ALL__ = ['if_to_phi']
class IfExpTransformer(cst.CSTTransformer):
def __init__(self, phi_name: str):
self.phi_name = phi_name
def leave_IfExp(
self,
original_node: cst.IfExp,
updated_node: cst.IfExp,
):
return cst.Call(
func=cst.Name(value=self.phi_name),
args=[
cst.Arg(value=v) for v in (
updated_node.test,
updated_node.body,
updated_node.orelse
)
],
)
class if_to_phi(Pass):
'''
Pass to convert IfExp to call to phi functions
phi should have signature:
phi :: Condition -> T -> F -> Union[T, F]
where:
Condition is usually bool
T is the True branch
F is the False branch
'''
def __init__(self,
phi: tp.Union[tp.Callable, str],
phi_name_prefix: tp.Optional[str] = None):
self.phi = phi
if isinstance(phi, str) and phi_name_prefix is not None:
warnings.warn('phi_name_prefix has no effect '
'if phi is a str', stacklevel=2)
elif phi_name_prefix is None:
phi_name_prefix = '__phi'
self.phi_name_prefix = phi_name_prefix
def rewrite(self,
tree: cst.CSTNode,
env: SymbolTable,
metadata: tp.MutableMapping) -> PASS_ARGS_T:
if not isinstance(self.phi, str):
phi_name = gen_free_name(tree, env, self.phi_name_prefix)
env.locals[phi_name] = self.phi
else:
phi_name = self.phi
visitor = IfExpTransformer(phi_name)
tree = tree.visit(visitor)
return tree, env, metadata