Files
Basys3Terminal/basic/test-strarr.py
T
brad ArantandClaude Opus 5.5 c553824df0 BASIC: string arrays (DIM A$(n), one or two subscripts)
Elements hold up to 79 characters, 80 bytes each, at most 255 per array;
undeclared ones get 0..10 like numeric arrays. All string-variable uses
now go through one str_ref(), which more than pays for the feature in
ROM. The arena gives up 156 bytes for the larger array table so nothing
after it moves. Adds basic/test-strarr.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 12:11:33 -08:00

80 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Try BASIC's string arrays in tools/z80emu.py, with the screen kept by pyte.
python3 test-strarr.py
"""
import importlib.util
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
te = importlib.util.module_from_spec(spec)
spec.loader.exec_module(te)
CR = te.CR
def main():
b = te.Basic()
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def run(prog):
b.type(b"NEW" + CR)
for line in prog.strip().splitlines():
b.type(line.encode() + CR)
b.type(b"RUN" + CR)
return "\n".join(l.rstrip() for l in b.screen_text().display).split("RUN")[-1]
b.type(b"")
out = run('''
10 DIM N$(4), A(3)
20 FOR I = 0 TO 4: READ N$(I): NEXT I
30 A(2) = 7: A$ = "PLAIN"
40 FOR I = 4 TO 0 STEP -1: PRINT N$(I);",";: NEXT I
50 PRINT: PRINT A$; A(2); LEN(N$(1)); " "; LEFT$(N$(3), 2)
60 DATA RED, GREEN, BLUE, "YEL, LOW", WHITE
''')
check("DIM, READ and PRINT a string array", "WHITE,YEL, LOW,BLUE,GREEN,RED," in out)
check("plain A$ and A() kept apart", "PLAIN 7 5 YE" in out)
out = run('''
10 DIM B$(2,3)
20 B$(1,2) = "MID": B$(2,3) = B$(1,2) + "DLE"
30 PRINT B$(2,3); "/"; B$(0,0); "/"; LEN(B$(0,1))
40 C$(10) = "AUTO": PRINT C$(10)
50 IF B$(1,2) = "MID" THEN PRINT "EQUAL"
''')
check("two subscripts and joining", "MIDDLE// 0" in out)
check("undeclared string array gets 0..10", "AUTO" in out)
check("comparing an element", "EQUAL" in out)
out = run('''
10 INPUT "NAME"; N$(3)
20 PRINT "HI "; N$(3)
''')
b.type(b"ZED" + CR)
check("INPUT into an element", "HI ZED" in "\n".join(l.rstrip() for l in b.screen_text().display))
out = run('10 LINE INPUT L$(1): PRINT "<"; L$(1); ">"')
b.type(b"A, B" + CR)
check("LINE INPUT into an element", "<A, B>" in "\n".join(l.rstrip() for l in b.screen_text().display))
out = run('10 DIM Q$(2): Q$(3) = "X"')
check("subscript out of range", "SUBSCRIPT" in out.upper())
out = run('10 DIM Z$(300)')
check("too many elements is an error", "SUBSCRIPT" in out.upper())
out = run('10 DIM Z$(200), Y$(200)')
check("running out of memory is an error", "MEMORY" in out.upper())
out = run('10 DIM Z$(200): Z$(200) = "LAST": PRINT Z$(200)')
check("a 201-element string array fits", "LAST" in out)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()