Files
Basys3RiscV/basic/test-strarr.py
T
brad ArantandClaude Opus 5.5 fa4752fd7a Assembler in GNU as syntax, objdump-style disassembly, UCASE$ and LCASE$
The monitor's assembler takes GNU as directives (.org, .equ, .byte, .half,
.word, .ascii, .string, .space, .align, .balign ...), # comments, 0x/0b
numbers, '\n' constants, . for the current address, << >> ^ ~, %hi/%lo and
the FP CSR short forms; the old ORG/DB/EQU/0FFH forms remain as aliases. The
disassembler prints lower case and 0x hex as objdump does. The monitor is
built with -msave-restore and puts .rodata first, to fit its ROM.
BASIC gains UCASE$ and LCASE$. Manual, README and tests updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:14:19 -08:00

97 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Try BASIC's string arrays in tools/rv32emu.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, out=""):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
if not ok and out:
print(out)
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 A$ = "Hello, World! 123"
20 PRINT UCASE$(A$); "|"; LCASE$(A$); "|"; LCASE$(UCASE$("mIxEd"))
30 B$ = CHR$(64) + CHR$(91) + CHR$(96) + CHR$(123) + CHR$(224)
40 PRINT (UCASE$(B$) = B$); (LCASE$(B$) = B$); LEN(UCASE$("")); ASC(LCASE$("Z"))
50 DIM N$(1): N$(1) = "abc": PRINT UCASE$(N$(1) + "d")
''')
check("UCASE$ and LCASE$", "HELLO, WORLD! 123|hello, world! 123|mixed" in out, out)
check("only A-Z and a-z change", "-1 -1 0 122" in out, out)
check("UCASE$ of an expression", "ABCD" in out, out)
b.type(b"LIST 20" + CR)
check("LIST spells UCASE$ and LCASE$",
'20 PRINT UCASE$(A$); "|"; LCASE$(A$); "|"; LCASE$(UCASE$("mIxEd"))' 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()