line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Language::Ook. |
3
|
|
|
|
|
|
|
# Copyright (c) 2002-2007 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::Ook; |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
10016
|
use 5.006; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Language::Ook - a Ook! interpreter. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use Language::Ook; |
22
|
|
|
|
|
|
|
my $interp = new Language::Ook( "program.ook" ); |
23
|
|
|
|
|
|
|
$interp->run_code; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Print the Perl code. |
26
|
|
|
|
|
|
|
$interp->print_code; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
A programming language should be writable and readable by orang-utans. |
32
|
|
|
|
|
|
|
So Ook! is a programming language designed for orang-utans. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Ook! is bijective with BrainFuck, and thus, Turing-complete. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
51
|
|
40
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
41
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1672
|
|
42
|
|
|
|
|
|
|
our $VERSION = '1.0.2'; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 new( [filename] ) |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Create a new Ook interpreter. If a filename is provided, then read |
50
|
|
|
|
|
|
|
and store the content of the file. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
sub new { |
54
|
|
|
|
|
|
|
# Create and bless the object. |
55
|
2
|
|
|
2
|
1
|
1822
|
my $proto = shift; |
56
|
2
|
|
33
|
|
|
16
|
my $class = ref($proto) || $proto; |
57
|
2
|
|
|
|
|
7
|
my $self = { code => "" }; |
58
|
2
|
|
|
|
|
6
|
bless $self, $class; |
59
|
|
|
|
|
|
|
# Read the file if needed. |
60
|
2
|
|
|
|
|
4
|
my $file = shift; |
61
|
2
|
50
|
|
|
|
8
|
defined($file) and $self->read_file( $file ); |
62
|
|
|
|
|
|
|
# Return the object. |
63
|
2
|
|
|
|
|
5
|
return $self; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 ACCESSORS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 code ( ) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Return the associated Perl code. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=cut |
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
2
|
1
|
31
|
sub code :lvalue { $_[0]->{code} } |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 print_code( ) |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Print the equivalent Perl code. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub print_code { |
87
|
0
|
|
|
0
|
1
|
0
|
print $_[0]->code; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 read_file( filename ) |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Read a file (given as argument) and store its code. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Side effect: clear the previous code. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub read_file { |
99
|
2
|
|
|
2
|
1
|
21
|
my ($self, $filename) = @_; |
100
|
|
|
|
|
|
|
# Fetch the code. |
101
|
2
|
|
|
|
|
2
|
my $code; |
102
|
2
|
50
|
|
|
|
74
|
open OOK, "<$filename" or croak "$filename: $!"; |
103
|
|
|
|
|
|
|
{ |
104
|
2
|
|
|
|
|
4
|
local $/; # slurp mode. |
|
2
|
|
|
|
|
8
|
|
105
|
2
|
|
|
|
|
57
|
$code = ; |
106
|
2
|
|
|
|
|
9
|
$code .= " "; |
107
|
|
|
|
|
|
|
} |
108
|
2
|
|
|
|
|
22
|
close OOK; |
109
|
|
|
|
|
|
|
# Store code. |
110
|
2
|
|
|
|
|
437
|
$code =~ s/[\n\s]+/ /g; |
111
|
2
|
|
|
|
|
5
|
my $perl = "{ local \$^W = 0;\nuse bytes;\nmy \@cell = ();\nmy \$ptr = 0;\n"; |
112
|
2
|
|
|
|
|
18
|
while ( $code =~ m/Ook(.) Ook(.) /g ) { |
113
|
345
|
|
|
|
|
628
|
my $instr = $1.$2; |
114
|
345
|
100
|
|
|
|
636
|
sw: { |
115
|
345
|
|
|
|
|
330
|
$instr eq ".?" and $perl .= '$ptr++;', last sw; |
116
|
288
|
100
|
|
|
|
801
|
$instr eq "?." and $perl .= '$ptr--;', last sw; |
117
|
235
|
100
|
|
|
|
623
|
$instr eq ".." and $perl .= '$cell[$ptr]++;', last sw; |
118
|
118
|
100
|
|
|
|
227
|
$instr eq "!!" and $perl .= '$cell[$ptr]--;', last sw; |
119
|
58
|
50
|
|
|
|
284
|
$instr eq ".!" and $perl .= 'read(STDIN,$cell[$ptr],1)?ord($cell[$ptr]):0;', last sw; |
120
|
58
|
100
|
|
|
|
124
|
$instr eq "!." and $perl .= 'print chr($cell[$ptr]);', last sw; |
121
|
34
|
100
|
|
|
|
64
|
$instr eq "!?" and $perl .= 'while ($cell[$ptr]) {', last sw; |
122
|
17
|
50
|
|
|
|
118
|
$instr eq "?!" and $perl .= '}', last sw; |
123
|
0
|
|
|
|
|
0
|
croak "Ook! Ook$1 Ook$2\n"; |
124
|
|
|
|
|
|
|
} |
125
|
345
|
|
|
|
|
1288
|
$perl .= "\n"; |
126
|
|
|
|
|
|
|
} |
127
|
2
|
|
|
|
|
3
|
$perl .= "}\n"; |
128
|
2
|
|
|
|
|
9
|
$self->code = $perl; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 run_code( ) |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Run the stored code. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub run_code { |
139
|
2
|
|
|
2
|
1
|
11
|
my $self = shift; |
140
|
2
|
|
|
1
|
|
340
|
eval $self->{code}; |
|
1
|
|
|
1
|
|
10
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
1; |
146
|
|
|
|
|
|
|
__END__ |