Skip to content

Commit

Permalink
bc: array assignment incorrectly prints (#452)
Browse files Browse the repository at this point in the history
* In exec_stmt(), instruction 'p' takes an array name and index number, then pushes the array element onto the stack (implicit print)
* The default value for array elements is 0
* The instruction '=P' assignd a value from the stack into an array element
* Make the code for 'p' simpler by appending '[]' to $name, as done for '=P' instruction
* For instruction '=P' the value being assigned to array should not be printed (this is controlled by $return variable)
* For input "a[9] = 123; a[9];" the value of 123 should be printed once, for the 2nd statement
* Tested this input against GNU and OpenBSD versions
  • Loading branch information
mknos authored Feb 12, 2024
1 parent 8a055a3 commit da1ef48
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions bin/bc
Original file line number Diff line number Diff line change
Expand Up @@ -2496,16 +2496,17 @@ sub exec_stmt
}

# debug {"p: $name, $idx.\n"};
unless (defined($sym_table{$name.'[]'})
and $sym_table{$name.'[]'}{'type'} eq 'array') {
$name .= '[]';
unless (defined($sym_table{$name})
and $sym_table{$name}{'type'} eq 'array') {

$sym_table{$name.'[]'} = { type => 'array'};
$sym_table{$name} = { type => 'array'};
}
unless ($sym_table{$name.'[]'}{'value'}[$idx]) {
$sym_table{$name.'[]'}{'value'}[$idx] = { type => 'var',
unless ($sym_table{$name}{'value'}[$idx]) {
$sym_table{$name}{'value'}[$idx] = { type => 'var',
value => 0 };
}
push(@ope_stack, $sym_table{$name.'[]'}{'value'}[$idx]{'value'});
push(@ope_stack, $sym_table{$name}{'value'}[$idx]{'value'});
next INSTR;

} elsif($_ eq '=V') {
Expand Down Expand Up @@ -2535,6 +2536,7 @@ sub exec_stmt
$sym_table{$name}{'value'}[$idx] = { type => 'var',
value => $value };
push(@ope_stack, $value);
$return = 1; # do not print result
next INSTR;

} elsif($_ eq 'IF') {
Expand Down

0 comments on commit da1ef48

Please sign in to comment.