| 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 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 13 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package Math::Formula::Token; | ||||||||||||||||||||
| 9 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 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 | 837 | ▶ | 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 | 323 | ▶ | 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 | 42 | ▶ | 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 MF::PARENS; | ||||||||||||||||||||
| 34 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 13 | ▶ | use base 'Math::Formula::Token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 12 | ▶ | sub level { $_[0][1] } | ||||||||||||||||||
|
Subroutine
level: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | #------------------- | ||||||||||||||||||||
| 43 | # MF::OPERATOR, operator of yet unknown type. | ||||||||||||||||||||
| 44 | # In the AST upgraded to either MF::PREFIX or MF::INFIX. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | package MF::OPERATOR; | ||||||||||||||||||||
| 47 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 13 | ▶ | use base 'Math::Formula::Token'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | use constant { | ||||||||||||||||||||
| 54 | # Associativity | ||||||||||||||||||||
| 55 | 13 | LTR => 1, RTL => 2, NOCHAIN => 3, | |||||||||||||||||||
| 56 | 13 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # method operator(): Returns the operator value in this token, which | ||||||||||||||||||||
| 59 | # "accidentally" is the same value as the M<token()> method produces. | ||||||||||||||||||||
| 60 | 122 | ▶ | sub operator() { $_[0][0] } | ||||||||||||||||||
|
Subroutine
operator: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub _compute { die } # must be extended | ||||||||||||||||||
|
Subroutine
_compute: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 13 | my %table; | |||||||||||||||||||
| 65 | { | ||||||||||||||||||||
| 66 | # Prefix operators and parenthesis are not needed here | ||||||||||||||||||||
| 67 | # Keep in sync with the table in Math::Formula | ||||||||||||||||||||
| 68 | 13 | my @order = ( | |||||||||||||||||||
| 69 | # [ LTR, ',' ], ? : | ||||||||||||||||||||
| 70 | [ LTR, qw/or xor/ ], | ||||||||||||||||||||
| 71 | [ LTR, 'and' ], | ||||||||||||||||||||
| 72 | [ NOCHAIN, qw/ <=> < <= == != >= > / ], | ||||||||||||||||||||
| 73 | [ NOCHAIN, qw/ cmp lt le eq ne ge gt/ ], | ||||||||||||||||||||
| 74 | [ LTR, qw/+ - ~/ ], | ||||||||||||||||||||
| 75 | [ LTR, qw!* / %! ], | ||||||||||||||||||||
| 76 | [ LTR, qw/=~ !~ like unlike/ ], | ||||||||||||||||||||
| 77 | [ LTR, '#', '.' ], | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 13 | my $level; | |||||||||||||||||||
| 81 | 13 | foreach (@order) | |||||||||||||||||||
| 82 | 104 | { my ($assoc, @line) = @$_; | |||||||||||||||||||
| 83 | 104 | $level++; | |||||||||||||||||||
| 84 | 104 | $table{$_} = [ $level, $assoc ] for @line; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # method find($operator) | ||||||||||||||||||||
| 89 | # Returns a list with knowledge about a know operator. | ||||||||||||||||||||
| 90 | # The first argument is a priority level for this operator. The actual | ||||||||||||||||||||
| 91 | # priority numbers may change over releases of this module. | ||||||||||||||||||||
| 92 | # The second value is a constant of associativety. Either the constant | ||||||||||||||||||||
| 93 | # LTR (compute left to right), RTL (right to left), or NOCHAIN (non-stackable | ||||||||||||||||||||
| 94 | # operator). | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 138 | ▶ | sub find($) { @{$table{$_[1]} // die "op $_[1]" } } | ||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=7.2
Condition50%1/2
$table{$_[1]} // die("op $_[1]")
MC/DC0%0/1
$table{$_[1]} // die("op $_[1]")
$table{$_[1]}
Truth table50%1/2
A
A= $table{$_[1]}
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | #------------------- | ||||||||||||||||||||
| 99 | # MF::PREFIX, monadic prefix operator | ||||||||||||||||||||
| 100 | # Prefix operators process the result of the expression which follows it. | ||||||||||||||||||||
| 101 | # This is a specialization from the MF::OPERATOR type, hence shares its methods. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | package MF::PREFIX; | ||||||||||||||||||||
| 104 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 13 | ▶ | 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 | 11 | ▶ | sub child() { $_[0][1] } | ||||||||||||||||||
|
Subroutine
child: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub _compute($$) | ||||||||||||||||||||
| 114 | 11 | ▶ | { my ($self, $context, $expr) = @_; | ||||||||||||||||||
|
Subroutine
_compute: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 115 | 11 | ▶ | my $value = $self->child->_compute($context, $expr) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | or return undef; | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | # no prefix operator needs $context or $expr yet | ||||||||||||||||||||
| 119 | 11 | $value->prefix($self->operator); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | #------------------- | ||||||||||||||||||||
| 123 | # MF::INFIX, infix (dyadic) operator | ||||||||||||||||||||
| 124 | # Infix operators have two arguments. This is a specialization from the | ||||||||||||||||||||
| 125 | # MF::OPERATOR type, hence shares its methods. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | package MF::INFIX; | ||||||||||||||||||||
| 128 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 13 | ▶ | use base 'MF::OPERATOR'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # method left(): Returns the AST left from the infix operator. | ||||||||||||||||||||
| 135 | 111 | ▶ | sub left() { $_[0][1] } | ||||||||||||||||||
|
Subroutine
left: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | # method right(): Returns the AST right from the infix operator. | ||||||||||||||||||||
| 138 | 111 | ▶ | sub right() { $_[0][2] } | ||||||||||||||||||
|
Subroutine
right: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | my %comparison = ( | ||||||||||||||||||||
| 141 | 0 | ▶ | '<' => [ '<=>', sub { $_[0] < 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 142 | 0 | ▶ | '<=' => [ '<=>', sub { $_[0] <= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 143 | 0 | ▶ | '==' => [ '<=>', sub { $_[0] == 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 144 | 0 | ▶ | '!=' => [ '<=>', sub { $_[0] != 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 145 | 0 | ▶ | '>=' => [ '<=>', sub { $_[0] >= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 146 | 0 | ▶ | '>' => [ '<=>', sub { $_[0] > 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 147 | 0 | ▶ | 'lt' => [ 'cmp', sub { $_[0] < 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 148 | 0 | ▶ | 'le' => [ 'cmp', sub { $_[0] <= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 149 | 0 | ▶ | 'eq' => [ 'cmp', sub { $_[0] == 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 150 | 0 | ▶ | 'ne' => [ 'cmp', sub { $_[0] != 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 151 | 0 | ▶ | 'ge' => [ 'cmp', sub { $_[0] >= 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 152 | 0 | ▶ | 'gt' => [ 'cmp', sub { $_[0] > 0 } ], | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | ▶ | sub _compare_ops { keys %comparison } | ||||||||||||||||||
|
Subroutine
_compare_ops: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub _compute($$) | ||||||||||||||||||||
| 158 | 111 | ▶ | { my ($self, $context, $expr) = @_; | ||||||||||||||||||
|
Subroutine
_compute: called CC=5 SCAR=20.3
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 111 | ▶ | my $left = $self->left->_compute($context, $expr) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | or return undef; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 111 | ▶ | my $right = $self->right->_compute($context, $expr) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 164 | or return undef; | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | # Comparison operators are all implemented via a space-ship, when available. | ||||||||||||||||||||
| 167 | # Otherwise, the usual track is taken. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 111 | my $op = $self->operator; | |||||||||||||||||||
| 170 | 111 | ▶ | if(my $rewrite = $comparison{$op}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | { my ($spaceship, $compare) = @$rewrite; | |||||||||||||||||||
| 172 | 0 | ▶ | if(my $result = $left->infix($spaceship, $right, $context, $expr)) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 0 | { return MF::BOOLEAN->new(undef, $compare->($result->value)); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 111 | $left->infix($op, $right, $context, $expr); | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | #------------------- | ||||||||||||||||||||
| 181 | # MF::FORMULAR, temporary wrapper for a formular | ||||||||||||||||||||
| 182 | # When a NAME has been looked-up at the left side of an infix operator, or as | ||||||||||||||||||||
| 183 | # child of a prefix operator, it will become this. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | package MF::Operator; | ||||||||||||||||||||
| 186 | 13 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 187 | 13 | $VERSION = '0.10'; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 13 | 1; | |||||||||||||||||||