Ejemplo n.º 1
0
 def to_AST(self):
     code = self.cl.to_AST()
     assert isinstance(code, L.Enumerator)
     code = code._replace(
         iter=L.DemQuery(self.demname, tuple(
             L.ln(p) for p in self.demparams), code.iter))
     return code
Ejemplo n.º 2
0
 def get_code(self, bindenv, body):
     # Just stick a DemQuery node in before the regular code.
     # TODO: This is a little ugly in that it results in
     # littering the code with "None"s. Maybe make a special
     # case in the translation of DemQuery to avoid this.
     code = self.cl.get_code(bindenv, body)
     new_node = L.Expr(value=L.DemQuery(
         self.demname, tuple(L.ln(p) for p in self.demparams), None))
     code = (new_node, ) + code
     return code
Ejemplo n.º 3
0
    def __init__(self, aggrop, rel, relmask, params, oper_demname,
                 oper_demparams):
        assert self.aggrop in ['count', 'sum', 'min', 'max']

        # AST node representation.
        node = L.ln(rel)
        if len(params) > 0:
            node = L.SetMatch(node, relmask.make_node().s, L.tuplify(params))
        if oper_demname is not None:
            node = L.DemQuery(oper_demname, [L.ln(p) for p in oper_demparams],
                              node)
        node = L.Aggregate(node, aggrop, None)
        self.node = node
Ejemplo n.º 4
0
    def visit_Comp(self, node):
        node = self.generic_visit(node)

        if node != self.inccomp.comp:
            return node

        if self.inccomp.use_uset:
            code = L.DemQuery(self.inccomp.name,
                              tuple(L.ln(p) for p in self.inccomp.uset_params),
                              self.get_res_code())
        else:
            code = self.get_res_code()

        return code
Ejemplo n.º 5
0
    def test(self):
        cl = DemClause(EnumClause(['x', 'y'], 'R'), 'f', ['x'])

        # AST round-trip.
        clast = cl.to_AST()
        exp_clast = \
            L.Enumerator(L.tuplify(['x', 'y'], lval=True),
                         L.DemQuery('f', (L.ln('x'),), L.ln('R')))
        self.assertEqual(clast, exp_clast)
        cl2 = DemClause.from_AST(exp_clast, DemClauseFactory)
        self.assertEqual(cl2, cl)

        # Attributes.
        self.assertEqual(cl.pat_mask, (True, True))
        self.assertEqual(cl.enumvars_tagsin, ('x', ))
        self.assertEqual(cl.enumvars_tagsout, ('y', ))

        # Rewriting.
        cl2 = cl.rewrite_subst({'x': 'z'}, DemClauseFactory)
        exp_cl = DemClause(EnumClause(['z', 'y'], 'R'), 'f', ['z'])
        self.assertEqual(cl2, exp_cl)

        # Fancy rewriting, uses LookupClause.
        cl2 = DemClause(LookupClause(['x', 'y'], 'R'), 'f', ['x'])
        cl2 = cl2.rewrite_subst({'x': 'z'}, DemClauseFactory)
        exp_cl = DemClause(LookupClause(['z', 'y'], 'R'), 'f', ['z'])
        self.assertEqual(cl2, exp_cl)

        # Rating.
        rate = cl.rate(['x'])
        self.assertEqual(rate, Rate.NORMAL)
        rate = cl.rate([])
        self.assertEqual(rate, Rate.UNRUNNABLE)

        # Code generation.
        code = cl.get_code(['x'], L.pc('pass'))
        exp_code = L.pc('''
            DEMQUERY(f, [x], None)
            for y in setmatch(R, 'bu', x):
                pass
            ''')
        self.assertEqual(code, exp_code)