Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using --debug-array with arrays passed to functions
#2
(03-09-2021, 01:11 PM)georgeo Wrote: Hi everyone,

I would like to use the --debug-array option to check I have not used out-of-range array indices in my program. However, it appears that I need to give the compiler more information, to use the option in a program that passes arrays as function arguments.

For example, the following simple program appears to compile/ run correctly:
Code:
function pickString$( choice as ubyte,  listOfStrings() as string ) as string
    return listOfStrings(choice)
end function

dim myString$(3) as string
myString$(0) = "Zero"
myString$(1) = "One"
myString$(2) = "Two"
myString$(3) = "Three"

dim n as ubyte

for n=0 to 3
    print pickString$(n, myString$)
next n

stop

--but the compiler generates an error if I try to compile with the --debug-array option:

Code:
zxbc -t -a -B --debug-array array_bounds_ex.bas
Traceback (most recent call last):
  File "c:\opt\zxbasic-1.14.0\zxbasic\zxbc.py", line 10, in <module>
    sys.exit(libzxbc.main())  # Exit
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\libzxbc\zxbc.py", line 246, in main
    func_visitor.start()
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\arch\zx48k\translator.py", line 1401, in start
    self.visit(f)
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\ast\ast.py", line 35, in visit
    stack.append(last.send(last_result))
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\arch\zx48k\translator.py", line 196, in visit_ARGLIST
    upper = node.parent.entry.bounds[i].upper
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\symbols\boundlist.py", line 29, in __getitem__
    return self.children[key]
  File "c:\opt\zxbasic-1.14.0\zxbasic\src\ast\tree.py", line 40, in __getitem__
    return self._children[key]
IndexError: list index out of range

I imaging the problem is that the compiler can't work out the array bounds within the function, unless I tell it. Anyone know if I can modify the source, so array-bound checking will still work?

Thanks in advance,

Hi, 

I have modified your code and it prints out what you expect 

Code:
dim myString$(3) as STRING
dim n as ubyte
myString$(0) = "Zero"
myString$(1) = "One"
myString$(2) = "Two"
myString$(3) = "Three"

function pickString$( choice as ubyte,  listOfStrings() as string ) as string
    return myString$(choice)
end function

for n=0 to 3
    print pickString$(n, myString$)
next n

Actually you don't need to pass in the listOfStrings() as string just this is needed:-

"function pickString$( choice as ubyte ) as string"
Reply


Messages In This Thread
RE: Using --debug-array with arrays passed to functions - by RandomiserUsr - 03-09-2021, 09:25 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)