| 1 | package PPI::Token::Number::Binary; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | PPI::Token::Number::Binary - Token class for a binary number | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $n = 0b1110011; # binary integer | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | PPI::Token::Number::Binary | ||||||||||||||||||||||||||||
| 16 | isa PPI::Token::Number | ||||||||||||||||||||||||||||
| 17 | isa PPI::Token | ||||||||||||||||||||||||||||
| 18 | isa PPI::Element | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | The C<PPI::Token::Number::Binary> class is used for tokens that | ||||||||||||||||||||||||||||
| 23 | represent base-2 numbers. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 METHODS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 62 | ▶ | use PPI::Token::Number (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | BEGIN { | ||||||||||||||||||||||||||||
| 34 | 62 | ▶ | $VERSION = '1.236'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 62 | @ISA = 'PPI::Token::Number'; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =pod | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head2 base | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Returns the base for the number: 2. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub base() { 2 } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =pod | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 literal | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Return the numeric value of this token. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub literal { | ||||||||||||||||||||||||||||
| 57 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
literal: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 16 | ▶ | return if $self->{_error}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 11 | my $str = $self->_literal; | |||||||||||||||||||||||||||
| 60 | 11 | my $neg = $str =~ s/^\-//; | |||||||||||||||||||||||||||
| 61 | 11 | $str =~ s/^0[bB]//; | |||||||||||||||||||||||||||
| 62 | 11 | my $val = 0; | |||||||||||||||||||||||||||
| 63 | 11 | for my $bit ( $str =~ m/(.)/g ) { | |||||||||||||||||||||||||||
| 64 | 17 | $val = $val * 2 + $bit; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 11 | ▶ | return $neg ? -$val : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ##################################################################### | ||||||||||||||||||||||||||||
| 74 | # Tokenizer Methods | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub __TOKENIZER__on_char { | ||||||||||||||||||||||||||||
| 77 | 68 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__on_char: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 78 | 68 | my $t = shift; | |||||||||||||||||||||||||||
| 79 | 68 | my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Allow underscores straight through | ||||||||||||||||||||||||||||
| 82 | 68 | ▶ | return 1 if $char eq '_'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 60 | ▶ | if ( $char =~ /[\w\d]/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 34 | ▶ | unless ( $char eq '1' or $char eq '0' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$char eq 1 or $char eq 0
MC/DC100%2/2
$char eq 1 or $char eq 0
$char eq 1
$char eq 0
Truth table100%3/3
A or B
A= $char eq 1B= $char eq 0
| |||||||||||||||||||||||||||||
| 86 | # Add a warning if it contains non-binary chars | ||||||||||||||||||||||||||||
| 87 | 9 | $t->{token}->{_error} = "Illegal character in binary number '$char'"; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 34 | return 1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Doesn't fit a special case, or is after the end of the token | ||||||||||||||||||||||||||||
| 93 | # End of token. | ||||||||||||||||||||||||||||
| 94 | 26 | $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 62 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =pod | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Chris Dolan E<lt>cdolan@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Copyright 2006 Chris Dolan. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 114 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 117 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||