BASIC: DO … LOOP, EXIT, ELSE, block IF and SELECT CASE
DO WHILE/UNTIL … LOOP WHILE/UNTIL, EXIT DO and EXIT FOR; IF … THEN … ELSE on one line; block IF … ELSEIF … ELSE … END IF; SELECT CASE with values, ranges and IS comparisons, numeric or string. RENUM also fixes line numbers after ELSE. basic/test-blocks.py checks them; the manual and README cover them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd876fd44d
commit
9e32e3eaf9
7 files changed
+2216
-1756
No files matched your search
@@ -14,7 +14,8 @@ RISC-V processor written from scratch in Verilog:
|
||||
- **USB keyboard** on the board's USB host port, with a keys-held port for
|
||||
games.
|
||||
- **Files** saved in the board's configuration flash.
|
||||
- **Software**: Dartmouth-style BASIC with graphics, sound and files, a
|
||||
- **Software**: Dartmouth-style BASIC with graphics, sound and files, and
|
||||
`DO … LOOP`, block `IF … ELSE … END IF` and `SELECT CASE`; a
|
||||
machine-code monitor with a RISC-V disassembler and assembler, and five
|
||||
original games.
|
||||
|
||||
@@ -152,7 +153,9 @@ uses 49.5 of the XC7A35T's 50.
|
||||
All in C (plus a little assembly), compiled with GCC:
|
||||
|
||||
- `basic/` — BASIC, with single-precision arithmetic on the FPU (`rv32imf`,
|
||||
`ilp32f`), correctly rounded. `lib/` has the few C library functions it
|
||||
`ilp32f`), correctly rounded, and the structured statements of later
|
||||
Dartmouth BASIC and True BASIC (`DO … LOOP`, `EXIT`, block `IF`, `SELECT
|
||||
CASE`). `lib/` has the few C library functions it
|
||||
needs (small string functions, `setjmp`, and `sinf` … `sqrtf`), since
|
||||
newlib's bring in its stdio and malloc.
|
||||
- `monitor/` — the machine-code monitor: memory, registers (integer and
|
||||
@@ -193,7 +196,7 @@ the configuration flash.
|
||||
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32uf`, `rv32mi`, fetched into `third_party/`) and `tests/` (interrupts, the NMI and single step) on `rv32_cpu.v` in xsim, with random wait states on the data bus. Two riscv-tests don't apply: `ma_data` expects misaligned loads and stores to work (here they trap, as the spec allows) and `pmpaddr` needs PMP. |
|
||||
| `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, both graphics modes, sprites, scrolling, flash files, the monitor (step, breakpoint, Pause, memory files, the assembler, Intel HEX), 3 Mbaud. `scripts/sim.sh quick` skips the terminal and the monitor; `wide` checks the 132 × 64 start-up. |
|
||||
| `monitor/test-disasm.py`, `test-asm.py` | the disassembler against GNU objdump on thousands of random instructions, and the assembler against GNU as |
|
||||
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays and graphics modes, and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
|
||||
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays, block structures and graphics modes, and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
|
||||
| `bench/` | the processor alone with 64 KB of RAM, for timing |
|
||||
|
||||
## Toolchain
|
||||
|
||||
Binary file not shown.
+220
-29
@@ -193,7 +193,8 @@ enum {
|
||||
T_BYE, T_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK, T_SCREEN,
|
||||
T_SAVE, T_LOAD, T_DIR, T_DEL, T_DATE, T_OPEN, T_CLOSE, T_MONITOR, T_CALL, T_BSAVE, T_BLOAD, T_OUTPUT, T_APPEND, T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS,
|
||||
T_SQR, T_INT, T_RND, T_SGN, T_FN, T_TAB, T_PEEK, T_INP, T_LEN, T_VAL, T_ASC, T_EOF,
|
||||
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM, T_LAST
|
||||
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM,
|
||||
T_DO, T_LOOP, T_WHILE, T_UNTIL, T_EXIT, T_ELSE, T_SELECT, T_CASE, T_LAST
|
||||
};
|
||||
|
||||
static const char *const kw[] = {
|
||||
@@ -203,7 +204,8 @@ static const char *const kw[] = {
|
||||
"BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK", "SCREEN",
|
||||
"SAVE", "LOAD", "DIR", "DEL", "DATE", "OPEN", "CLOSE", "MONITOR", "CALL", "BSAVE", "BLOAD", "OUTPUT", "APPEND", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
|
||||
"SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC", "EOF",
|
||||
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM"
|
||||
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM",
|
||||
"DO", "LOOP", "WHILE", "UNTIL", "EXIT", "ELSE", "SELECT", "CASE"
|
||||
};
|
||||
|
||||
/* ----------------------------------------------------------------- memory */
|
||||
@@ -212,6 +214,7 @@ static const char *const kw[] = {
|
||||
#define NVARS (26 * 11)
|
||||
#define FORMAX 16
|
||||
#define GOSUBMAX 32
|
||||
#define DOMAX 16
|
||||
|
||||
#ifdef __riscv
|
||||
/* at 8000h (basic.ld), where machine-code programs know to load into it */
|
||||
@@ -239,6 +242,10 @@ struct subent { u8 *tp, *ln; };
|
||||
static struct subent subs[GOSUBMAX];
|
||||
static u8 nsub;
|
||||
|
||||
struct doent { u8 *tp, *ln; }; /* tp is the DO itself */
|
||||
static struct doent dos[DOMAX];
|
||||
static u8 ndo;
|
||||
|
||||
static u8 *tp; /* text pointer */
|
||||
static u8 *curln; /* line being run, 0 in direct mode */
|
||||
static u8 *datap; /* next DATA item, 0 = search from the start */
|
||||
@@ -285,7 +292,8 @@ static void put_u16(u16 v)
|
||||
enum {
|
||||
E_SYNTAX, E_LINE, E_RETURN, E_NEXT, E_DATA, E_SUBSCRIPT, E_DIV0, E_MEMORY,
|
||||
E_TYPE, E_STRING, E_FUNC, E_UNDEF_FN, E_FOR, E_GOSUB, E_NOFILE, E_DISKFULL,
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_OPEN, E_NOTOPEN, E_EOF, E_BREAK, E_STOP
|
||||
E_DIRFULL, E_NAME, E_FLASH, E_TOOBIG, E_OPEN, E_NOTOPEN, E_EOF, E_NOLOOP,
|
||||
E_NODO, E_DODEEP, E_NONEXT, E_ENDIF, E_ENDSEL, E_BREAK, E_STOP
|
||||
};
|
||||
static const char *const errmsg[] = {
|
||||
"SYNTAX ERROR", "UNDEFINED LINE NUMBER", "RETURN WITHOUT GOSUB",
|
||||
@@ -294,6 +302,8 @@ static const char *const errmsg[] = {
|
||||
"UNDEFINED FUNCTION", "FOR NESTED TOO DEEPLY", "GOSUB NESTED TOO DEEPLY",
|
||||
"FILE NOT FOUND", "DISK FULL", "TOO MANY FILES", "BAD FILE NAME", "FLASH ERROR",
|
||||
"FILE TOO BIG", "FILE ALREADY OPEN", "FILE NOT OPEN", "INPUT PAST END",
|
||||
"DO WITHOUT LOOP", "LOOP WITHOUT DO", "DO NESTED TOO DEEPLY", "FOR WITHOUT NEXT",
|
||||
"IF WITHOUT END IF", "SELECT WITHOUT END SELECT",
|
||||
"BREAK", "STOP"
|
||||
};
|
||||
|
||||
@@ -576,7 +586,7 @@ static void clear_vars(void)
|
||||
memset(arrays, 0, sizeof arrays);
|
||||
memset(fns, 0, sizeof fns);
|
||||
arr_low = arena + ARENA;
|
||||
nfor = nsub = 0;
|
||||
nfor = nsub = ndo = 0;
|
||||
datap = 0;
|
||||
}
|
||||
|
||||
@@ -633,7 +643,7 @@ static void expect(u8 c)
|
||||
static u8 at_end(void)
|
||||
{
|
||||
u8 c = peekc();
|
||||
return c == 0 || c == ':';
|
||||
return c == 0 || c == ':' || c == T_ELSE; /* ELSE ends IF ... THEN's statement */
|
||||
}
|
||||
|
||||
static u16 get_lineno(void)
|
||||
@@ -1427,6 +1437,181 @@ static void st_read(void)
|
||||
} while (accept(','));
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------- block structures */
|
||||
/* Move tp to the start of the next statement, on to the next line if need
|
||||
be. Returns 0 at the end of the program (or of a direct statement). */
|
||||
static u8 next_stmt(void)
|
||||
{
|
||||
skip_statement();
|
||||
if (*tp) { tp++; return 1; }
|
||||
if (!curln || !*(curln + *curln)) return 0;
|
||||
curln += *curln;
|
||||
tp = curln + 3;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Does the statement at p end its line with THEN (a block IF)? */
|
||||
static u8 ends_then(u8 *p)
|
||||
{
|
||||
u8 q = 0, last = 0;
|
||||
for (; *p; p++) {
|
||||
if (*p == '"') q = !q;
|
||||
else if (*p != ' ') last = q ? 0 : *p;
|
||||
}
|
||||
return last == T_THEN;
|
||||
}
|
||||
|
||||
/* Skip to just past the NEXT that closes the FOR we are in, and its
|
||||
variables. */
|
||||
static void find_next(void)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_NONEXT);
|
||||
c = peekc();
|
||||
if (c == T_FOR) depth++;
|
||||
else if (c == T_NEXT && !depth--) {
|
||||
tp++;
|
||||
while (!at_end()) { var_name(); if (!accept(',')) break; }
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip to just past the LOOP that closes the DO we are in, and its
|
||||
condition. */
|
||||
static void find_loop(void)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_NOLOOP);
|
||||
c = peekc();
|
||||
if (c == T_DO) depth++;
|
||||
else if (c == T_LOOP && !depth--) { tp++; skip_statement(); return; }
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip to just past the END IF or END SELECT (k is T_IF or T_SELECT) that
|
||||
closes the block we are in. With els, a block IF's condition was false:
|
||||
stop instead at its ELSE, or at an ELSEIF whose condition is true. */
|
||||
static void find_end(u8 k, u8 els)
|
||||
{
|
||||
u8 depth = 0, c;
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(k == T_IF ? E_ENDIF : E_ENDSEL);
|
||||
c = peekc();
|
||||
if (c == k && (k == T_SELECT || ends_then(tp))) depth++;
|
||||
else if (c == T_END) {
|
||||
tp++;
|
||||
if (accept(k) && !depth--) break;
|
||||
} else if (c == T_ELSE && els && !depth) {
|
||||
tp++;
|
||||
if (!accept(T_IF)) break; /* ELSE */
|
||||
c = expr() != 0; /* ELSEIF */
|
||||
expect(T_THEN);
|
||||
if (c) break;
|
||||
}
|
||||
}
|
||||
jumped = 1; /* run on from here */
|
||||
}
|
||||
|
||||
static void st_do(void)
|
||||
{
|
||||
u8 *at = tp - 1, i, go = 1;
|
||||
for (i = 0; i < ndo; i++) /* back at a DO left by GOTO: restart it */
|
||||
if (dos[i].tp == at) { ndo = i; break; }
|
||||
if (accept(T_WHILE)) go = expr() != 0;
|
||||
else if (accept(T_UNTIL)) go = expr() == 0;
|
||||
if (!go) { find_loop(); return; }
|
||||
if (ndo >= DOMAX) error(E_DODEEP);
|
||||
dos[ndo].tp = at;
|
||||
dos[ndo].ln = curln;
|
||||
ndo++;
|
||||
}
|
||||
|
||||
static void st_loop(void)
|
||||
{
|
||||
u8 again = 1;
|
||||
if (!ndo) error(E_NODO);
|
||||
if (accept(T_WHILE)) again = expr() != 0;
|
||||
else if (accept(T_UNTIL)) again = expr() == 0;
|
||||
ndo--;
|
||||
if (again) { /* back to the DO, which tests its condition */
|
||||
tp = dos[ndo].tp;
|
||||
curln = dos[ndo].ln;
|
||||
jumped = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void st_exit(void)
|
||||
{
|
||||
if (accept(T_DO)) {
|
||||
if (!ndo) error(E_NODO);
|
||||
ndo--;
|
||||
find_loop();
|
||||
} else if (accept(T_FOR)) {
|
||||
if (!nfor) error(E_NEXT);
|
||||
nfor--;
|
||||
find_next();
|
||||
} else error(E_SYNTAX);
|
||||
}
|
||||
|
||||
/* x compared with the next value: -1, 0 or 1 */
|
||||
static i16 case_cmp(u8 str, float x, const char *xs)
|
||||
{
|
||||
if (str) {
|
||||
char a[STRMAX];
|
||||
i16 c;
|
||||
if (!is_str_start()) error(E_TYPE);
|
||||
sexpr(a);
|
||||
c = strcmp(xs, a);
|
||||
return c < 0 ? -1 : c > 0;
|
||||
} else {
|
||||
float a = expr();
|
||||
return x < a ? -1 : x > a;
|
||||
}
|
||||
}
|
||||
|
||||
/* One item of a CASE list: value, low TO high, or IS relation value. */
|
||||
static u8 case_item(u8 str, float x, const char *xs)
|
||||
{
|
||||
u8 op;
|
||||
i16 c;
|
||||
if (peekc() == 'I' && tp[1] == 'S') tp += 2;
|
||||
op = relop();
|
||||
c = case_cmp(str, x, xs);
|
||||
if (op) return (c < 0 && (op & 1)) || (c == 0 && (op & 2)) || (c > 0 && (op & 4));
|
||||
if (!accept(T_TO)) return c == 0;
|
||||
return (c >= 0) & (case_cmp(str, x, xs) <= 0);
|
||||
}
|
||||
|
||||
/* SELECT CASE: find the first CASE that matches and run on from it. */
|
||||
static void st_select(void)
|
||||
{
|
||||
char s[STRMAX];
|
||||
float x = 0;
|
||||
u8 str = 0, depth = 0, c, m;
|
||||
expect(T_CASE);
|
||||
if (is_str_start()) { sexpr(s); str = 1; }
|
||||
else x = expr();
|
||||
for (;;) {
|
||||
if (!next_stmt()) error(E_ENDSEL);
|
||||
c = peekc();
|
||||
if (c == T_SELECT) depth++;
|
||||
else if (c == T_END) {
|
||||
tp++;
|
||||
if (accept(T_SELECT) && !depth--) break;
|
||||
} else if (c == T_CASE && !depth) {
|
||||
tp++;
|
||||
if (accept(T_ELSE)) break;
|
||||
m = 0;
|
||||
do m |= case_item(str, x, s); while (accept(','));
|
||||
if (m) break;
|
||||
}
|
||||
}
|
||||
jumped = 1;
|
||||
}
|
||||
|
||||
static void st_for(void)
|
||||
{
|
||||
struct forent *f;
|
||||
@@ -1447,27 +1632,8 @@ static void st_for(void)
|
||||
nfor++;
|
||||
/* skip the body when the loop runs zero times */
|
||||
if ((f->step >= 0 && vars[v] > f->limit) || (f->step < 0 && vars[v] < f->limit)) {
|
||||
u8 depth = 0;
|
||||
nfor--;
|
||||
for (;;) {
|
||||
u8 c;
|
||||
skip_statement();
|
||||
if (!*tp) {
|
||||
if (!curln || !*(curln + *curln)) error(E_FOR);
|
||||
curln += *curln;
|
||||
tp = curln + 3;
|
||||
} else tp++;
|
||||
c = peekc();
|
||||
if (c == T_FOR) depth++;
|
||||
else if (c == T_NEXT) {
|
||||
if (!depth) {
|
||||
tp++;
|
||||
while (!at_end()) { var_name(); if (!accept(',')) break; }
|
||||
return;
|
||||
}
|
||||
depth--;
|
||||
}
|
||||
}
|
||||
find_next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2191,7 +2357,7 @@ static void st_renum(void)
|
||||
rn_put(0); rn_put(n); rn_put(n >> 8);
|
||||
for (q = p + 3; (t = *q++) != 0; ) {
|
||||
rn_put(t);
|
||||
if ((t < T_THEN || t > T_GOSUB) && t != T_RUN) continue; /* THEN GOTO GOSUB */
|
||||
if ((t < T_THEN || t > T_GOSUB) && t != T_RUN && t != T_ELSE) continue; /* THEN GOTO GOSUB ELSE */
|
||||
for (want = 1;;) {
|
||||
while (*q == ' ') rn_put(*q++);
|
||||
if (want && is_digit(*q)) {
|
||||
@@ -2229,8 +2395,22 @@ static void statement(void);
|
||||
static void st_if(void)
|
||||
{
|
||||
float c = expr();
|
||||
u8 depth = 0, q = 0;
|
||||
if (!accept(T_THEN) && peekc() != T_GOTO) error(E_SYNTAX);
|
||||
if (!c) { skip_line(); return; }
|
||||
if (!peekc()) { /* block IF: THEN ends the line */
|
||||
if (!c && curln) find_end(T_IF, 1);
|
||||
return;
|
||||
}
|
||||
if (!c) { /* on to this IF's ELSE, if it has one */
|
||||
for (;; tp++) {
|
||||
if (!*tp) return;
|
||||
if (*tp == '"') q = !q;
|
||||
else if (q) continue;
|
||||
else if (*tp == T_IF) depth++;
|
||||
else if (*tp == T_ELSE && !depth--) break;
|
||||
}
|
||||
tp++;
|
||||
}
|
||||
if (is_digit(peekc())) goto_line(get_lineno());
|
||||
else statement();
|
||||
}
|
||||
@@ -2259,7 +2439,17 @@ static void statement(void)
|
||||
case T_NEXT: st_next(); break;
|
||||
case T_DIM: st_dim(); break;
|
||||
case T_DEF: st_def(); break;
|
||||
case T_END: close_all(); running = 0; break;
|
||||
case T_END:
|
||||
if (accept(T_IF) || accept(T_SELECT)) break;
|
||||
close_all();
|
||||
running = 0;
|
||||
break;
|
||||
case T_DO: st_do(); break;
|
||||
case T_LOOP: st_loop(); break;
|
||||
case T_EXIT: st_exit(); break;
|
||||
case T_SELECT: st_select(); break;
|
||||
case T_ELSE: find_end(T_IF, 0); break; /* end of the THEN part */
|
||||
case T_CASE: find_end(T_SELECT, 0); break; /* end of a CASE's part */
|
||||
case T_STOP: error(E_STOP); break;
|
||||
case T_REM: skip_line(); break;
|
||||
case T_DATA: skip_line(); break;
|
||||
@@ -2354,6 +2544,7 @@ static void execute(void)
|
||||
if (!running) break;
|
||||
if (jumped) continue;
|
||||
if (accept(':')) continue;
|
||||
if (peekc() == T_ELSE) skip_line(); /* IF ... THEN's statement ran */
|
||||
if (peekc()) error(E_SYNTAX);
|
||||
if (!curln) break; /* end of a direct statement */
|
||||
curln += *curln;
|
||||
@@ -2377,7 +2568,7 @@ int main(void)
|
||||
newline();
|
||||
if (setjmp(errjmp)) {
|
||||
running = 0;
|
||||
nfor = nsub = 0;
|
||||
nfor = nsub = ndo = 0;
|
||||
curln = 0;
|
||||
}
|
||||
for (;;) {
|
||||
|
||||
+1722
-1722
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Try BASIC's block structures (DO ... LOOP, EXIT, IF ... ELSE ... END IF,
|
||||
SELECT CASE) in tools/rv32emu.py, with the screen kept by pyte.
|
||||
|
||||
python3 test-blocks.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:
|
||||
print(out)
|
||||
errors += not ok
|
||||
|
||||
def screen():
|
||||
return "\n".join(l.rstrip() for l in b.screen_text().display)
|
||||
|
||||
def run(prog):
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
for line in prog.strip().splitlines():
|
||||
b.type(line.encode() + CR)
|
||||
b.type(b"RUN" + CR)
|
||||
return screen().split("RUN")[-1]
|
||||
|
||||
b.type(b"")
|
||||
out = run('''
|
||||
10 I = 0
|
||||
20 DO WHILE I < 5
|
||||
30 PRINT I;
|
||||
40 I = I + 1
|
||||
50 LOOP
|
||||
60 PRINT "/";
|
||||
70 DO
|
||||
80 I = I - 1: PRINT I;
|
||||
90 LOOP UNTIL I = 0
|
||||
100 PRINT "/";
|
||||
110 DO UNTIL 1: PRINT "NEVER": LOOP
|
||||
120 DO WHILE 0
|
||||
130 PRINT "NEVER"
|
||||
140 LOOP WHILE 1
|
||||
150 PRINT "END"
|
||||
''')
|
||||
check("DO WHILE, LOOP UNTIL, and loops that run no times",
|
||||
"0 1 2 3 4 / 4 3 2 1 0 /END" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 3
|
||||
20 J = 0
|
||||
30 DO
|
||||
40 J = J + 1
|
||||
50 IF J > I THEN EXIT DO
|
||||
60 PRINT J;
|
||||
70 LOOP
|
||||
80 PRINT ",";
|
||||
90 NEXT I
|
||||
100 FOR K = 1 TO 100
|
||||
110 IF K = 4 THEN EXIT FOR
|
||||
120 NEXT K
|
||||
130 PRINT K
|
||||
''')
|
||||
check("nested DO in FOR, EXIT DO, EXIT FOR", "1 , 1 2 , 1 2 3 , 4" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 4
|
||||
20 IF I = 1 THEN
|
||||
30 PRINT "ONE";
|
||||
40 ELSEIF I = 2 THEN
|
||||
50 PRINT "TWO";
|
||||
60 IF I > 0 THEN
|
||||
70 PRINT "+";
|
||||
80 ELSE
|
||||
90 PRINT "NEVER";
|
||||
100 END IF
|
||||
110 ELSE IF I = 3 THEN
|
||||
120 PRINT "THREE";
|
||||
130 ELSE
|
||||
140 PRINT "MANY";
|
||||
150 END IF
|
||||
160 PRINT ",";
|
||||
170 NEXT I
|
||||
''')
|
||||
check("block IF, ELSEIF, ELSE IF, ELSE, nested, END IF",
|
||||
"ONE,TWO+,THREE,MANY," in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 1 TO 3
|
||||
20 IF I = 2 THEN PRINT "B"; ELSE PRINT "A";
|
||||
30 IF I = 1 THEN 50 ELSE PRINT "-";
|
||||
40 GOTO 60
|
||||
50 PRINT "*";
|
||||
60 NEXT I
|
||||
70 IF 1 THEN IF 0 THEN PRINT "X" ELSE PRINT "Y" ELSE PRINT "Z"
|
||||
80 IF 0 THEN IF 1 THEN PRINT "X" ELSE PRINT "Y" ELSE PRINT "Z"
|
||||
90 IF 0 THEN PRINT "ELSE" ELSE PRINT "ELSE OK"
|
||||
''')
|
||||
check("one-line IF ... ELSE, with line numbers and nesting",
|
||||
"A*B-A-Y\nZ\nELSE OK" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 FOR I = 0 TO 7
|
||||
20 SELECT CASE I * 2
|
||||
30 CASE 0
|
||||
40 PRINT "ZERO";
|
||||
50 CASE 2, 4
|
||||
60 PRINT "SMALL";
|
||||
70 CASE 6 TO 9
|
||||
80 PRINT "MID";
|
||||
90 CASE IS >= 12
|
||||
100 PRINT "BIG";
|
||||
110 CASE ELSE
|
||||
120 PRINT "OTHER";
|
||||
130 END SELECT
|
||||
140 PRINT ",";
|
||||
150 NEXT I
|
||||
160 A$ = "PEAR"
|
||||
170 SELECT CASE A$
|
||||
180 CASE "APPLE": PRINT "A"
|
||||
190 CASE "B" TO "Q": PRINT "IN RANGE"
|
||||
200 END SELECT
|
||||
210 SELECT CASE 5: CASE 1: PRINT "NO": END SELECT
|
||||
220 PRINT "DONE"
|
||||
''')
|
||||
check("SELECT CASE numbers: values, TO, IS, ELSE",
|
||||
"ZERO,SMALL,SMALL,MID,MID,OTHER,BIG,BIG," in out, out)
|
||||
check("SELECT CASE strings, no CASE matching", "IN RANGE\nDONE" in out, out)
|
||||
|
||||
out = run('''
|
||||
10 X = 0
|
||||
20 DO
|
||||
30 X = X + 1: PRINT X;
|
||||
40 IF X < 30 THEN GOTO 20
|
||||
50 IF X > 40 THEN EXIT DO
|
||||
60 LOOP
|
||||
70 PRINT "END"
|
||||
''')
|
||||
check("GOTO back to a DO does not fill its stack", " 40 41 END" in out, out)
|
||||
|
||||
b.type(b"CLS" + CR)
|
||||
b.type(b'I=0: DO: I=I+1: LOOP UNTIL I=7: PRINT "DIRECT"; I' + CR)
|
||||
check("DO ... LOOP in a direct statement", "DIRECT 7" in screen(), screen())
|
||||
|
||||
out = run('10 LOOP')
|
||||
check("LOOP without DO", "LOOP WITHOUT DO" in out, out)
|
||||
out = run('10 DO WHILE 0\n20 PRINT')
|
||||
check("DO without LOOP", "DO WITHOUT LOOP" in out, out)
|
||||
out = run('10 IF 0 THEN\n20 PRINT')
|
||||
check("IF without END IF", "IF WITHOUT END IF" in out, out)
|
||||
out = run('10 SELECT CASE 1\n20 PRINT')
|
||||
check("SELECT without END SELECT", "SELECT WITHOUT END SELECT" in out, out)
|
||||
out = run('10 FOR I = 2 TO 1\n20 PRINT')
|
||||
check("FOR without NEXT", "FOR WITHOUT NEXT" in out, out)
|
||||
|
||||
b.type(b"NEW" + CR)
|
||||
b.type(b"CLS" + CR)
|
||||
for l in (b"10 IF A THEN 30 ELSE 40", b"30 END IF", b"40 END"):
|
||||
b.type(l + CR)
|
||||
b.type(b"RENUM 100" + CR)
|
||||
b.type(b"LIST" + CR)
|
||||
check("RENUM follows ELSE's line number", "100 IF A THEN 110 ELSE 120" in screen(), screen())
|
||||
sys.exit(1 if errors else 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+89
-2
@@ -99,7 +99,7 @@ L End Enter now it is 10 PRINT "HELLO"
|
||||
|
||||
`RENUM` gives the lines new numbers, starting at 10 (or the first number
|
||||
given) and going up in tens (or the second). It also changes the line numbers
|
||||
after `GOTO`, `GOSUB`, `THEN`, `RUN` and `ON … GOTO`/`GOSUB` so the program
|
||||
after `GOTO`, `GOSUB`, `THEN`, `ELSE`, `RUN` and `ON … GOTO`/`GOSUB` so the program
|
||||
runs as before. A number that isn't a line in the program is left alone.
|
||||
Numbers inside quotes, `REM` and `DATA` are left as they are, and so is a
|
||||
line number worked out at run time. If the new numbers would pass 65535, or
|
||||
@@ -270,6 +270,37 @@ by statements to run. When it is false, the rest of the line is skipped, so
|
||||
statements after the colon run only when the condition is true. Any non-zero
|
||||
value counts as true.
|
||||
|
||||
`ELSE` gives the line a second part, run only when the condition is false:
|
||||
|
||||
```
|
||||
IF X > 100 THEN 500 ELSE 600
|
||||
IF A$ = "YES" THEN PRINT "OK": GOTO 20 ELSE PRINT "NO"
|
||||
IF A THEN IF B THEN PRINT "BOTH" ELSE PRINT "A ONLY" ELSE PRINT "NOT A"
|
||||
```
|
||||
|
||||
An `ELSE` belongs to the nearest `IF` before it that has none yet.
|
||||
|
||||
**Block IF.** When `THEN` is the last thing on its line, the statements that
|
||||
follow, down to `END IF`, are the `THEN` part, and the block can have
|
||||
`ELSEIF` and `ELSE` parts too:
|
||||
|
||||
```
|
||||
10 IF T < 0 THEN
|
||||
20 PRINT "FREEZING"
|
||||
30 C = 1
|
||||
40 ELSEIF T < 20 THEN
|
||||
50 PRINT "COOL"
|
||||
60 ELSE
|
||||
70 PRINT "WARM"
|
||||
80 END IF
|
||||
```
|
||||
|
||||
Only the first part whose condition is true runs (or `ELSE`'s, if none is);
|
||||
then the program carries on after `END IF`. Blocks nest inside each other and
|
||||
inside loops. `ELSEIF` may be written `ELSE IF`, and `END IF` `ENDIF`. Each
|
||||
`ELSEIF` must end its line with `THEN`. Block IFs work in a program, not in
|
||||
a direct statement.
|
||||
|
||||
### GOTO, GOSUB, RETURN, ON
|
||||
|
||||
```
|
||||
@@ -304,6 +335,56 @@ NEXT J, I
|
||||
- Loops nest 16 deep.
|
||||
- Jumping out of a loop with `GOTO` is allowed. Starting a new `FOR` with the
|
||||
same variable discards the old loop.
|
||||
- `EXIT FOR` leaves the innermost loop at once, carrying on after its `NEXT`;
|
||||
the variable keeps the value it had.
|
||||
|
||||
### DO … LOOP — loops on a condition
|
||||
|
||||
```
|
||||
10 DO WHILE I < 10
|
||||
20 I = I + 1
|
||||
30 LOOP
|
||||
|
||||
10 DO
|
||||
20 INPUT "GUESS"; G
|
||||
30 LOOP UNTIL G = 7
|
||||
```
|
||||
|
||||
- `DO WHILE` *condition* runs the loop while the condition is true, testing it
|
||||
before each time round, so the body may not run at all. `DO UNTIL`
|
||||
*condition* runs it until the condition is true.
|
||||
- `LOOP WHILE` and `LOOP UNTIL` test at the end instead, so the body always
|
||||
runs at least once.
|
||||
- A plain `DO` … `LOOP` goes round for ever, until `EXIT DO` (or `GOTO`, `END`
|
||||
or Ctrl-C) leaves it. `EXIT DO` carries on after the `LOOP`.
|
||||
- Loops nest 16 deep, with `FOR` loops inside and out. A whole loop can be on
|
||||
one line, or in a direct statement:
|
||||
`I = 0: DO: I = I + 1: PRINT I: LOOP UNTIL I = 5`.
|
||||
|
||||
### SELECT CASE — choosing by value
|
||||
|
||||
```
|
||||
10 SELECT CASE N
|
||||
20 CASE 0
|
||||
30 PRINT "NONE"
|
||||
40 CASE 1, 2, 3
|
||||
50 PRINT "A FEW"
|
||||
60 CASE 4 TO 9
|
||||
70 PRINT "SEVERAL"
|
||||
80 CASE IS >= 10
|
||||
90 PRINT "LOTS"
|
||||
100 CASE ELSE
|
||||
110 PRINT "LESS THAN NONE"
|
||||
120 END SELECT
|
||||
```
|
||||
|
||||
`SELECT CASE` works out its value once, then runs the statements under the
|
||||
first `CASE` that matches it and carries on after `END SELECT`. A `CASE` lists
|
||||
values, ranges (*low* `TO` *high*) and comparisons (`IS` followed by `<`,
|
||||
`<=`, `=`, `<>`, `>=` or `>` and a value), separated by commas; any one of
|
||||
them matching is enough. `CASE ELSE` matches anything, so it goes last. When
|
||||
no `CASE` matches, nothing runs. The value may be a string, with strings in
|
||||
the `CASE`s: `CASE "A" TO "M"` compares in alphabetical order, as `<` does.
|
||||
|
||||
### DIM — arrays
|
||||
|
||||
@@ -1228,7 +1309,13 @@ An error stops the program and reports the line, for example
|
||||
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store (also from `RENUM`) |
|
||||
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
|
||||
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
|
||||
| `FOR NESTED TOO DEEPLY` | More than 16 loops open |
|
||||
| `FOR NESTED TOO DEEPLY` | More than 16 `FOR` loops open |
|
||||
| `DO NESTED TOO DEEPLY` | More than 16 `DO` loops open |
|
||||
| `FOR WITHOUT NEXT` | A `FOR` that runs no times, or `EXIT FOR`, with no `NEXT` after it |
|
||||
| `DO WITHOUT LOOP` | A `DO` whose condition fails, or `EXIT DO`, with no `LOOP` after it |
|
||||
| `LOOP WITHOUT DO` | `LOOP` or `EXIT DO` with no `DO` loop open |
|
||||
| `IF WITHOUT END IF` | A block `IF` (or its `ELSE`) with no `END IF` after it |
|
||||
| `SELECT WITHOUT END SELECT` | A `SELECT CASE` (or its `CASE`) with no `END SELECT` after it |
|
||||
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |
|
||||
| `FILE NOT FOUND` | `LOAD`, `RUN` or `DEL` of a name not in `DIR` |
|
||||
| `BAD FILE NAME` | A name that is empty, longer than 8 or not just letters and digits |
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user