line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Language::Befunge. |
3
|
|
|
|
|
|
|
# Copyright (c) 2001-2009 Jerome Quelin, all rights reserved. |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
6
|
|
|
|
|
|
|
# it under the same terms as Perl itself. |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package Language::Befunge::lib::FILE; |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
6373
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
48
|
|
13
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
14
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
|
|
14
|
use Class::XSAccessor accessors => { |
17
|
|
|
|
|
|
|
_iohs => '_iohs', |
18
|
|
|
|
|
|
|
_bufs => '_bufs', |
19
|
1
|
|
|
1
|
|
8
|
}; |
|
1
|
|
|
|
|
2
|
|
20
|
1
|
|
|
1
|
|
1169
|
use IO::File; |
|
1
|
|
|
|
|
11609
|
|
|
1
|
|
|
|
|
1243
|
|
21
|
|
|
|
|
|
|
|
22
|
0
|
|
|
0
|
1
|
|
sub new { return bless { _iohs=>{}, _bufs=>{} }, shift; } |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
# C( $fid ) |
27
|
|
|
|
|
|
|
# |
28
|
|
|
|
|
|
|
# Close filehandle corresponding to $fid |
29
|
|
|
|
|
|
|
# |
30
|
|
|
|
|
|
|
sub C { |
31
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
32
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
35
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
36
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
#use Data::Dumper; print Dumper($self); |
39
|
|
|
|
|
|
|
#warn ">>>$fid=$fh<<<\n"; |
40
|
0
|
|
|
|
|
|
delete $self->_iohs->{$fid}; |
41
|
0
|
|
|
|
|
|
delete $self->_bufs->{$fid}; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
$fh->close or $ip->dir_reverse; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub D { |
47
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
48
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $file = $ip->spop_gnirts; |
51
|
0
|
0
|
|
|
|
|
unlink $file or $ip->dir_reverse; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub G { |
55
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
56
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
59
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
60
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
0
|
|
|
|
my $line = $fh->getline // ''; |
63
|
0
|
|
|
|
|
|
$ip->spush_args( $fid, $line, length($line) ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub L { |
67
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
68
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
71
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
72
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
73
|
0
|
|
|
|
|
|
my $pos = $fh->tell; |
74
|
0
|
0
|
|
|
|
|
$pos == -1 |
75
|
|
|
|
|
|
|
? $ip->dir_reverse |
76
|
|
|
|
|
|
|
: $ip->spush( $fid, $pos ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub O { |
80
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
81
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# pop the values |
84
|
0
|
|
|
|
|
|
my $path = $ip->spop_gnirts; |
85
|
0
|
|
|
|
|
|
my $mode = $ip->spop; |
86
|
0
|
|
|
|
|
|
my $vec = $ip->spop_vec; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# try to open the file |
89
|
0
|
|
|
|
|
|
my @modes = ( '<', '>', '>>', '+<', '+>', '+>>' ); |
90
|
0
|
|
|
|
|
|
my $fh = IO::File->new; |
91
|
0
|
0
|
|
|
|
|
$fh->open( $path, $modes[$mode] ) or return $ip->dir_reverse; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# store handles & whatnots |
94
|
0
|
|
|
|
|
|
my $fid = $fh->fileno; |
95
|
0
|
|
|
|
|
|
$self->_iohs->{$fid} = $fh; |
96
|
0
|
|
|
|
|
|
$self->_bufs->{$fid} = $vec; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
$ip->spush( $fid ); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub P { |
102
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
103
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $str = $ip->spop_gnirts; |
106
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
107
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
108
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
$fh->print($str) or return $ip->dir_reverse; |
111
|
0
|
|
|
|
|
|
$ip->spush($fid); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub R { |
115
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
116
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my $c = $ip->spop; |
119
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
120
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
121
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
my $buf; |
124
|
0
|
|
|
|
|
|
$fh->read( $buf, $c ); |
125
|
0
|
|
|
|
|
|
$ip->spush( $fid ); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my $storage = $interp->get_storage; |
128
|
0
|
|
|
|
|
|
my $vec = $self->_bufs->{$fid}; |
129
|
0
|
|
|
|
|
|
$storage->store_binary( $buf, $vec ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub S { |
133
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
134
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# pop the values |
137
|
0
|
|
|
|
|
|
my $pos = $ip->spop; |
138
|
0
|
|
|
|
|
|
my $from = $ip->spop; |
139
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
140
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
141
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
$fh->seek( $pos, $from ); |
144
|
0
|
|
|
|
|
|
$ip->spush( $fid ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub W { |
148
|
0
|
|
|
0
|
1
|
|
my ($self, $interp) = @_; |
149
|
0
|
|
|
|
|
|
my $ip = $interp->get_curip; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# pop the values |
152
|
0
|
|
|
|
|
|
my $c = $ip->spop; |
153
|
0
|
|
|
|
|
|
my $fid = $ip->spop; |
154
|
0
|
|
|
|
|
|
my $fh = $self->_iohs->{$fid}; |
155
|
0
|
0
|
|
|
|
|
defined $fh or return $ip->dir_reverse; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
my $storage = $interp->get_storage; |
158
|
0
|
|
|
|
|
|
my $pos = $self->_bufs->{$fid}; |
159
|
0
|
|
|
|
|
|
my $size = Language::Befunge::Vector->new_zeroes( $pos->get_dims ); |
160
|
0
|
|
|
|
|
|
$size->set_component($_,1) for 1 .. $pos->get_dims - 1; |
161
|
0
|
|
|
|
|
|
$size->set_component(0, $c); |
162
|
0
|
|
|
|
|
|
my $buf = $storage->rectangle($pos, $size); |
163
|
0
|
|
|
|
|
|
$fh->syswrite( $buf ); |
164
|
0
|
|
|
|
|
|
$ip->spush( $fid ); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
1; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
__END__ |