BASIC: RENUM [start[,step]] renumbers lines and their references

Fixes the numbers after GOTO, GOSUB, THEN, RUN and ON ... GOTO/GOSUB;
missing targets, quotes, REM and DATA are left alone. The new program is
built above the old one and copied down at the end, so a step of 0,
numbers past 65535, an over-long line or lack of memory change nothing.

To make room without giving up --opt-code-speed: one decimal formatter
(fmt_ul) shared by LIST, RENUM and number printing; fmt_num's output
branches merged (same text for ~2M test values); parse_num reads the
whole part and decimals in one loop. Works around an SDCC 4.5 bug that
turned a 0-trip loop into a 256-trip djnz. Adds basic/test-renum.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 13:24:56 -08:00
1 parent c553824df0
commit 8f098b5502
7 files changed
+10302 -10089

No files matched your search

+14 -2
View File
@@ -88,6 +88,8 @@ L End Enter now it is 10 PRINT "HELLO"
| `LIST` | List the whole program (Ctrl-C stops a long listing) |
| `LIST 100` | List line 100 |
| `LIST 100-200` | List lines 100 to 200 (`LIST -200` and `LIST 100-` work too) |
| `RENUM` | Renumber the program 10, 20, 30… (see below) |
| `RENUM 100, 5` | Renumber it 100, 105, 110… |
| `NEW` or `SCRATCH` | Erase the program and all variables |
| `SAVE "NAME"` | Keep the program in the board's flash (section 11) |
| `LOAD "NAME"` | Bring a saved program back |
@@ -95,6 +97,16 @@ L End Enter now it is 10 PRINT "HELLO"
| `DIR` / `DEL "NAME"` | List the saved programs / delete one |
| `DATE "2026-11-28 09:30"` | Set the date and time, which `SAVE` records |
`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
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
a line would grow too long or the program run out of memory, `RENUM` stops
with an error and changes nothing. Like any change to the program, it clears
the variables.
## 2. Program lines
A program line is a number from 0 to 65535 followed by one or more statements
@@ -1133,8 +1145,8 @@ An error stops the program and reports the line, for example
| `DIVISION BY ZERO` | Dividing by 0 |
| `OUT OF MEMORY` | The program and arrays don't fit |
| `TYPE MISMATCH` | A string where a number belongs, or the other way round |
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store |
| `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` |
| `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 |
| `GOSUB NESTED TOO DEEPLY` | More than 32 subroutines in progress |