| 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 | package Math::Formula;{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 28 | our $VERSION = '0.18'; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 28 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 28 | ▶ | use Log::Report 'math-formula'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 28 | ▶ | use Math::Formula::Token (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 28 | ▶ | use Math::Formula::Type (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 28 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | #-------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | #-------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | sub new(%) | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 104 | ▶ | { my ($class, $name, $expr, %self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 104 | $self{_name} = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 104 | $self{_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 104 | (bless {}, $class)->init(\%self); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | sub init($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 104 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 104 | ▶ | my $name = $self->{MSBE_name} = $args->{_name} or panic "every formular requires a name"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 104 | ▶ | my $expr = $args->{_expr} or panic "every formular requires an expression"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 104 | my $returns = $self->{MSBE_returns} = $args->{returns}; | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 104 | ▶ | if(ref $expr eq 'SCALAR') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | { $expr = MF::STRING->new(undef, $$expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ▶ | elsif(! ref $expr && $returns && $returns->isa('MF::STRING')) | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
not ref $expr and $returns and $returns->isa("MF::STRING")
not ref $expr and $returns
MC/DC0%0/3
not ref $expr and $returns and $returns->isa("MF::STRING")
not ref $expr
$returns
$returns->isa("MF::STRING")
Truth table75%3/4
not(A) and B and C
A= not ref $exprB= $returnsC= $returns->isa("MF::STRING")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | { $expr = MF::STRING->new(undef, $expr); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 104 | $self->{MSBE_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 104 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | #-------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 23 | ▶ | sub name() { $_[0]->{MSBE_name} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 306 | ▶ | sub expression() { $_[0]->{MSBE_expr} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
expression: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 282 | ▶ | sub returns() { $_[0]->{MSBE_returns} } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
returns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | sub tree($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 268 | ▶ | { my ($self, $expression) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tree: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 268 | ▶ | $self->{MSBE_ast} ||= $self->_build_ast($self->_tokenize($expression), 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"MSBE_ast"} ||= $self->_build_ast($self->_tokenize($expression), 0)
MC/DC0%0/2
$self->{"MSBE_ast"} ||= $self->_build_ast($self->_tokenize($expression), 0)
$self->{"MSBE_ast"}
$self->_build_ast($self->_tokenize($expression), 0)
Truth table66%2/3
A ||= B
A= $self->{"MSBE_ast"}B= $self->_build_ast($self->_tokenize($expression), 0)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # For testing only: to load a new expression without the need to create | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # a new object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | sub _test($$) | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 214 | ▶ | { my ($self, $expr) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_test: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 214 | $self->{MSBE_expr} = $expr; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 214 | delete $self->{MSBE_ast}; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ### | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ### PARSER | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ### | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 28 | my $match_int = MF::INTEGER->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 28 | my $match_float = MF::FLOAT->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 28 | my $match_name = MF::NAME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 28 | my $match_date = MF::DATE->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 28 | my $match_time = MF::TIME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 28 | my $match_tz = MF::TIMEZONE->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 28 | my $match_dt = MF::DATETIME->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 28 | my $match_dur = MF::DURATION->_match; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 28 | my $match_op = join '|', | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | qw{ // -> }, '[?*\/+\-#~.%]', | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | qw{ =~ !~ <=> <= >= == != < > }, # order is important | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | qw{ :(?![0-9][0-9]) (?<![0-9][0-9]): }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ( map "$_\\b", qw/ and or not xor exists like unlike cmp lt le eq ne ge gt/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | sub _tokenize($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 301 | ▶ | { my ($self, $s) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 301 | our @t = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 301 | my $parens_open = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 28 | ▶ | use re 'eval'; #XXX needed with newer than 5.16 perls? | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 301 | $s =~ m/ ^ | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | (?: \s* | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | (?| \# (?: \s [^\n\r]+ | $ ) \ | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 47 | | ( true\b | false\b ) (?{ push @t, MF::BOOLEAN->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | | ( \" (?: \\\" | [^"] )* \" ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 100 | (?{ push @t, MF::STRING->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | | ( \' (?: \\\' | [^'] )* \' ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | (?{ push @t, MF::STRING->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 39 | | ( $match_dur ) (?{ push @t, MF::DURATION->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 13 | | ( $match_tz ) (?{ push @t, MF::TIMEZONE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 313 | | ( $match_op ) (?{ push @t, MF::OPERATOR->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 82 | | ( $match_name ) (?{ push @t, MF::NAME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | 31 | | ( $match_dt ) (?{ push @t, MF::DATETIME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 23 | | ( $match_date ) (?{ push @t, MF::DATE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 121 | 21 | | ( $match_time ) (?{ push @t, MF::TIME->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 42 | | ( $match_float ) (?{ push @t, MF::FLOAT->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 166 | | ( $match_int ) (?{ push @t, MF::INTEGER->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 5 | | \( (?{ push @t, MF::PARENS->new('(', ++$parens_open) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | | \) (?{ push @t, MF::PARENS->new(')', $parens_open--) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | | \$ ([1-9][0-9]*) (?{ push @t, MF::CAPTURE->new($+) }) | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | | $ | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | | (.+) (?{ error __x"expression '{name}', failed at '{where}'", | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | name => $self->name, where => $+ }) | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | )+ \z /sxo; | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 301 | ▶ | ! $parens_open | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | or error __x"expression '{name}', parenthesis do not match", name => $self->name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 301 | \@t; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | sub _build_ast($$) | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 566 | ▶ | { my ($self, $t, $prio) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ast: called CC=23 SCAR=31.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 566 | ▶ | return shift @$t if @$t < 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | PROGRESS: | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 299 | ▶ | while(my $first = shift @$t) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 601 | ▶ | if($first->isa('MF::PARENS')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 5 | { my $level = $first->level; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 5 | my @nodes; | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 5 | ▶ | while(my $node = shift @$t) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 25 | ▶ | { last if $node->isa('MF::PARENS') && $node->level==$level; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$node->isa("MF::PARENS") and $node->level == $level
MC/DC100%2/2
$node->isa("MF::PARENS") and $node->level == $level
$node->isa("MF::PARENS")
$node->level == $level
Truth table100%3/3
A and B
A= $node->isa("MF::PARENS")B= $node->level == $level
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 20 | push @nodes, $node; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 5 | $first = $self->_build_ast(\@nodes, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 5 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 596 | ▶ | if(ref $first eq 'MF::OPERATOR') # unresolved operator | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 38 | { my $op = $first->token; | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 38 | ▶ | if($op eq '#' || $op eq '.') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$op eq "#" or $op eq "."
MC/DC100%2/2
$op eq "#" or $op eq "."
$op eq "#"
$op eq "."
Truth table100%3/3
A or B
A= $op eq "#"B= $op eq "."
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | { # Fragments and Methods are always infix, but their left-side arg | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # can be left-out. As PREFIX, they would be RTL but we need LTR | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 6 | unshift @$t, $first; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | $first = MF::NAME->new(''); | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 32 | ▶ | my $next = $self->_build_ast($t, $prio) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | or error __x"expression '{name}', monadic '{op}' not followed by anything useful", | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | name => $self->name, op => $op; | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 32 | $first = MF::PREFIX->new($op, $next); | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 32 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 558 | ▶ | my $next = $t->[0] | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | or return $first; # end of expression | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 291 | ▶ | if(ref $next ne 'MF::OPERATOR') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | { if($next->isa('MF::TIMEZONE')) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | { # Oops, mis-parse | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 2 | unshift @$t, $next->cast('MF::INTEGER'); | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $next = MF::OPERATOR->new('+'); | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | else | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | { error __x"expression '{name}', expected infix operator but found '{type}'", name => $self->name, type => ref $next; | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 291 | my $op = $next->token; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 291 | ▶ | @$t or error __x"expression '{name}', infix operator '{op}' requires right-hand argument", name => $self->name, op => $op; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 291 | my ($next_prio, $assoc) = MF::OPERATOR->find($op); | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 291 | ▶ | return $first | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$next_prio < $prio or $next_prio == $prio and $assoc == 1
$next_prio == $prio and $assoc == 1
MC/DC66%2/3
$next_prio < $prio or $next_prio == $prio and $assoc == 1
$next_prio < $prio
$next_prio == $prio
$assoc == 1
Truth table75%3/4
A or B and C
A= $next_prio < $prioB= $next_prio == $prioC= $assoc == 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | if $next_prio < $prio | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | || ($next_prio==$prio && $assoc==MF::OPERATOR::LTR); | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 266 | ▶ | if($op eq ':') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 7 | { return $first; | |||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 259 | shift @$t; # apply the operator | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 259 | ▶ | if($op eq '?') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 7 | { my $then = $self->_build_ast($t, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 7 | my $colon = shift @$t; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 7 | ▶ | $colon && $colon->token eq ':' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$colon and $colon->token eq ":"
MC/DC0%0/2
$colon and $colon->token eq ":"
$colon
$colon->token eq ":"
Truth table33%1/3
A and B
A= $colonB= $colon->token eq ":"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | or error __x"expression '{name}', expected ':' in '?:', but got '{token}'", | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | name => $self->name, token => ($next ? $colon->token : 'end-of-line'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 7 | my $else = $self->_build_ast($t, $next_prio); | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | 7 | $first = MF::TERNARY->new($op, $first, $then, $else); | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | 7 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 252 | $first = MF::INFIX->new($op, $first, $self->_build_ast($t, $next_prio)); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 252 | redo PROGRESS; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | #-------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | sub evaluate($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 279 | ▶ | { my ($self, $context, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 279 | my $expr = $self->expression; | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 279 | ▶ | my $result | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | = ref $expr eq 'CODE' ? $self->toType($expr->($context, $self, %args)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | : ! blessed $expr ? $self->tree($expr)->compute($context) | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | : $expr->isa('Math::Formula::Type') ? $expr | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | : panic; | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # For external evaluation calls, we must follow the request | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 279 | ▶ | my $expect = $args{expect} || $self->returns; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$args{'expect'} || $self->returns
MC/DC100%2/2
$args{'expect'} || $self->returns
$args{'expect'}
$self->returns
Truth table100%3/3
A || B
A= $args{'expect'}B= $self->returns
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 279 | ▶ | $result && $expect && ! $result->isa($expect) ? $result->cast($expect, $context) : $result; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$result and $expect and not $result->isa($expect)
$result and $expect
MC/DC66%2/3
$result and $expect and not $result->isa($expect)
$result
$expect
not $result->isa($expect)
Truth table75%3/4
A and B and not(C)
A= $resultB= $expectC= not $result->isa($expect)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 28 | my %_match = map { my $match = $_->_match; ( $_ => qr/^$match$/x ) } | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | qw/MF::DATETIME MF::TIME MF::DATE MF::DURATION/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | sub toType($) | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 22 | ▶ | { my ($self, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
toType: called CC=16 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 22 | ▶ | if(blessed $data) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 8 | ▶ | { return $data if $data->isa('Math::Formula::Type'); # explicit type | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 4 | ▶ | return MF::DATETIME->new(undef, $data) if $data->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 3 | ▶ | return MF::DURATION->new(undef, $data) if $data->isa('DateTime::Duration'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | return MF::FRAGMENT->new($data->name, $data) if $data->isa('Math::Formula::Context'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | my $match = sub { my $type = shift; my $match = $type->_match; qr/^$match$/ }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ref $data eq 'SCALAR' ? MF::STRING->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | : $data =~ /^[+-]?[0-9]+$/ ? MF::INTEGER->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | : $data =~ /^[+-]?[0-9]+\./ ? MF::FLOAT->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 258 | : $data =~ /^(?:true|false)$/ ? MF::BOOLEAN->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | : ref $data eq 'Regexp' ? MF::REGEXP->new(undef, $data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | : $data =~ $_match{'MF::DATETIME'} ? MF::DATETIME->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | : $data =~ $_match{'MF::TIME'} ? MF::TIME->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | : $data =~ $_match{'MF::DATE'} ? MF::DATE->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 15 | ▶ | : $data =~ $_match{'MF::DURATION'} ? MF::DURATION->new($data) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | : $data =~ /^(['"]).*\1$/ ? MF::STRING->new($data) | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | : error __x"not an expression (string needs \\ ) for '{data}'", data => $data; | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | #-------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 28 | 1; | |||||||||||||||||||||||||||||||||||||||||||||