示例#1
0
    def _descend(self, tree):
        return [self._traverse(sub) for sub in tree.subtrees]



class CollectVisitor(RichTreeWalk.Visitor):

    container = set

    def __init__(self, container=None):
        if container is None:
            self.collection = self.container()
        else:
            self.collection = container

    def done(self):
        return self.collection
    


if __name__ == '__main__':
    from adt.tree.build import TreeAssistant
    
    inputs = [(1, [(2, [3, 4, 5]), (6, [(7, [8]), 9])])]
    
    for input in inputs: #@ReservedAssignment
        tree = TreeAssistant.build(input)
        print tree
        print [x.root for x in PreorderWalk(tree)]
示例#2
0
                self.collect.append(self.path)

        def leave(self, node):
            self.path = self.path.up()

        def done(self, root, final):
            return self.collect

    PATH = lambda path: path
    NODE = lambda path: path.end
    VALUE = lambda path: path.end.root

    def __init__(self, criterion, applies_to=NODE):
        self.criterion = criterion
        self.applies_to = applies_to

    def __call__(self, tree):
        visitor = self.Visitor(self.criterion, self.applies_to)
        return RichTreeWalk(visitor)(tree)

    PATH = staticmethod(PATH)
    NODE = staticmethod(NODE)
    VALUE = staticmethod(VALUE)


if __name__ == '__main__':
    from adt.tree.build import TreeAssistant

    t = TreeAssistant.build((1, [2, 3]))
    print ScanFor(lambda x: x % 2, applies_to=ScanFor.VALUE)(t)
示例#3
0
            return ConditionalPattern.ConditionComplement(self)

    class ConditionComplement(Condition):
        def __init__(self, condition):
            self.condition = condition

        def __call__(self, groups):
            return not self.condition(groups)

        def __repr__(self):
            return u"not %r" % self.condition

    class FunctorCondition(Condition):
        def __init__(self, functor):
            self.functor = functor

        def __call__(self, groups):
            return self.functor(groups)

        def __repr__(self):
            return ` self.functor `


# Snippet
if __name__ == '__main__':
    from adt.tree.build import TreeAssistant as TA
    pat = TA.build(('a', [('?', ['$1...']), ('c', ['$', '$2...'])]))
    txt = TA.build(('a', ['b', ('c', [('d', ['e', 'f']), 'k', 'j'])]))
    print "pattern:", pat
    print "text:   ", txt
    print TreeTopPattern(pat).match(txt)
示例#4
0
class Gcc(object):
    def compile_and_run(self, program_code, aout="/tmp/eval"):
        import subprocess
        gcc = subprocess.Popen("g++ -o %s -xc++ -" % aout,
                               shell=True,
                               stdin=subprocess.PIPE)
        gcc.communicate(program_code)
        if gcc.returncode == 0:
            ev = subprocess.Popen(aout, stdout=subprocess.PIPE)
            return ev.communicate()


if __name__ == '__main__':
    from adt.tree.build import TreeAssistant

    t = TreeAssistant().of(FolFormula)

    class Signature:
        a = FolFormula.Identifier(u'α', 'predicate')
        v = FolFormula.Identifier('v', 'variable')
        u = FolFormula.Identifier('u', 'variable')

    FORALL = FolFormula.FORALL
    IMPLIES = FolFormula.IMPLIES
    AND = FolFormula.AND
    vars().update(Signature.__dict__)

    inputs = [
        (FORALL, [v, (FORALL, [u,
                               (IMPLIES, [(a, [v, u]),
                                          (a, [u,
 def __call__(self, *substructure):
     ta = TreeAssistant().of(CompositeIdentifier.InternalStructure)
     return CompositeIdentifier(ta((self, substructure)).normalize(),
                                None)
示例#6
0
                g[k] = v(g)

        def __repr__(self):
            return "(aug. %s)" % ",".join(
                repr(x) for x in self.augment.iterkeys())

        def __rand__(self, template):
            if isinstance(template, Tree):
                T = self.TreePatternSubstitution
                return (T.Substitution(template) & self)
            else:
                return NotImplemented


TreePatternSubstitution.Substitution.TreePatternSubstitution = TreePatternSubstitution
TreePatternSubstitution.AugmentSubstitution.TreePatternSubstitution = TreePatternSubstitution

# Snippet
if __name__ == '__main__':
    from adt.tree.build import TreeAssistant as TA
    from adt.tree.search.pattern import TreeTopPattern
    tree = TA.build(('v', ['x', 'y', 'z']))
    tsub = TreeSubstitution({TA.build('x'): [TA.build(x) for x in 'abc']})
    #print 'before:', tree
    #print 'after: ', tsub.inplace(tree)
    pat = TreeTopPattern(TA.build(('v', ['$...'])))
    psb = TA.build(('t', ['$...', '0']))
    tpat = TreePatternSubstitution({pat: psb})
    #print 'pattern:', pat
    #print 'text:   ', tree
    #print tpat(tree)