def sisters_(x, y, equality=False): """ Check if x,y are sisters in T """ if not (isFunctionNode(x) and isFunctionNode(y)): return False return (x.parent is not None) and x.parent == y.parent
def ancestors_(x): if not isFunctionNode(x): return [] out = [] while x.parent is not None: out.append(x.parent) x = x.parent return out
def trim_leaves_(t): """ Take a tree t and replace terminal nodes (leaves) with their returntypes. next_(next_(((nine_ if True else four_) if equal_(ten_, ten_) else one_))) to: next_(next_(((WORD if BOOL else WORD) if equal_(WORD, WORD) else WORD))) NOTE: This modifies t! """ if not isFunctionNode(t): return t elif t.is_terminal(): return t.returntype if isFunctionNode(t) and t.args is not None: t.args = [ x.returntype if (isFunctionNode(x) and x.is_terminal()) else trim_leaves_(x) for x in t.args] return t
def co_refers_(x, y): if x is y: return False # By stipulation, nothing co-refers to itself # Weird corner cases if isinstance(x, list) or isinstance(y, list): return False if x is None or y is None: return False ## Check if two FunctionNodes or strings co-refer (e.g. are indexed with the same .i in their name) xx = x.name if isFunctionNode(x) else x yy = y.name if isFunctionNode(y) else y mx = coref_matcher.search(xx) my = coref_matcher.search(yy) if mx is None or my is None: return False else: return ( mx.groups("X")[0] == my.groups("Y")[0] ) # set the default in groups so that they won't be equal if there is no match
def tree_up_(x): if isFunctionNode(x): return x.parent else: return None