| 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 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 2 | 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 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 54 | # Create and bless the object. | ||||||||||||||||||||
| 55 | 0 | my $proto = shift; | |||||||||||||||||||
| 56 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 57 | 0 | my $self = { code => "" }; | |||||||||||||||||||
| 58 | 0 | bless $self, $class; | |||||||||||||||||||
| 59 | # Read the file if needed. | ||||||||||||||||||||
| 60 | 0 | my $file = shift; | |||||||||||||||||||
| 61 | 0 | ▶ | defined($file) and $self->read_file( $file ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | # Return the object. | ||||||||||||||||||||
| 63 | 0 | return $self; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head1 ACCESSORS | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 code ( ) | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Return the associated Perl code. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | sub code :lvalue { $_[0]->{code} } | ||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 PUBLIC METHODS | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head2 print_code( ) | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Print the equivalent Perl code. | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | sub print_code { | ||||||||||||||||||
|
Subroutine
print_code: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 87 | 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 | 0 | ▶ | sub read_file { | ||||||||||||||||||
|
Subroutine
read_file: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||
| 99 | 0 | my ($self, $filename) = @_; | |||||||||||||||||||
| 100 | # Fetch the code. | ||||||||||||||||||||
| 101 | 0 | my $code; | |||||||||||||||||||
| 102 | 0 | ▶ | open OOK, "<$filename" or croak "$filename: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | { | ||||||||||||||||||||
| 104 | 0 | local $/; # slurp mode. | |||||||||||||||||||
| 105 | 0 | $code = <OOK>; | |||||||||||||||||||
| 106 | 0 | $code .= " "; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 0 | close OOK; | |||||||||||||||||||
| 109 | # Store code. | ||||||||||||||||||||
| 110 | 0 | $code =~ s/[\n\s]+/ /g; | |||||||||||||||||||
| 111 | 0 | my $perl = "{ local \$^W = 0;\nuse bytes;\nmy \@cell = ();\nmy \$ptr = 0;\n"; | |||||||||||||||||||
| 112 | 0 | ▶ | while ( $code =~ m/Ook(.) Ook(.) /g ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | my $instr = $1.$2; | |||||||||||||||||||
| 114 | sw: { | ||||||||||||||||||||
| 115 | 0 | ▶ | $instr eq ".?" and $perl .= '$ptr++;', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 0 | ▶ | $instr eq "?." and $perl .= '$ptr--;', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | ▶ | $instr eq ".." and $perl .= '$cell[$ptr]++;', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | ▶ | $instr eq "!!" and $perl .= '$cell[$ptr]--;', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 0 | ▶ | $instr eq ".!" and $perl .= 'read(STDIN,$cell[$ptr],1)?ord($cell[$ptr]):0;', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | ▶ | $instr eq "!." and $perl .= 'print chr($cell[$ptr]);', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | ▶ | $instr eq "!?" and $perl .= 'while ($cell[$ptr]) {', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | ▶ | $instr eq "?!" and $perl .= '}', last sw; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | croak "Ook! Ook$1 Ook$2\n"; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 0 | $perl .= "\n"; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | 0 | $perl .= "}\n"; | |||||||||||||||||||
| 128 | 0 | $self->code = $perl; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 run_code( ) | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Run the stored code. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | sub run_code { | ||||||||||||||||||
|
Subroutine
run_code: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||
| 140 | 0 | eval $self->{code}; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 2 | 1; | |||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||