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