[LON-CAPA-cvs] cvs: loncom /homework/math_parser CalcEnv.pm ENode.pm Parser.pm

damieng damieng at source.lon-capa.org
Tue Jun 30 13:42:15 EDT 2015


damieng		Tue Jun 30 17:42:15 2015 EDT

  Modified files:              
    /loncom/homework/math_parser	CalcEnv.pm ENode.pm Parser.pm 
  Log:
  math parser: changed syntax to avoid compatibility problems with Perl 5.8.8
  
Index: loncom/homework/math_parser/CalcEnv.pm
diff -u loncom/homework/math_parser/CalcEnv.pm:1.1 loncom/homework/math_parser/CalcEnv.pm:1.2
--- loncom/homework/math_parser/CalcEnv.pm:1.1	Mon Jun 29 15:42:13 2015
+++ loncom/homework/math_parser/CalcEnv.pm	Tue Jun 30 17:42:14 2015
@@ -36,9 +36,12 @@
 # @param {boolean} unit_mode
 ##
 sub new {
-    my $class = shift;
+    my ($class, $unit_mode) = @_;
+    if (!defined $unit_mode) {
+        $unit_mode = 0;
+    }
     my $self = {
-        _unit_mode => shift // 0,
+        _unit_mode => $unit_mode,
     };
     if ($self->{_unit_mode}) {
         $self->{_units} = Units->new();
Index: loncom/homework/math_parser/ENode.pm
diff -u loncom/homework/math_parser/ENode.pm:1.2 loncom/homework/math_parser/ENode.pm:1.3
--- loncom/homework/math_parser/ENode.pm:1.2	Mon Jun 29 17:47:00 2015
+++ loncom/homework/math_parser/ENode.pm	Tue Jun 30 17:42:14 2015
@@ -50,13 +50,16 @@
 # @param {interval_type} - The interval type, NOT_AN_INTERVAL | OPEN_OPEN | OPEN_CLOSED | CLOSED_OPEN | CLOSED_CLOSED
 ##
 sub new {
-    my $class = shift;
+    my ($class, $type, $op, $value, $children, $interval_type) = @_;
+    if (!defined $interval_type) {
+        $interval_type = NOT_AN_INTERVAL;
+    }
     my $self = {
-        _type => shift,
-        _op => shift,
-        _value => shift,
-        _children => shift,
-        _interval_type => shift // NOT_AN_INTERVAL,
+        _type => $type,
+        _op => $op,
+        _value => $value,
+        _children => $children,
+        _interval_type => $interval_type,
     };
     bless $self, $class;
     return $self;
@@ -307,15 +310,15 @@
                 die CalcException->new("Missing parameter for function [_1].", $fname);
             }
             my ($q1, $q2);
-            if ($fname ~~ ['pow', 'sqrt', 'abs', 'exp', 'ln', 'log', 'log10', 'factorial',
+            if (string_in_array(['pow', 'sqrt', 'abs', 'exp', 'ln', 'log', 'log10', 'factorial',
                     'mod', 'sgn', 'ceil', 'floor', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan',
-                    'atan2', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh']) {
+                    'atan2', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh'], $fname)) {
                 $q1 = $children[1]->calc($env);
                 if (!$q1->isa(Quantity)) {
                     die CalcException->new("The [_1] function is not implemented for this type.", $fname);
                 }
             }
-            if ($fname ~~ ['pow', 'mod', 'atan2']) {
+            if (string_in_array(['pow', 'mod', 'atan2'], $fname)) {
                 if (!defined $children[2]) {
                     die CalcException->new("Missing parameter for function [_1].", $fname);
                 }
@@ -634,7 +637,7 @@
                 "Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma",
                 "Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega",
             );
-            if ($name ~~ @greek) {
+            if (string_in_array(\@greek, $name)) {
                 return('\\'.$name);
             } elsif ($name eq "hbar") {
                 return("\\hbar");
@@ -1002,5 +1005,21 @@
     }
 }
 
+##
+# Tests if a string is in an array (using eq) (to avoid using $value ~~ @array)
+# @param {Array<string>} 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__
Index: loncom/homework/math_parser/Parser.pm
diff -u loncom/homework/math_parser/Parser.pm:1.1 loncom/homework/math_parser/Parser.pm:1.2
--- loncom/homework/math_parser/Parser.pm:1.1	Mon Jun 29 15:42:13 2015
+++ loncom/homework/math_parser/Parser.pm	Tue Jun 30 17:42:14 2015
@@ -39,10 +39,16 @@
 # @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 @@
                 ($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 @@
     return $root;
 }
 
+##
+# Tests if a string is in an array (using eq) (to avoid using $value ~~ @array)
+# @param {Array<string>} 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__




More information about the LON-CAPA-cvs mailing list