| 1 | # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>]. | ||||||||||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||||||||||||||||||||
| 5 | 28 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 28 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package Math::Formula::Token; | ||||||||||||||||||||
| 9 | 28 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 28 | $VERSION = '0.16'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | #!!! The declarations of all other packages in this file are indented to avoid | ||||||||||||||||||||
| 14 | #!!! indexing by CPAN. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | #!!! Classes and methods which are of interest of normal users are documented | ||||||||||||||||||||
| 17 | #!!! in ::Types, because the package set-up caused too many issues with OODoc. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # The object is an ARRAY. | ||||||||||||||||||||
| 20 | 1748 | ▶ | sub new(%) { my $class = shift; bless [@_], $class } | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # Returns the token in string form. This may be a piece of text as parsed | ||||||||||||||||||||
| 24 | # from the expression string, or generated when the token is computed. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 722 | ▶ | sub token { $_[0][0] //= $_[0]->_token($_[0]->value) } | ||||||||||||||||||
|
Subroutine
token: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%3/3
$_[0][0] //= $_[0]->_token($_[0]->value)
MC/DC100%2/2
$_[0][0] //= $_[0]->_token($_[0]->value)
$_[0][0]
$_[0]->_token($_[0]->value)
Truth table100%3/3
A //= B
A= $_[0][0]B= $_[0]->_token($_[0]->value)
| |||||||||||||||||||||
| 27 | 61 | ▶ | sub _token { $_[1] } | ||||||||||||||||||
|
Subroutine
_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | #------------------- | ||||||||||||||||||||
| 30 | # MF::PARENS, parenthesis tokens | ||||||||||||||||||||
| 31 | # Parser object to administer parenthesis, but disappears in the AST. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | package | ||||||||||||||||||||
| 34 | MF::PARENS; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 28 | ▶ | use base 'Math::Formula::Token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 12 | ▶ | sub level { $_[0][1] } | ||||||||||||||||||
|
Subroutine
level: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | #------------------- | ||||||||||||||||||||
| 41 | # MF::OPERATOR, operator of yet unknown type. | ||||||||||||||||||||
| 42 | # In the AST upgraded to either MF::PREFIX or MF::INFIX. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | package | ||||||||||||||||||||
| 45 | MF::OPERATOR; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 28 | ▶ | use base 'Math::Formula::Token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 28 | ▶ | use Log::Report 'math-formula', import => [ 'panic' ]; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | use constant { | ||||||||||||||||||||
| 51 | # Associativity | ||||||||||||||||||||
| 52 | 28 | LTR => 1, RTL => 2, NOCHAIN => 3, | |||||||||||||||||||
| 53 | 28 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # method operator(): Returns the operator value in this token, which | ||||||||||||||||||||
| 56 | # "accidentally" is the same value as the M<token()> method produces. | ||||||||||||||||||||
| 57 | 267 | ▶ | sub operator() { $_[0][0] } | ||||||||||||||||||
|
Subroutine
operator: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | sub compute | ||||||||||||||||||||
| 60 | 0 | ▶ | { my ($self, $context) = @_; | ||||||||||||||||||
|
Subroutine
compute: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 61 | 0 | panic +(ref $self) . ' does not compute'; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 28 | my %table; | |||||||||||||||||||
| 65 | { | ||||||||||||||||||||
| 66 | # Prefix operators and parenthesis are not needed here | ||||||||||||||||||||
| 67 | # Keep in sync with the table in Math::Formula | ||||||||||||||||||||
| 68 | 28 | my @order = ( | |||||||||||||||||||
| 69 | # [ LTR, ',' ], | ||||||||||||||||||||
| 70 | [ LTR, '?', ':' ], # ternary ?: | ||||||||||||||||||||
| 71 | [ NOCHAIN, '->' ], | ||||||||||||||||||||
| 72 | [ LTR, qw/or xor/, '//' ], | ||||||||||||||||||||
| 73 | [ LTR, 'and' ], | ||||||||||||||||||||
| 74 | [ NOCHAIN, qw/ <=> < <= == != >= > / ], | ||||||||||||||||||||
| 75 | [ NOCHAIN, qw/ cmp lt le eq ne ge gt/ ], | ||||||||||||||||||||
| 76 | [ LTR, qw/+ - ~/ ], | ||||||||||||||||||||
| 77 | [ LTR, qw!* / %! ], | ||||||||||||||||||||
| 78 | [ LTR, qw/=~ !~ like unlike/ ], | ||||||||||||||||||||
| 79 | [ LTR, '#', '.' ], | ||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 28 | my $level; | |||||||||||||||||||
| 83 | 28 | foreach (@order) | |||||||||||||||||||
| 84 | 280 | { my ($assoc, @line) = @$_; | |||||||||||||||||||
| 85 | 280 | $level++; | |||||||||||||||||||
| 86 | 280 | $table{$_} = [ $level, $assoc ] for @line; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # method find($operator) | ||||||||||||||||||||
| 91 | # Returns a list with knowledge about a know operator. | ||||||||||||||||||||
| 92 | # The first argument is a priority level for this operator. The actual | ||||||||||||||||||||
| 93 | # priority numbers may change over releases of this module. | ||||||||||||||||||||
| 94 | # The second value is a constant of associativety. Either the constant | ||||||||||||||||||||
| 95 | # LTR (compute left to right), RTL (right to left), or NOCHAIN (non-stackable | ||||||||||||||||||||
| 96 | # operator). | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 288 | ▶ | sub find($) { @{$table{$_[1]} // panic "op $_[1]" } } | ||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=8.1
Condition33%1/3
$table{$_[1]} // &panic("op $_[1]")
MC/DC0%0/2
$table{$_[1]} // &panic("op $_[1]")
$table{$_[1]}
&panic("op $_[1]")
Truth table33%1/3
A // B
A= $table{$_[1]}B= &panic("op $_[1]")
| |||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | #------------------- | ||||||||||||||||||||
| 101 | # MF::PREFIX, monadic prefix operator | ||||||||||||||||||||
| 102 | # Prefix operators process the result of the expression which follows it. | ||||||||||||||||||||
| 103 | # This is a specialization from the MF::OPERATOR type, hence shares its methods. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | package | ||||||||||||||||||||
| 106 | MF::PREFIX; | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 28 | ▶ | use base 'MF::OPERATOR'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # method child(): Returns the AST where this operator works on. | ||||||||||||||||||||
| 111 | 30 | ▶ | sub child() { $_[0][1] } | ||||||||||||||||||
|
Subroutine
child: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub compute($$) | ||||||||||||||||||||
| 114 | 30 | ▶ | { my ($self, $context) = @_; | ||||||||||||||||||
|
Subroutine
compute: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 115 | 30 | ▶ | my $value = $self->child->compute($context) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | or return undef; | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 30 | $value->prefix($self->operator, $context); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | #------------------- | ||||||||||||||||||||
| 122 | # MF::INFIX, infix (dyadic) operator | ||||||||||||||||||||
| 123 | # Infix operators have two arguments. This is a specialization from the | ||||||||||||||||||||
| 124 | # MF::OPERATOR type, hence shares its methods. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | package | ||||||||||||||||||||
| 127 | MF::INFIX; | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 28 | ▶ | use base 'MF::OPERATOR'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | # method left(): Returns the AST left from the infix operator. | ||||||||||||||||||||
| 132 | 237 | ▶ | sub left() { $_[0][1] } | ||||||||||||||||||
|
Subroutine
left: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # method right(): Returns the AST right from the infix operator. | ||||||||||||||||||||
| 135 | 237 | ▶ | sub right() { $_[0][2] } | ||||||||||||||||||
|
Subroutine
right: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | my %comparison = ( | ||||||||||||||||||||
| 138 | 3 | ▶ | '<' => [ '<=>', sub { $_[0] < 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 139 | 3 | ▶ | '<=' => [ '<=>', sub { $_[0] <= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 140 | 3 | ▶ | '==' => [ '<=>', sub { $_[0] == 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 141 | 3 | ▶ | '!=' => [ '<=>', sub { $_[0] != 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 142 | 3 | ▶ | '>=' => [ '<=>', sub { $_[0] >= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 143 | 3 | ▶ | '>' => [ '<=>', sub { $_[0] > 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 144 | 3 | ▶ | 'lt' => [ 'cmp', sub { $_[0] < 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 145 | 3 | ▶ | 'le' => [ 'cmp', sub { $_[0] <= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 146 | 3 | ▶ | 'eq' => [ 'cmp', sub { $_[0] == 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 147 | 3 | ▶ | 'ne' => [ 'cmp', sub { $_[0] != 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 148 | 3 | ▶ | 'ge' => [ 'cmp', sub { $_[0] >= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 149 | 3 | ▶ | 'gt' => [ 'cmp', sub { $_[0] > 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | ▶ | sub _compare_ops { keys %comparison } | ||||||||||||||||||
|
Subroutine
_compare_ops: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | sub compute($$) | ||||||||||||||||||||
| 155 | 237 | ▶ | { my ($self, $context) = @_; | ||||||||||||||||||
|
Subroutine
compute: called CC=5 SCAR=16.4
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 237 | ▶ | my $left = $self->left->compute($context) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | or return undef; | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 237 | ▶ | my $right = $self->right->compute($context) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | or return undef; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | # Comparison operators are all implemented via a space-ship, when available. | ||||||||||||||||||||
| 164 | # Otherwise, the usual track is taken. | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 237 | my $op = $self->operator; | |||||||||||||||||||
| 167 | 237 | ▶ | if(my $rewrite = $comparison{$op}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 168 | 36 | { my ($spaceship, $compare) = @$rewrite; | |||||||||||||||||||
| 169 | 36 | ▶ | if(my $result = $left->infix($spaceship, $right, $context)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 36 | { return MF::BOOLEAN->new(undef, $compare->($result->value)); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 201 | $left->infix($op, $right, $context); | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | #------------------- | ||||||||||||||||||||
| 179 | # MF::TERNARY, if ? then : else | ||||||||||||||||||||
| 180 | # Ternary operators have three arguments. This is a specialization from the | ||||||||||||||||||||
| 181 | # MF::OPERATOR type, hence shares its methods. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | package | ||||||||||||||||||||
| 184 | MF::TERNARY; | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 28 | ▶ | use base 'MF::OPERATOR'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 7 | ▶ | sub condition() { $_[0][1] } | ||||||||||||||||||
|
Subroutine
condition: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 189 | 3 | ▶ | sub then() { $_[0][2] } | ||||||||||||||||||
|
Subroutine
then: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 190 | 4 | ▶ | sub else() { $_[0][3] } | ||||||||||||||||||
|
Subroutine
else: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | sub compute($$) | ||||||||||||||||||||
| 193 | 7 | ▶ | { my ($self, $context) = @_; | ||||||||||||||||||
|
Subroutine
compute: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 7 | ▶ | my $cond = $self->condition->compute($context) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | or return undef; | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 7 | ▶ | ($cond->value ? $self->then : $self->else)->compute($context) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | #------------------- | ||||||||||||||||||||
| 202 | # When used, this returns a MF::STRING taken from the captures in the context. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | package | ||||||||||||||||||||
| 205 | MF::CAPTURE; | ||||||||||||||||||||
| 206 | 28 | ▶ | use base 'Math::Formula::Token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 4 | ▶ | sub seqnr() { $_[0][0] } | ||||||||||||||||||
|
Subroutine
seqnr: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | sub compute($$) | ||||||||||||||||||||
| 211 | 4 | ▶ | { my ($self, $context) = @_; | ||||||||||||||||||
|
Subroutine
compute: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 212 | 4 | my $v = $context->capture($self->seqnr -1); | |||||||||||||||||||
| 213 | 4 | ▶ | defined $v ? MF::STRING->new(undef, $v) : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | 28 | 1; | |||||||||||||||||||