File Coverage

blib/lib/Language/Befunge/lib/BASE.pm
Criterion Covered Total %
statement 9 35 25.7
branch 0 6 0.0
condition 0 12 0.0
subroutine 3 9 33.3
pod 6 6 100.0
total 18 68 26.4


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::BASE;
11              
12 1     1   4578 use strict;
  1         3  
  1         38  
13 1     1   6 use warnings;
  1         2  
  1         30  
14              
15 1     1   964 use Math::BaseCalc;
  1         952  
  1         15  
16              
17 0     0 1   sub new { return bless {}, shift; }
18              
19             my @digits = ( 0 .. 9, 'a'..'z' );
20              
21             # -- outputs
22              
23             #
24             # B( $n )
25             #
26             # Output top of stack in binary.
27             #
28             sub B {
29 0     0 1   my ($self, $lbi) = @_;
30 0           printf "%b", $lbi->get_curip->spop;
31             }
32              
33              
34             #
35             # H( $n )
36             #
37             # Output top of stack in binary.
38             #
39             sub H {
40 0     0 1   my ($self, $lbi) = @_;
41 0           printf "%x", $lbi->get_curip->spop;
42             }
43              
44              
45             #
46             # N( $n, $b )
47             #
48             # Output $n in base $b.
49             #
50             sub N {
51 0     0 1   my ($self, $lbi) = @_;
52 0           my $ip = $lbi->get_curip;
53 0           my $b = $ip->spop;
54 0           my $n = $ip->spop;
55 0 0 0       if ( $b == 0 || $b == 1 || $b > scalar(@digits) ) {
      0        
56             # bases 0 and 1 are not valid.
57             # bases greater than 36 require too much chars.
58 0           $ip->dir_reverse;
59 0           return;
60             }
61 0           my $bc = Math::BaseCalc->new(digits=> [ @digits[0..$b-1] ]);
62 0           print $bc->to_base( $n );
63             }
64              
65             #
66             # O( $n )
67             #
68             # Output top of stack in octal.
69             #
70             sub O {
71 0     0 1   my ($self, $lbi) = @_;
72 0           printf "%o", $lbi->get_curip->spop;
73             }
74              
75              
76             # -- input
77              
78             #
79             # $n = I( $b )
80             #
81             # Input value in specified base, and push it on the stack.
82             #
83             sub I {
84 0     0 1   my ($self, $lbi) = @_;
85 0           my $ip = $lbi->get_curip;
86 0           my $in = $lbi->get_input;
87 0 0         return $ip->dir_reverse unless defined $in;
88 0           my $b = $ip->spop;
89 0 0 0       if ( $b == 0 || $b == 1 || $b > scalar(@digits) ) {
      0        
90             # bases 0 and 1 are not valid.
91             # bases greater than 36 require too much chars.
92 0           $ip->dir_reflect;
93 0           return;
94             }
95 0           my $bc = Math::BaseCalc->new(digits=> [ @digits[0..$b-1] ]);
96            
97 0           $ip->spush( $bc->to_base( $in ) );
98             }
99              
100              
101              
102             1;
103              
104             __END__