Exemple #1
0
def dynac(f, out, anf=None, compiler_args=()):
    """
    Run compiler on file, ``f``, write results to ``out``. Raises
    ``DynaCompilerError`` on failure.
    """

    f = path(f)
    if not f.exists():
        raise DynaCompilerError("File '%s' does not exist." % f)

    cmd = ['%s/dist/build/dyna/dyna' % dynahome, '-B', 'python', '-o', out, f]

    if anf is not None:
        cmd += ['--dump-anf=' + anf]

    cmd += compiler_args

    p = Popen(cmd, stdout=PIPE, stderr=PIPE)

    stdout, stderr = p.communicate()
    if p.returncode:
        assert not stdout.strip(), [stdout, stderr]
        stderr = hide_ugly_filename(
            stderr, lambda m: '\n  %s\n' % span_to_src(m.group(0)))
        raise DynaCompilerError(stderr, f)
Exemple #2
0
def dynac(f, out, anf=None, compiler_args=()):
    """
    Run compiler on file, ``f``, write results to ``out``. Raises
    ``DynaCompilerError`` on failure.
    """

    f = path(f)
    if not f.exists():
        raise DynaCompilerError("File '%s' does not exist." % f)

    cmd = ['%s/dist/build/dyna/dyna' % dynahome,
           '-B', 'python', '-o', out, f]

    if anf is not None:
        cmd += ['--dump-anf=' + anf]

    cmd += compiler_args

    p = Popen(cmd, stdout=PIPE, stderr=PIPE)

    stdout, stderr = p.communicate()
    if p.returncode:
        assert not stdout.strip(), [stdout, stderr]
        stderr = hide_ugly_filename(stderr, lambda m: '\n  %s\n' % span_to_src(m.group(0)))
        raise DynaCompilerError(stderr, f)
Exemple #3
0
    def add_rule(self, index, span, anf, init=None, query=None):
        assert index not in self.rules

        for (x, label, ys) in anf.unifs:
            if x == anf.head:
                assert label == '&'
                head_fn = '%s/%d' % (ys[0], len(ys) - 1)
                break
        else:
            raise AssertionError('Did not find head.')

        self.rules[index] = rule = Rule(index)
        rule.span = hide_ugly_filename(span)
        rule.src = span_to_src(span).strip()
        rule.anf = anf
        rule.head_fn = head_fn

        self.update_coarse(rule)

        if init:
            rule.init = init
            init.rule = rule
            self.uninitialized_rules.append(rule)

        elif query:
            rule.query = query
            query.rule = rule

            # fix dependents
            if head_fn not in self._gbc:
                # retract and replan rules dependent on this predicate which is
                # now backchained.
                for d in self.rule_dep[head_fn]:
                    if rule != d and d.head_fn not in self._gbc:
                        self.needs_recompile(d)

            self._gbc[head_fn].append(query)

            if head_fn in self.chart:
                # if we've added a new rule we need to recompute memos for
                # existing memos. (TODO: can do slightly better than recompute
                # -- only need to evaluate contributions from this new rule)
                self.recompute_gbc_memo(head_fn)

        else:
            raise AssertionError("Can't add rule with out an initializer or query handler.")

        if True:
            args = (index,
                    rule.src,
                    todyna([anf.head, anf.agg, anf.result, anf.evals, anf.unifs]),
                    init,
                    query)
            fn = '$rule/%s' % len(args)
            if self.agg_name[fn] is None:
                self.new_fn(fn, ':=')
            rule.item = self.build(fn, *args)
            self.emit(rule.item, true, ruleix=None, variables=None, delete=False)
Exemple #4
0
    def add_rule(self, index, span, anf, init=None, query=None):
        assert index not in self.rules

        for (x, label, ys) in anf.unifs:
            if x == anf.head:
                assert label == '&'
                head_fn = '%s/%d' % (ys[0], len(ys) - 1)
                break
        else:
            raise AssertionError('Did not find head.')

        self.rules[index] = rule = Rule(index)
        rule.span = hide_ugly_filename(span)
        rule.src = span_to_src(span).strip()
        rule.anf = anf
        rule.head_fn = head_fn

        self.update_coarse(rule)

        if init:
            rule.init = init
            init.rule = rule
            self.uninitialized_rules.append(rule)

        elif query:
            rule.query = query
            query.rule = rule

            # fix dependents
            if head_fn not in self._gbc:
                # retract and replan rules dependent on this predicate which is
                # now backchained.
                for d in self.rule_dep[head_fn]:
                    if rule != d and d.head_fn not in self._gbc:
                        self.needs_recompile(d)

            self._gbc[head_fn].append(query)

            if head_fn in self.chart:
                # if we've added a new rule we need to recompute memos for
                # existing memos. (TODO: can do slightly better than recompute
                # -- only need to evaluate contributions from this new rule)
                self.recompute_gbc_memo(head_fn)

        else:
            raise AssertionError(
                "Can't add rule with out an initializer or query handler.")

        if True:
            args = (index, rule.src,
                    todyna(
                        [anf.head, anf.agg, anf.result, anf.evals,
                         anf.unifs]), init, query)
            fn = '$rule/%s' % len(args)
            if self.agg_name[fn] is None:
                self.new_fn(fn, ':=')
            rule.item = self.build(fn, *args)
            self.emit(rule.item,
                      true,
                      ruleix=None,
                      variables=None,
                      delete=False)