File: abstract_prolog_writer.pm

package info (click to toggle)
libgo-perl 0.09-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,392 kB
  • ctags: 888
  • sloc: perl: 13,268; sh: 21; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 1,566 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

package GO::Handlers::abstract_prolog_writer;
use base qw(GO::Handlers::base Exporter);
use strict;

sub out {
    my $self = shift;
    $self->print("@_");
}

sub cmt {
    my $self = shift;
    my $cmt = shift;
    $self->out(" % $cmt") if $cmt;
    return;
}

sub prologquote {
    my $s = shift;
    my $force = shift;
    if (ref($s)) {
        if (ref($s) ne 'HASH') {
            sprintf("[%s]",
                    join(',',map{prologquote($_)} @$s));
        }
        else {
            my @keys = keys %$s;
            if (@keys == 1) {
                my $functor = $keys[0];
                my $args = $s->{$functor};
                sprintf("$functor(%s)",
                        join(', ', map {prologquote($_)} @$args));
            }
            else {
                warn "@keys != 1 - ignoring";
            }
        }
    }
    else {
        $s = '' unless defined $s;
        if ($s =~ /^\-?[0-9]+$/ && !$force) {
            return $s;
        }
        $s =~ s/\'/\'\'/g;
        "'$s'";
    }
}

sub nl {
    shift->print("\n");
}

sub fact {
    my $self = shift;
    my $pred = shift;
    my @args = @{shift||[]};
    my $cmt = shift;
    $self->out(sprintf("$pred(%s).",
		       join(', ', map {prologquote($_)} @args)));
    $self->cmt($cmt);
    $self->nl;
}

# ensure all fields are quoted
sub factq {
    my $self = shift;
    my $pred = shift;
    my @args = @{shift||[]};
    my $cmt = shift;
    $self->out(sprintf("$pred(%s).",
		       join(', ', map {prologquote($_,1)} @args)));
    $self->cmt($cmt);
    $self->nl;
}

1;