--- loncom/homework/math_parser/Parser.pm 2015/06/29 15:42:13 1.1 +++ loncom/homework/math_parser/Parser.pm 2015/06/30 17:42:14 1.2 @@ -39,10 +39,16 @@ use aliased 'Apache::math_parser::Tokeni # @optional {boolean} unit_mode - handle only numerical expressions with units (no variable) ## sub new { - my $class = shift; + my ($class, $implicit_operators, $unit_mode) = @_; + if (!defined $implicit_operators) { + $implicit_operators = 0; + } + if (!defined $unit_mode) { + $unit_mode = 0; + } my $self = { - _implicit_operators => shift // 0, - _unit_mode => shift // 0, + _implicit_operators => $implicit_operators, + _unit_mode => $unit_mode, _defs => Definitions->new(), }; $self->{_defs}->define(); @@ -200,18 +206,18 @@ sub addHiddenOperators { ($token_type == Token->NAME && $next_token_type == Token->NAME) || ($token_type == Token->NUMBER && $next_token_type == Token->NAME) || ($token_type == Token->NUMBER && $next_token_type == Token->NUMBER) || - ($token_type == Token->NUMBER && $next_token_value ~~ ["(","[","{"]) || + ($token_type == Token->NUMBER && string_in_array(["(","[","{"], $next_token_value)) || # ($token_type == Token->NAME && $next_token_value eq "(") || # name ( could be a function call - ($token_value ~~ [")","]","}"] && $next_token_type == Token->NAME) || - ($token_value ~~ [")","]","}"] && $next_token_type == Token->NUMBER) || - ($token_value ~~ [")","]","}"] && $next_token_value eq "(") + (string_in_array([")","]","}"], $token_value) && $next_token_type == Token->NAME) || + (string_in_array([")","]","}"], $token_value) && $next_token_type == Token->NUMBER) || + (string_in_array([")","]","}"], $token_value) && $next_token_value eq "(") ) { # support for things like "(1/2) (m/s)" is complex... my $units = ($self->unit_mode && !$in_units && - ($token_type == Token->NUMBER || $token_value ~~ [")","]","}"]) && + ($token_type == Token->NUMBER || string_in_array([")","]","}"], $token_value)) && ($next_token_type == Token->NAME || - ($next_token_value ~~ ["(","[","{"] && scalar(@{$self->tokens}) > $i + 2 && + (string_in_array(["(","[","{"], $next_token_value) && scalar(@{$self->tokens}) > $i + 2 && $self->tokens->[$i + 2]->type == Token->NAME))); if ($units) { my( $test_token, $index_test); @@ -276,5 +282,21 @@ sub parse { return $root; } +## +# Tests if a string is in an array (using eq) (to avoid using $value ~~ @array) +# @param {Array} array - reference to the array of strings +# @param {string} value - the string to look for +# @returns 1 if found, 0 otherwise +## +sub string_in_array { + my ($array, $value) = @_; + foreach my $v (@{$array}) { + if ($v eq $value) { + return 1; + } + } + return 0; +} + 1; __END__