Skip to content

Commit ae64876

Browse files
authored
Merge pull request #1277 from Alex-Jordan/parens-only
only use parentheses (not brackets) for Formula strings
2 parents 1124d4f + 1a68f66 commit ae64876

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

lib/Parser/Context/Default.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ $flags = {
434434
reduceConstants => 1, # 1 = automatically combine constants
435435
reduceConstantFunctions => 1, # 1 = compute function values of constants
436436
showExtraParens => 1, # 1 = add useful parens, 2 = make things painfully unambiguous
437+
stringifyNoBrackets => 0, # 1 = only use parentheses not brackets when stringifying
437438
formatStudentAnswer => 'evaluated', # or 'parsed' or 'reduced'
438439
allowMissingOperands => 0, # 1 is used by Typeset context
439440
allowMissingFunctionInputs => 0, # 1 is used by Typeset context

lib/Parser/Item.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ sub canBeInUnion {0}
113113
sub isSetOfReals { (shift)->type =~ m/^(Interval|Union|Set)$/ }
114114

115115
#
116-
# Add parens to an expression (alternating the type of paren)
116+
# Add parens to an expression (alternating the type of paren unless stringifyNoBrackets is set)
117117
#
118118
sub addParens {
119119
my $self = shift;
120120
my $string = shift;
121-
if ($string =~ m/^[^\[]*\(/) { return '[' . $string . ']' }
121+
return '[' . $string . ']' if $string =~ m/^[^\[]*\(/ && !$self->context->flag('stringifyNoBrackets');
122122
return '(' . $string . ')';
123123
}
124124

lib/Plots/Data.pm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ sub function_string {
260260
my $format = $formula->context->{format}{number};
261261
$formula->context->flags->set(showExtraParens => 2);
262262
$formula->context->{format}{number} = "%f#";
263-
my $func = $formula->string;
263+
# Get no bracket string for $formula
264+
my $func = $formula . "";
264265
$func =~ s/\s//g;
265266
$formula->context->flags->set(showExtraParens => $extraParens);
266267
$formula->context->{format}{number} = $format;
@@ -368,8 +369,6 @@ sub function_string {
368369
}
369370
}
370371

371-
$out =~ s/\[/(/g;
372-
$out =~ s/\]/)/g;
373372
return $out;
374373
}
375374

lib/Value/Formula.pm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ sub _dot {
112112
$l->SUPER::_dot($r, $flag);
113113
}
114114

115-
sub pdot { '(' . (shift->stringify) . ')' }
115+
sub pdot {
116+
my $self = shift;
117+
my $nobrackets = $self->context->flag('stringifyNoBrackets');
118+
$self->context->flags->set(stringifyNoBrackets => 1);
119+
my $string = '(' . ($self->stringify) . ')';
120+
$self->context->flags->set(stringifyNoBrackets => $nobrackets);
121+
return $string;
122+
}
116123

117124
#
118125
# Call the Parser::Function call function

0 commit comments

Comments
 (0)