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