Skip to content

Commit

Permalink
bc: array name versus digits (#523)
Browse files Browse the repository at this point in the history
* bc: array name versus digits

* Array names are allowed to contain numeric digits but cannot start with a digit: ary2 is valid but 2ary is not
* Assigning then fetching an array value didn't work as expected
* I got a hint when adding debug statement to the =P instruction: "name[[]] idx[] varName[ary2[]1] at bc.y line 982, <STDIN> line 2." 
* The regex did not match because varName is "ary2[]1"; the regex expected that a digit would only be found after the "[]" substring
* Make the code more correct by splitting on "[]" to get the array name "ary2" and the index of "1"
* varName string is built earlier by the P instruction

%perl bc -d # before patch
ary2[0] = 1
instruction: N, 0
instruction: N, 1
instruction: P, ary2
instruction: =P
ary2[0]
instruction: N, 0
instruction: p, ary2
0

* bc: fix implicit print

* When looping, setting $return=1 in =v instruction caused the loop to terminate early
* Example input: for (i=1; i<10; i++) print i
* Instead, take a hint from FOR-COND instruction and set $return=3 for suppressing print
* This allows the simple statement "x=1" to not print
* Apply the same to =P instruction, where the $return=1 code was copied from

* no implicit print for "break"

* Set $return=1 in BREAK instruction to match RETURN
* In a loop with nested break, this stops the break from printing 0
* Example input: for ( v=0; v<5; v++) { print v; if (v>2) break }; print "\n"
  • Loading branch information
mknos committed Mar 26, 2024
1 parent 6f5aeb8 commit 512139e
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions bin/bc
Expand Up @@ -2519,15 +2519,14 @@ sub exec_stmt
$sym_table{$varName} = { type => 'var',
value => $value };
push(@ope_stack, $value);
$return = 1; # do not print result
$return = 3; # do not print result
next INSTR;

} elsif($_ eq '=P') {

my $varName = pop(@ope_stack);
my $value = pop(@ope_stack);
my ($name, $idx) = ($varName =~ /([a-z]+)\[\](\d+)/);

my ($name, $idx) = split /\[\]/, $varName, 2;
$name .= '[]';
unless (defined($sym_table{$name})
and $sym_table{$name}{'type'} eq 'array')
Expand All @@ -2538,7 +2537,7 @@ sub exec_stmt
$sym_table{$name}{'value'}[$idx] = { type => 'var',
value => $value };
push(@ope_stack, $value);
$return = 1; # do not print result
$return = 3; # do not print result
next INSTR;

} elsif($_ eq 'IF') {
Expand Down Expand Up @@ -2731,7 +2730,7 @@ sub exec_stmt

# debug {"breaking.\n"};

$return = 2;
$return = 1;
push(@ope_stack, 0);

last INSTR;
Expand Down

0 comments on commit 512139e

Please sign in to comment.