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
@@ -212,7 +212,7 @@ to copy that program line one character at a time, or End to copy all of it.
|
|||||||
- Numbers: a letter or a letter and digit (`X`, `A1`), single-precision
|
- Numbers: a letter or a letter and digit (`X`, `A1`), single-precision
|
||||||
floating point.
|
floating point.
|
||||||
- Arrays: one letter, one or two subscripts from 0, 10 by default.
|
- Arrays: one letter, one or two subscripts from 0, 10 by default.
|
||||||
- Strings: `A$`..`Z$`, up to 79 characters.
|
- Strings: `A$`..`Z$`, up to 79 characters; string arrays `A$(n)` likewise.
|
||||||
- **Operators**: `+ - * / ^`, `= <> < > <= >=`, `AND OR NOT`.
|
- **Operators**: `+ - * / ^`, `= <> < > <= >=`, `AND OR NOT`.
|
||||||
- **Functions**:
|
- **Functions**:
|
||||||
- Numeric: `SIN COS TAN ATN EXP LOG ABS SQR INT RND SGN`, `FNx`
|
- Numeric: `SIN COS TAN ATN EXP LOG ABS SQR INT RND SGN`, `FNx`
|
||||||
@@ -222,7 +222,7 @@ to copy that program line one character at a time, or End to copy all of it.
|
|||||||
- Spaces are optional, as at Dartmouth: `FORI=1TO10` works.
|
- Spaces are optional, as at Dartmouth: `FORI=1TO10` works.
|
||||||
- `DATA` takes the rest of its line.
|
- `DATA` takes the rest of its line.
|
||||||
|
|
||||||
About 20.3 KB of program space is free; F000h-F3F7h is kept free for machine code.
|
About 20.2 KB of program space is free; F000h-F3F7h is kept free for machine code.
|
||||||
|
|
||||||
**Graphics:** the layer is 320×240 pixels in the 16 terminal colours, each
|
**Graphics:** the layer is 320×240 pixels in the 16 terminal colours, each
|
||||||
pixel a 4×4 block, with (0,0) at the top left. Colour 0 is transparent. Text is
|
pixel a 4×4 block, with (0,0) at the top left. Colour 0 is transparent. Text is
|
||||||
|
|||||||
Binary file not shown.
+40
-36
@@ -221,7 +221,7 @@ static const char *const kw[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* ----------------------------------------------------------------- memory */
|
/* ----------------------------------------------------------------- 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 */
|
F000-F3F7 is left free for machine code */
|
||||||
#define STRMAX 80
|
#define STRMAX 80
|
||||||
#define NVARS (26 * 11)
|
#define NVARS (26 * 11)
|
||||||
@@ -235,8 +235,8 @@ static u8 *arr_low; /* lowest byte used by arrays */
|
|||||||
static float vars[NVARS];
|
static float vars[NVARS];
|
||||||
static char svars[26][STRMAX];
|
static char svars[26][STRMAX];
|
||||||
|
|
||||||
struct array { float *p; u16 d1, d2; };
|
struct array { u8 *p; u16 d1, d2; };
|
||||||
static struct array arrays[26];
|
static struct array arrays[52]; /* A()-Z(), then A$()-Z$() */
|
||||||
|
|
||||||
struct fn { u8 *body; u8 var; };
|
struct fn { u8 *body; u8 var; };
|
||||||
static struct fn fns[26];
|
static struct fn fns[26];
|
||||||
@@ -690,7 +690,24 @@ static i16 to_int(float v)
|
|||||||
return (i16)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];
|
struct array *ar = &arrays[a];
|
||||||
i16 i, j = 0;
|
i16 i, j = 0;
|
||||||
@@ -700,18 +717,18 @@ static float *array_elem(u8 a)
|
|||||||
two = accept(',');
|
two = accept(',');
|
||||||
if (two) j = to_int(expr());
|
if (two) j = to_int(expr());
|
||||||
expect(')');
|
expect(')');
|
||||||
if (!ar->p) { /* undeclared: dimension 10 (0..10) */
|
if (!ar->p) arr_alloc(a, 10, two ? 10 : 0); /* undeclared: 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 (i < 0 || i > (i16)ar->d1 || j < 0 || j > (i16)ar->d2) error(E_SUBSCRIPT);
|
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. */
|
/* A numeric variable or array element as an lvalue. */
|
||||||
@@ -721,7 +738,7 @@ static float *num_ref(void)
|
|||||||
if (is_alpha(peekc()) && tp[1] == '(') {
|
if (is_alpha(peekc()) && tp[1] == '(') {
|
||||||
v = *tp - 'A';
|
v = *tp - 'A';
|
||||||
tp++;
|
tp++;
|
||||||
return array_elem(v);
|
return (float *)array_elem(v);
|
||||||
}
|
}
|
||||||
v = var_name();
|
v = var_name();
|
||||||
if (v == 255) error(E_SYNTAX);
|
if (v == 255) error(E_SYNTAX);
|
||||||
@@ -960,8 +977,7 @@ static void str_prim(char *out)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (is_alpha(c) && tp[1] == '$') {
|
if (is_alpha(c) && tp[1] == '$') {
|
||||||
strcpy(out, svars[c - 'A']);
|
strcpy(out, str_ref());
|
||||||
tp += 2;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tp++;
|
tp++;
|
||||||
@@ -1276,8 +1292,7 @@ static void st_let(void)
|
|||||||
{
|
{
|
||||||
u8 c = peekc();
|
u8 c = peekc();
|
||||||
if (is_alpha(c) && tp[1] == '$') {
|
if (is_alpha(c) && tp[1] == '$') {
|
||||||
char *dst = svars[c - 'A'];
|
char *dst = str_ref();
|
||||||
tp += 2;
|
|
||||||
expect('=');
|
expect('=');
|
||||||
sexpr(dst);
|
sexpr(dst);
|
||||||
} else {
|
} else {
|
||||||
@@ -1293,8 +1308,7 @@ static u8 assign_item(u8 **pp)
|
|||||||
u8 *p = *pp, c = peekc(), n = 0;
|
u8 *p = *pp, c = peekc(), n = 0;
|
||||||
while (*p == ' ') p++;
|
while (*p == ' ') p++;
|
||||||
if (is_alpha(c) && tp[1] == '$') {
|
if (is_alpha(c) && tp[1] == '$') {
|
||||||
char *dst = svars[c - 'A'];
|
char *dst = str_ref();
|
||||||
tp += 2;
|
|
||||||
if (*p == '"') {
|
if (*p == '"') {
|
||||||
for (p++; *p && *p != '"'; p++) if (n < STRMAX - 1) dst[n++] = *p;
|
for (p++; *p && *p != '"'; p++) if (n < STRMAX - 1) dst[n++] = *p;
|
||||||
if (*p) p++;
|
if (*p) p++;
|
||||||
@@ -1355,9 +1369,8 @@ static void st_line_input(void)
|
|||||||
}
|
}
|
||||||
v = peekc();
|
v = peekc();
|
||||||
if (!is_alpha(v) || tp[1] != '$') error(E_TYPE);
|
if (!is_alpha(v) || tp[1] != '$') error(E_TYPE);
|
||||||
tp += 2;
|
|
||||||
if (strlen(linebuf) >= STRMAX) linebuf[STRMAX - 1] = 0;
|
if (strlen(linebuf) >= STRMAX) linebuf[STRMAX - 1] = 0;
|
||||||
strcpy(svars[v - 'A'], linebuf);
|
strcpy(str_ref(), linebuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void st_input(void)
|
static void st_input(void)
|
||||||
@@ -1494,23 +1507,14 @@ static void st_dim(void)
|
|||||||
{
|
{
|
||||||
do {
|
do {
|
||||||
u8 a = peekc() - 'A';
|
u8 a = peekc() - 'A';
|
||||||
u16 d1, d2 = 0, n;
|
u16 d1, d2 = 0;
|
||||||
u8 *np;
|
|
||||||
if (a >= 26) error(E_SYNTAX);
|
if (a >= 26) error(E_SYNTAX);
|
||||||
tp++;
|
if (*++tp == '$') { tp++; a += 26; }
|
||||||
expect('(');
|
expect('(');
|
||||||
d1 = to_int(expr());
|
d1 = to_int(expr());
|
||||||
if (accept(',')) d2 = to_int(expr());
|
if (accept(',')) d2 = to_int(expr());
|
||||||
expect(')');
|
expect(')');
|
||||||
n = (d1 + 1) * (d2 + 1);
|
arr_alloc(a, d1, d2);
|
||||||
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;
|
|
||||||
} while (accept(','));
|
} 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()
|
||||||
+20
-4
@@ -154,6 +154,21 @@ empty. String constants are written in double quotes. `+` joins strings:
|
|||||||
30 PRINT B$
|
30 PRINT B$
|
||||||
```
|
```
|
||||||
|
|
||||||
|
String arrays work like numeric ones, with a `$` after the letter. Each element
|
||||||
|
holds up to 79 characters and starts empty:
|
||||||
|
|
||||||
|
```
|
||||||
|
10 DIM N$(4)
|
||||||
|
20 FOR I = 0 TO 4: READ N$(I): NEXT I
|
||||||
|
30 PRINT N$(2)
|
||||||
|
40 DATA RED, GREEN, BLUE, YELLOW, WHITE
|
||||||
|
```
|
||||||
|
|
||||||
|
`N$`, `N$( )` and `N( )` are all separate. A string array used without `DIM`
|
||||||
|
gets subscripts 0 to 10. Every element takes 80 bytes whether it is used or
|
||||||
|
not, so a string array can have at most 255 elements, and `DIM A$(200)` uses
|
||||||
|
about 16 KB of the program space.
|
||||||
|
|
||||||
## 4. Expressions
|
## 4. Expressions
|
||||||
|
|
||||||
From highest precedence to lowest:
|
From highest precedence to lowest:
|
||||||
@@ -281,10 +296,11 @@ NEXT J, I
|
|||||||
### DIM — arrays
|
### DIM — arrays
|
||||||
|
|
||||||
```
|
```
|
||||||
DIM A(50), B(10,20)
|
DIM A(50), B(10,20), N$(30)
|
||||||
```
|
```
|
||||||
|
|
||||||
See section 3. An array can hold at most 6000 elements.
|
See section 3. A numeric array can hold at most 6000 elements, a string array
|
||||||
|
255.
|
||||||
|
|
||||||
### DEF FN — your own functions
|
### DEF FN — your own functions
|
||||||
|
|
||||||
@@ -1323,11 +1339,11 @@ while keeping the cave connected, mirrored and free of dead ends
|
|||||||
|
|
||||||
| Item | Limit |
|
| Item | Limit |
|
||||||
|---|---|
|
|---|---|
|
||||||
| Program and arrays together | about 20.3 KB |
|
| Program and arrays together | about 20.2 KB |
|
||||||
| Line numbers | 0 – 65535 |
|
| Line numbers | 0 – 65535 |
|
||||||
| Line length | 250 characters typed |
|
| Line length | 250 characters typed |
|
||||||
| Numeric variables | A–Z and A0–Z9 |
|
| Numeric variables | A–Z and A0–Z9 |
|
||||||
| String variables | A$–Z$, 79 characters each |
|
| String variables | A$–Z$, 79 characters each; string arrays A$( )–Z$( ), up to 255 elements |
|
||||||
| Arrays | A–Z, 1 or 2 subscripts, up to 6000 elements each |
|
| Arrays | A–Z, 1 or 2 subscripts, up to 6000 elements each |
|
||||||
| User functions | FNA–FNZ, one argument each |
|
| User functions | FNA–FNZ, one argument each |
|
||||||
| FOR nesting / GOSUB nesting | 16 / 32 |
|
| FOR nesting / GOSUB nesting | 16 / 32 |
|
||||||
|
|||||||
Binary file not shown.
Reference in new issue
Block a user