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>
This commit is contained in:
1 parent
2d1be15786
commit
c553824df0
7 files changed
+2269
-2170
No files matched your search
Binary file not shown.
+40
-36
@@ -221,7 +221,7 @@ static const char *const kw[] = {
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------- memory */
|
||||
#define ARENA 20820 /* program text grows up, arrays grow down;
|
||||
#define ARENA 20664 /* program text grows up, arrays grow down;
|
||||
F000-F3F7 is left free for machine code */
|
||||
#define STRMAX 80
|
||||
#define NVARS (26 * 11)
|
||||
@@ -235,8 +235,8 @@ static u8 *arr_low; /* lowest byte used by arrays */
|
||||
static float vars[NVARS];
|
||||
static char svars[26][STRMAX];
|
||||
|
||||
struct array { float *p; u16 d1, d2; };
|
||||
static struct array arrays[26];
|
||||
struct array { u8 *p; u16 d1, d2; };
|
||||
static struct array arrays[52]; /* A()-Z(), then A$()-Z$() */
|
||||
|
||||
struct fn { u8 *body; u8 var; };
|
||||
static struct fn fns[26];
|
||||
@@ -690,7 +690,24 @@ static i16 to_int(float v)
|
||||
return (i16)v;
|
||||
}
|
||||
|
||||
static float *array_elem(u8 a)
|
||||
/* Give array a (26 and up for strings) room for subscripts 0..d1, 0..d2. */
|
||||
static void arr_alloc(u8 a, u16 d1, u16 d2)
|
||||
{
|
||||
struct array *ar = &arrays[a];
|
||||
u16 n = (d1 + 1) * (d2 + 1), sz;
|
||||
u8 *np;
|
||||
if ((i16)d1 < 0 || (i16)d2 < 0 || n > (a < 26 ? 6000 : 255)) error(E_SUBSCRIPT);
|
||||
sz = n * (a < 26 ? sizeof(float) : STRMAX);
|
||||
np = arr_low - sz;
|
||||
if (np <= prog_end + 1) error(E_MEMORY);
|
||||
arr_low = np;
|
||||
memset(np, 0, sz);
|
||||
ar->p = np;
|
||||
ar->d1 = d1;
|
||||
ar->d2 = d2;
|
||||
}
|
||||
|
||||
static u8 *array_elem(u8 a)
|
||||
{
|
||||
struct array *ar = &arrays[a];
|
||||
i16 i, j = 0;
|
||||
@@ -700,18 +717,18 @@ static float *array_elem(u8 a)
|
||||
two = accept(',');
|
||||
if (two) j = to_int(expr());
|
||||
expect(')');
|
||||
if (!ar->p) { /* undeclared: dimension 10 (0..10) */
|
||||
u16 n = two ? 11 * 11 : 11;
|
||||
u8 *np = arr_low - n * sizeof(float);
|
||||
if (np <= prog_end + 1) error(E_MEMORY);
|
||||
arr_low = np;
|
||||
ar->p = (float *)np;
|
||||
ar->d1 = 10;
|
||||
ar->d2 = two ? 10 : 0;
|
||||
memset(np, 0, n * sizeof(float));
|
||||
}
|
||||
if (!ar->p) arr_alloc(a, 10, two ? 10 : 0); /* undeclared: 0..10 */
|
||||
if (i < 0 || i > (i16)ar->d1 || j < 0 || j > (i16)ar->d2) error(E_SUBSCRIPT);
|
||||
return ar->p + (u16)i * (ar->d2 + 1) + j;
|
||||
return ar->p + ((u16)i * (ar->d2 + 1) + j) * (a < 26 ? sizeof(float) : STRMAX);
|
||||
}
|
||||
|
||||
/* A string variable or string array element (tp on its letter). */
|
||||
static char *str_ref(void)
|
||||
{
|
||||
u8 a = peekc() - 'A';
|
||||
tp += 2;
|
||||
if (peekc() == '(') return (char *)array_elem(26 + a);
|
||||
return svars[a];
|
||||
}
|
||||
|
||||
/* A numeric variable or array element as an lvalue. */
|
||||
@@ -721,7 +738,7 @@ static float *num_ref(void)
|
||||
if (is_alpha(peekc()) && tp[1] == '(') {
|
||||
v = *tp - 'A';
|
||||
tp++;
|
||||
return array_elem(v);
|
||||
return (float *)array_elem(v);
|
||||
}
|
||||
v = var_name();
|
||||
if (v == 255) error(E_SYNTAX);
|
||||
@@ -960,8 +977,7 @@ static void str_prim(char *out)
|
||||
return;
|
||||
}
|
||||
if (is_alpha(c) && tp[1] == '$') {
|
||||
strcpy(out, svars[c - 'A']);
|
||||
tp += 2;
|
||||
strcpy(out, str_ref());
|
||||
return;
|
||||
}
|
||||
tp++;
|
||||
@@ -1276,8 +1292,7 @@ static void st_let(void)
|
||||
{
|
||||
u8 c = peekc();
|
||||
if (is_alpha(c) && tp[1] == '$') {
|
||||
char *dst = svars[c - 'A'];
|
||||
tp += 2;
|
||||
char *dst = str_ref();
|
||||
expect('=');
|
||||
sexpr(dst);
|
||||
} else {
|
||||
@@ -1293,8 +1308,7 @@ static u8 assign_item(u8 **pp)
|
||||
u8 *p = *pp, c = peekc(), n = 0;
|
||||
while (*p == ' ') p++;
|
||||
if (is_alpha(c) && tp[1] == '$') {
|
||||
char *dst = svars[c - 'A'];
|
||||
tp += 2;
|
||||
char *dst = str_ref();
|
||||
if (*p == '"') {
|
||||
for (p++; *p && *p != '"'; p++) if (n < STRMAX - 1) dst[n++] = *p;
|
||||
if (*p) p++;
|
||||
@@ -1355,9 +1369,8 @@ static void st_line_input(void)
|
||||
}
|
||||
v = peekc();
|
||||
if (!is_alpha(v) || tp[1] != '$') error(E_TYPE);
|
||||
tp += 2;
|
||||
if (strlen(linebuf) >= STRMAX) linebuf[STRMAX - 1] = 0;
|
||||
strcpy(svars[v - 'A'], linebuf);
|
||||
strcpy(str_ref(), linebuf);
|
||||
}
|
||||
|
||||
static void st_input(void)
|
||||
@@ -1494,23 +1507,14 @@ static void st_dim(void)
|
||||
{
|
||||
do {
|
||||
u8 a = peekc() - 'A';
|
||||
u16 d1, d2 = 0, n;
|
||||
u8 *np;
|
||||
u16 d1, d2 = 0;
|
||||
if (a >= 26) error(E_SYNTAX);
|
||||
tp++;
|
||||
if (*++tp == '$') { tp++; a += 26; }
|
||||
expect('(');
|
||||
d1 = to_int(expr());
|
||||
if (accept(',')) d2 = to_int(expr());
|
||||
expect(')');
|
||||
n = (d1 + 1) * (d2 + 1);
|
||||
if ((i16)d1 < 0 || (i16)d2 < 0 || n > 6000) error(E_SUBSCRIPT);
|
||||
np = arr_low - n * sizeof(float);
|
||||
if (np <= prog_end + 1) error(E_MEMORY);
|
||||
arr_low = np;
|
||||
memset(np, 0, n * sizeof(float));
|
||||
arrays[a].p = (float *)np;
|
||||
arrays[a].d1 = d1;
|
||||
arrays[a].d2 = d2;
|
||||
arr_alloc(a, d1, d2);
|
||||
} while (accept(','));
|
||||
}
|
||||
|
||||
|
||||
+2128
-2128
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,79 @@
|
||||
#!/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()
|
||||
Reference in new issue
Block a user