def visit_List(self, node): if node.elts: self.push() *elts, last = node.elts for each in elts: self.visit(each) if is_bitor(last): self.visit(last.left) self.visit(last.right) *items, left, right = self.pop() cdr = self.cons(left, right) else: self.visit(last) *items, last = self.pop() cdr = self.cons(last, NIL) self.append(foldr(self.cons, items, cdr)) else: self.append(NIL)
def expand_pushbacks(node): if not node.elts: return None elif all(is_terminal(each) for each in node.elts): elts = [collect_pushback(_C_(unit(each))) for each in node.elts] return foldr(conjunction, elts) else: raise TypeError( 'Non-terminal in DCG pushback list found: {}'.format(node))
def expand_terminals(node, cont): if not node.elts: return cont(None) elif all(is_terminal(each) for each in node.elts): *elts, last = ( collect_terminal(_C_(unit(each))) for each in node.elts) return foldr(conjunction, elts, cont(last)) else: raise TypeError( 'Non-terminal in DCG terrminal list found: {}'.format(node))
def make_list(env, items, tail=NIL): def cons(*params): return List(env=env, params=params) return foldr(cons, items, tail)