BASIC compiler (tools/basc.py) and a runtime jump table in BASIC's ROM
tools/basc.py compiles a BASIC program to C and through GCC to an Intel HEX file loaded at A200h like the games, with a two-line BLOAD/CALL loader. Arithmetic, variables, arrays, all control flow and graphics arguments are compiled; PRINT, INPUT, strings, files, sound, DATA and the remaining functions go through BASIC's ROM via a fixed jump table at 9F00h (RT_ABI 1), so they behave identically. basic/test-compile.py runs 31 programs both ways and checks identical screens and graphics; typical loops run 7-26x faster. New error PROGRAM NEEDS ANOTHER BASIC. Fixes a bug present since the first version: variable numbers past 255 wrapped, so X1-Z9 shared storage with A-B9 (A7 and Y were the same variable). test-blocks.py checks it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa4752fd7a
commit
2e668c708f
11 files changed
+5703
-3671
No files matched your search
+165
-24
@@ -239,10 +239,10 @@ static char svars[26][STRMAX];
|
||||
struct array { u8 *p; u16 d1, d2; };
|
||||
static struct array arrays[52]; /* A()-Z(), then A$()-Z$() */
|
||||
|
||||
struct fn { u8 *body; u8 var; };
|
||||
struct fn { u8 *body; u16 var; };
|
||||
static struct fn fns[26];
|
||||
|
||||
struct forent { u8 var; float limit, step; u8 *tp, *ln; };
|
||||
struct forent { u16 var; float limit, step; u8 *tp, *ln; };
|
||||
static struct forent fors[FORMAX];
|
||||
static u8 nfor;
|
||||
|
||||
@@ -254,6 +254,8 @@ struct doent { u8 *tp, *ln; }; /* tp is the DO itself */
|
||||
static struct doent dos[DOMAX];
|
||||
static u8 ndo;
|
||||
|
||||
static u8 *data_base; /* where READ looks for DATA: 0 = the program */
|
||||
static u8 *rt_floor; /* arrays stay above this (a compiled program) */
|
||||
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 */
|
||||
@@ -301,7 +303,7 @@ 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_NOLOOP,
|
||||
E_NODO, E_DODEEP, E_NONEXT, E_ENDIF, E_ENDSEL, E_BREAK, E_STOP
|
||||
E_NODO, E_DODEEP, E_NONEXT, E_ENDIF, E_ENDSEL, E_VERSION, E_BREAK, E_STOP
|
||||
};
|
||||
static const char *const errmsg[] = {
|
||||
"SYNTAX ERROR", "UNDEFINED LINE NUMBER", "RETURN WITHOUT GOSUB",
|
||||
@@ -311,7 +313,7 @@ static const char *const errmsg[] = {
|
||||
"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",
|
||||
"IF WITHOUT END IF", "SELECT WITHOUT END SELECT", "PROGRAM NEEDS ANOTHER BASIC",
|
||||
"BREAK", "STOP"
|
||||
};
|
||||
|
||||
@@ -597,6 +599,7 @@ static void clear_vars(void)
|
||||
nfor = nsub = ndo = 0;
|
||||
datap = 0;
|
||||
deg = 0;
|
||||
data_base = rt_floor = 0;
|
||||
}
|
||||
|
||||
static void new_prog(void)
|
||||
@@ -663,16 +666,18 @@ static u16 get_lineno(void)
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Numeric variable name: letter with an optional digit. Returns its index, or
|
||||
255 if the text is not a numeric variable (does not move tp then). */
|
||||
static u8 var_name(void)
|
||||
/* Numeric variable name: letter with an optional digit. Returns its index,
|
||||
0 to 285 (so not a byte: X1 to Z9 once wrapped onto A to B9), or NOVAR if
|
||||
the text is not a numeric variable (does not move tp then). */
|
||||
#define NOVAR 0xFFFF
|
||||
static u16 var_name(void)
|
||||
{
|
||||
u8 c = peekc(), d;
|
||||
if (!is_alpha(c)) return 255;
|
||||
if (!is_alpha(c)) return NOVAR;
|
||||
d = tp[1];
|
||||
if (d == '$') return 255;
|
||||
if (d == '$') return NOVAR;
|
||||
if (is_digit(d)) {
|
||||
if (tp[2] == '$') return 255;
|
||||
if (tp[2] == '$') return NOVAR;
|
||||
tp += 2;
|
||||
return (c - 'A') * 11 + 1 + d - '0';
|
||||
}
|
||||
@@ -708,7 +713,7 @@ static void arr_alloc(u8 a, u16 d1, u16 d2)
|
||||
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);
|
||||
if (np <= prog_end + 1 || np < rt_floor) error(E_MEMORY);
|
||||
arr_low = np;
|
||||
memset(np, 0, sz);
|
||||
ar->p = np;
|
||||
@@ -716,9 +721,10 @@ static void arr_alloc(u8 a, u16 d1, u16 d2)
|
||||
ar->d2 = d2;
|
||||
}
|
||||
|
||||
static u8 *elem_at(u8 a, i16 i, i16 j, u8 two);
|
||||
|
||||
static u8 *array_elem(u8 a)
|
||||
{
|
||||
struct array *ar = &arrays[a];
|
||||
i16 i, j = 0;
|
||||
u8 two;
|
||||
expect('(');
|
||||
@@ -726,6 +732,13 @@ static u8 *array_elem(u8 a)
|
||||
two = accept(',');
|
||||
if (two) j = to_int(expr());
|
||||
expect(')');
|
||||
return elem_at(a, i, j, two);
|
||||
}
|
||||
|
||||
/* element i (, j) of array a */
|
||||
static u8 *elem_at(u8 a, i16 i, i16 j, u8 two)
|
||||
{
|
||||
struct array *ar = &arrays[a];
|
||||
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) * (a < 26 ? sizeof(float) : STRMAX);
|
||||
@@ -743,14 +756,14 @@ static char *str_ref(void)
|
||||
/* A numeric variable or array element as an lvalue. */
|
||||
static float *num_ref(void)
|
||||
{
|
||||
u8 v;
|
||||
u16 v;
|
||||
if (is_alpha(peekc()) && tp[1] == '(') {
|
||||
v = *tp - 'A';
|
||||
tp++;
|
||||
return (float *)array_elem(v);
|
||||
}
|
||||
v = var_name();
|
||||
if (v == 255) error(E_SYNTAX);
|
||||
if (v == NOVAR) error(E_SYNTAX);
|
||||
return &vars[v];
|
||||
}
|
||||
|
||||
@@ -813,11 +826,12 @@ static float round_to(float x, i16 n, u8 rnd)
|
||||
return (n < 0 ? s * p : s / p) + 0.0f;
|
||||
}
|
||||
|
||||
static float func2v(u8 t, float v);
|
||||
|
||||
static float func2(u8 t, float v)
|
||||
{
|
||||
float w;
|
||||
i16 n = 0;
|
||||
u8 i;
|
||||
switch (t) {
|
||||
case T_ANGLE: /* ANGLE(x, y): the direction of (x, y) */
|
||||
expect(',');
|
||||
@@ -846,6 +860,14 @@ static float func2(u8 t, float v)
|
||||
return v;
|
||||
}
|
||||
expect(')');
|
||||
return func2v(t, v);
|
||||
}
|
||||
|
||||
/* the one-argument functions from DEG on, of v */
|
||||
static float func2v(u8 t, float v)
|
||||
{
|
||||
float w;
|
||||
u8 i;
|
||||
switch (t) {
|
||||
case T_DEG: return v * 180.0f / PI_F;
|
||||
case T_RAD: return v * PI_F / 180.0f;
|
||||
@@ -878,6 +900,8 @@ static float func2(u8 t, float v)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float func1(u8 t, float v);
|
||||
|
||||
static float func(u8 t)
|
||||
{
|
||||
char s[STRMAX];
|
||||
@@ -917,6 +941,12 @@ static float func(u8 t)
|
||||
v = expr();
|
||||
if (t >= T_ANGLE) return func2(t, v);
|
||||
expect(')');
|
||||
return func1(t, v);
|
||||
}
|
||||
|
||||
/* the one-argument functions SIN to INP, of v */
|
||||
static float func1(u8 t, float v)
|
||||
{
|
||||
switch (t) {
|
||||
case T_SIN: return sin_cos(v, 0);
|
||||
case T_COS: return sin_cos(v, 1);
|
||||
@@ -1543,7 +1573,7 @@ static void next_data(void)
|
||||
while (*datap == ' ') datap++;
|
||||
if (*datap == ',') { datap++; return; }
|
||||
dataln += *dataln;
|
||||
} else dataln = arena;
|
||||
} else dataln = data_base ? data_base : arena;
|
||||
for (; *dataln; dataln += *dataln) {
|
||||
u8 *p = dataln + 3, q = 0;
|
||||
for (; *p; p++) {
|
||||
@@ -1741,8 +1771,9 @@ static void st_select(void)
|
||||
static void st_for(void)
|
||||
{
|
||||
struct forent *f;
|
||||
u8 v = var_name(), i;
|
||||
if (v == 255) error(E_SYNTAX);
|
||||
u16 v = var_name();
|
||||
u8 i;
|
||||
if (v == NOVAR) error(E_SYNTAX);
|
||||
expect('=');
|
||||
vars[v] = expr();
|
||||
for (i = 0; i < nfor; i++) /* reusing a loop variable restarts it */
|
||||
@@ -1766,11 +1797,11 @@ static void st_for(void)
|
||||
static void st_next(void)
|
||||
{
|
||||
struct forent *f;
|
||||
u8 v;
|
||||
u16 v;
|
||||
do {
|
||||
v = var_name();
|
||||
if (!nfor) error(E_NEXT);
|
||||
if (v != 255) {
|
||||
if (v != NOVAR) {
|
||||
while (nfor && fors[nfor - 1].var != v) nfor--;
|
||||
if (!nfor) error(E_NEXT);
|
||||
}
|
||||
@@ -1784,7 +1815,7 @@ static void st_next(void)
|
||||
return;
|
||||
}
|
||||
nfor--;
|
||||
} while (v != 255 && accept(','));
|
||||
} while (v != NOVAR && accept(','));
|
||||
}
|
||||
|
||||
static void st_dim(void)
|
||||
@@ -1804,14 +1835,15 @@ static void st_dim(void)
|
||||
|
||||
static void st_def(void)
|
||||
{
|
||||
u8 f, v;
|
||||
u8 f;
|
||||
u16 v;
|
||||
expect(T_FN);
|
||||
f = peekc();
|
||||
if (!is_alpha(f)) error(E_SYNTAX);
|
||||
tp++;
|
||||
expect('(');
|
||||
v = var_name();
|
||||
if (v == 255) error(E_SYNTAX);
|
||||
if (v == NOVAR) error(E_SYNTAX);
|
||||
expect(')');
|
||||
expect('=');
|
||||
fns[f - 'A'].var = v;
|
||||
@@ -1853,10 +1885,17 @@ static void run_prog(void)
|
||||
static u8 gcolor = 15;
|
||||
|
||||
/* Up to n comma-separated integer arguments; returns how many were given. */
|
||||
static const i16 *pre_a; /* rt_gfx: the numbers, worked out */
|
||||
static u8 pre_n;
|
||||
|
||||
static u8 int_args(i16 *a, u8 min, u8 n)
|
||||
{
|
||||
u8 i = 0;
|
||||
do a[i++] = to_int(expr()); while (i < n && accept(','));
|
||||
if (pre_a) {
|
||||
for (; i < pre_n; i++) a[i] = pre_a[i];
|
||||
pre_a = 0;
|
||||
} else
|
||||
do a[i++] = to_int(expr()); while (i < n && accept(','));
|
||||
if (i < min) error(E_SYNTAX);
|
||||
return i;
|
||||
}
|
||||
@@ -2687,6 +2726,108 @@ static void execute(void)
|
||||
running = 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------ for compiled programs
|
||||
tools/basc.py compiles BASIC to machine code, which calls these through
|
||||
the jump table at 9F00h (rt_table): statements and expressions it leaves
|
||||
to the interpreter go to rt_exec and rt_eval as tokens, so they behave
|
||||
exactly as they would interpreted. line is a 3-byte line record (its
|
||||
number in bytes 1 and 2) for error messages. The table's order and the
|
||||
tokens' and errors' values only ever grow: RT_ABI counts the changes. */
|
||||
#define RT_ABI 1
|
||||
static u8 *rt_tp, *rt_ln; /* the CALL that started the program */
|
||||
|
||||
float *rt_init(u32 abi, u8 *data, u8 *end)
|
||||
{
|
||||
if (abi != RT_ABI) error(E_VERSION);
|
||||
rt_tp = tp;
|
||||
rt_ln = curln;
|
||||
clear_vars(); /* as RUN does */
|
||||
data_base = data;
|
||||
rt_floor = end;
|
||||
return vars;
|
||||
}
|
||||
|
||||
void rt_end(void) /* END, or the last line */
|
||||
{
|
||||
close_all();
|
||||
tp = rt_tp;
|
||||
curln = rt_ln;
|
||||
}
|
||||
|
||||
void rt_exec(u8 *s, u8 *line) /* one statement */
|
||||
{
|
||||
curln = line;
|
||||
tp = s;
|
||||
statement();
|
||||
if (peekc()) error(E_SYNTAX);
|
||||
}
|
||||
|
||||
float rt_eval(u8 *s, u8 *line) /* a numeric expression */
|
||||
{
|
||||
curln = line;
|
||||
tp = s;
|
||||
return expr();
|
||||
}
|
||||
|
||||
void rt_seval(u8 *s, char *out, u8 *line) /* a string expression */
|
||||
{
|
||||
curln = line;
|
||||
tp = s;
|
||||
sexpr(out);
|
||||
}
|
||||
|
||||
float rt_fn(u8 t, float v, u8 *line) /* a one-argument function; RND, TIM */
|
||||
{
|
||||
curln = line;
|
||||
if (t == T_RND) return rnd();
|
||||
if (t == T_TIM) return (float)millis() / 1000.0f;
|
||||
return t >= T_ANGLE ? func2v(t, v) : func1(t, v);
|
||||
}
|
||||
|
||||
float *rt_aref(u8 a, float i, float j, u8 two, u8 *line) /* A(I) or A(I, J) */
|
||||
{
|
||||
i16 k;
|
||||
curln = line;
|
||||
k = to_int(i);
|
||||
return (float *)elem_at(a, k, two ? to_int(j) : 0, two);
|
||||
}
|
||||
|
||||
float rt_pow(float x, float y, u8 *line)
|
||||
{
|
||||
curln = line;
|
||||
return ipow(x, y);
|
||||
}
|
||||
|
||||
/* a graphics statement (CLS COLOR PLOT LINE RECT FILL CIRCLE LOCATE INK
|
||||
SCREEN) with its n numbers, between its fewest and most */
|
||||
void rt_gfx(u8 t, const i16 *a, u8 n, u8 *line)
|
||||
{
|
||||
curln = line;
|
||||
pre_a = t == T_CLS ? 0 : a;
|
||||
pre_n = n;
|
||||
graphics(t);
|
||||
}
|
||||
|
||||
void rt_error(u8 e, u8 *line)
|
||||
{
|
||||
curln = line;
|
||||
error(e);
|
||||
}
|
||||
|
||||
void rt_break(u8 *line) /* Ctrl-C? */
|
||||
{
|
||||
curln = line;
|
||||
check_break();
|
||||
}
|
||||
|
||||
/* at 9F00h (basic.ld), fixed for good */
|
||||
__asm__(".section .rtab, \"ax\"\n"
|
||||
".globl rt_table\n"
|
||||
"rt_table:\n"
|
||||
" j rt_init\n j rt_end\n j rt_exec\n j rt_eval\n j rt_seval\n"
|
||||
" j rt_fn\n j rt_aref\n j rt_pow\n j rt_error\n j rt_break\n j rt_gfx\n"
|
||||
".previous");
|
||||
|
||||
int main(void)
|
||||
{
|
||||
u16 n;
|
||||
|
||||
Reference in new issue
Block a user