Menu

#6 Array items above 0..10 not accessible is some cases

v1.0 (example)
open
nobody
arrays (1)
5
2017-09-25
2017-09-13
Marcos Cruz
No

During the development of a project in bwBASIC, I've found a strange problem
that seems a bug of the interpreter: some times array items above range 0..10
can not be accessed, depending on the position of the corresponding dim, ie.
in a sub, a subroutine or the main flow.

I'm running bwBASIC 3.20 on Raspbian.

I've written a test program to isolate the error conditions. I think it's
self-explanatory:

' Bug in bwBASIC?

' In some cases, only the first 11 items (0..10) of arrays can be
' accessed.

' By Marcos Cruz (programandala.net)

' 2017-09-13

option terminal ansi
option base 0

cls
print "Tests:"
print "0) DIM in SUB:";tab(50);"fails"
print "1) DIM in subroutine:";tab(50);"works fine"
print "2) DIM in main flow:";tab(50);"works fine"
print "3) DIM in subroutine called from SUB:";tab(50);"fails"

repeat
    input "Choose your option (0..3) ",test%
until test%>=0 and test%<=3

cls
print "Test: ";

select case test%

  case 0

    print "DIM in SUB"
    call init0

  case 1

    print "DIM in subroutine"
    gosub init1

  case 2

    print "DIM in main flow"
    let terms%=end_terms_data-terms_data-1
    print "terms = ";terms%
    dim term$(terms%-1)

  case 3

    print "DIM in subroutine called from SUB"
    call init3

end select

call define_terms

end

sub init0

  let terms%=end_terms_data-terms_data-1
  print "terms = ";terms%
  dim term$(terms%-1)

  end sub

sub init3

  gosub init1

  end sub

init1:

  let terms%=end_terms_data-terms_data-1
  print "terms = ";terms%
  dim term$(terms%-1)

  return

sub define_terms

  let x%=base

  terms_data:
  call define_term("term-00")
  call define_term("term-01")
  call define_term("term-02")
  call define_term("term-03")
  call define_term("term-04")
  call define_term("term-05")
  call define_term("term-06")
  call define_term("term-07")
  call define_term("term-08")
  call define_term("term-09")
  call define_term("term-10")
  call define_term("term-11")
  call define_term("term-12")
  call define_term("term-13")
  call define_term("term-14")
  call define_term("term-15")
  call define_term("term-16")
  call define_term("term-17")
  end_terms_data:

  end sub

sub define_term(a_term$)

  print "About to store '";a_term$;"' into array position ";x%
  let term$(x%)=a_term$
  let x%=x%+1

  end sub

Am I missing something? I've found no clue in the documentation. Could
someone confirm this is a bug?

Thank you

Discussion

  • AF5NE

    AF5NE - 2017-09-25

    DIM variables in a SUB (or FUNCTION) are local to the SUB (or FUNCTION). This is by design.

    By the way, a GOSUB (or GOTO) from within a SUB (or FUNCTION) to a line outside that SUB (or FUNCTION), such as the "gosub init1" from within "sub init3", is not supported.

     
    • Marcos Cruz

      Marcos Cruz - 2017-09-25

      DIM variables in a SUB (or FUNCTION) are local to the SUB (or FUNCTION). This is by design.

      Ok. Thank you. As local is not supported (in the Bywater variant), and the documentation does not explain that feature, I didn't suspect arrays are treated differently from ordinary variables.

      By the way, a GOSUB (or GOTO) from within a SUB (or FUNCTION) to a line outside that SUB (or FUNCTION), such as the "gosub init1" from within "sub init3", is not supported.

      That restriction is not mentioned in the glossary. But anyway it works for me:

      print "About to call init3"
      call init3
      print "Done"
      end
      
      sub init3
        gosub init1
        end sub
      
      init1:
        print "I'm init1!"
        return
      
       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.