Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
def make_list(env, items, tail=NIL):

    def cons(*params):
        return List(env=env, params=params)

    return foldr(cons, items, tail)