Forum
Using --debug-array with arrays passed to functions - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16)
+---- Thread: Using --debug-array with arrays passed to functions (/showthread.php?tid=1036)



Using --debug-array with arrays passed to functions - georgeo - 03-09-2021

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,


RE: Using --debug-array with arrays passed to functions - RandomiserUsr - 03-09-2021

(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"


RE: Using --debug-array with arrays passed to functions - georgeo - 03-10-2021

Hi RandomiserUser,

Thanks again. So, are arrays (maybe anything that is passed by reference) globally defined? That is, can functions and subroutines access the content of arrays defined in the main program, even if they are not explicitly passed in as parameters?

I think your version will only work for the one string "myString()", whereas my (admittedly pointless) function could be used with any string array.

Thanks again,
Georgeo.


RE: Using --debug-array with arrays passed to functions - boriel - 03-10-2021

(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,

This crash is a bug. The compiler should never crash (if there's an error in your code it should report the syntax/semantic error and exit gracefully).

I've been able to reproduce it.
Will fix it and let you know.


RE: Using --debug-array with arrays passed to functions - georgeo - 03-11-2021

(03-10-2021, 10:20 PM)boriel Wrote: This crash is a bug. The compiler should never crash (if there's an error in your code it should report the syntax/semantic error and exit gracefully).

I've been able to reproduce it.
Will fix it and let you know.

Ah, okay. Thanks for your help.