v4 text.c: a word's flags and capabilities after its name; import lines (task 3, part 1)
As ruled (plans §10.2, §10.3, §10.5): ': NAME inline pinned mode N ports request N' records the word's attributes and lays nothing down; 'import TRAIN NAME' declares another train's word, its calls, jumps and &NAME recorded as that import. test_text: 11 checks failed before; 274 pass at both widths. The check that ': T immediate ;' is refused is replaced, as ruled. Full gates run at the end of task 3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
+138
-7
@@ -9,6 +9,7 @@
|
||||
#include <string.h>
|
||||
|
||||
enum { KIND_WORD = 1, KIND_CONST, KIND_MACRO, KIND_SET }; /* the ROM's kinds: 1 word, 2 constant, 3 macro, 4 set variable */
|
||||
#define KIND_IMPORT 5 /* text.c's only: a word of another train (an import line) */
|
||||
|
||||
static const char *const mnemonic[V4_OPCODE_COUNT] = {
|
||||
";", "ex", "jump", "call", "unext", "next", "if", "-if",
|
||||
@@ -71,7 +72,9 @@ static int is_reserved(const char *s)
|
||||
|| strcmp(s, "header") == 0 || strcmp(s, "constant") == 0
|
||||
|| strcmp(s, "#,") == 0 || strcmp(s, "\",") == 0 || strcmp(s, "set") == 0 || strcmp(s, "here") == 0
|
||||
|| strcmp(s, "#id,") == 0 || strcmp(s, "#ids") == 0
|
||||
|| strcmp(s, "store") == 0 || strcmp(s, "start") == 0 || flag_of(s) != 0;
|
||||
|| strcmp(s, "store") == 0 || strcmp(s, "start") == 0 || flag_of(s) != 0
|
||||
|| strcmp(s, "pinned") == 0 || strcmp(s, "mode") == 0 || strcmp(s, "ports") == 0
|
||||
|| strcmp(s, "request") == 0 || strcmp(s, "import") == 0;
|
||||
}
|
||||
|
||||
static int is_label_def(const char *s)
|
||||
@@ -171,22 +174,33 @@ static int raw_token(v4_text *t, reader *r, char *tok)
|
||||
/* One cell appended at the next word address; nothing is being built. */
|
||||
/* The address record: `cell` holds an address of this assembly, in the branch field of `slot`, or
|
||||
* whole (V4_TEXT_WHOLE_CELL). Nothing when the record is off. */
|
||||
static void note(v4_text *t, v4_cell cell, unsigned slot)
|
||||
static void note_import(v4_text *t, v4_cell cell, unsigned slot, int import)
|
||||
{
|
||||
if (!t->rec || t->failed) return;
|
||||
if (t->nrec == t->rec_cap) { fail(t, "too many addresses for the address record", NULL); return; }
|
||||
t->rec[t->nrec].cell = cell;
|
||||
t->rec[t->nrec].slot = slot;
|
||||
t->rec[t->nrec].import = import;
|
||||
t->nrec++;
|
||||
}
|
||||
|
||||
static void note(v4_text *t, v4_cell cell, unsigned slot)
|
||||
{
|
||||
note_import(t, cell, slot, -1);
|
||||
}
|
||||
|
||||
/* `@p` and a literal that is an address of this assembly: the literal's cell follows the instruction
|
||||
* word, after the literals before it in that word. */
|
||||
static void lit_addr(v4_text *t, v4_cell value)
|
||||
static void lit_addr_of(v4_text *t, v4_cell value, int import)
|
||||
{
|
||||
v4_cell cell = t->as.here + 1 + (v4_cell)t->as.nlit;
|
||||
v4_asm_lit(&t->as, value);
|
||||
if (!t->as.error) note(t, cell, V4_TEXT_WHOLE_CELL);
|
||||
if (!t->as.error) note_import(t, cell, V4_TEXT_WHOLE_CELL, import);
|
||||
}
|
||||
|
||||
static void lit_addr(v4_text *t, v4_cell value)
|
||||
{
|
||||
lit_addr_of(t, value, -1);
|
||||
}
|
||||
|
||||
static void put_cell(v4_text *t, v4_cell value)
|
||||
@@ -262,14 +276,19 @@ static void check_asm(v4_text *t, const char *tok)
|
||||
if (t->as.error) fail(t, "does not fit: memory is full, or a branch cannot reach", tok);
|
||||
}
|
||||
|
||||
static void branch_to(v4_text *t, unsigned op, v4_cell target, const char *tok)
|
||||
static void branch_to_of(v4_text *t, unsigned op, v4_cell target, const char *tok, int import)
|
||||
{
|
||||
v4_asm_ref ref;
|
||||
place_branch(t);
|
||||
ref = v4_asm_branch_fwd(&t->as, op); /* v4_asm_branch, with its ref kept for the record */
|
||||
v4_asm_resolve(&t->as, ref, target);
|
||||
check_asm(t, tok);
|
||||
if (!t->failed) note(t, ref.addr, ref.slot);
|
||||
if (!t->failed) note_import(t, ref.addr, ref.slot, import);
|
||||
}
|
||||
|
||||
static void branch_to(v4_text *t, unsigned op, v4_cell target, const char *tok)
|
||||
{
|
||||
branch_to_of(t, op, target, tok, -1);
|
||||
}
|
||||
|
||||
static void branch_later(v4_text *t, unsigned op, const char *name, int local)
|
||||
@@ -298,6 +317,7 @@ static void branch(v4_text *t, unsigned op, const char *name, int may_be_label)
|
||||
if (strcmp(t->pool + t->label[i].name, name) == 0) { branch_to(t, op, t->label[i].addr, name); return; }
|
||||
s = find(t, name);
|
||||
if (s && s->kind == KIND_WORD && !may_be_label) { branch_to(t, op, s->value, name); return; }
|
||||
if (s && s->kind == KIND_IMPORT) { branch_to_of(t, op, s->value, name, (int)s->req); return; }
|
||||
if ((s && s->kind != KIND_WORD) || opcode_of(name) >= 0 || is_reserved(name) || is_label_def(name)) {
|
||||
fail(t, "not something to branch to", name);
|
||||
return;
|
||||
@@ -332,7 +352,10 @@ static void end_word(v4_text *t)
|
||||
t->ref[i].local = 0;
|
||||
s = find(t, t->pool + t->ref[i].name);
|
||||
if (s && s->kind == KIND_WORD) resolve(t, i, s->value); /* moves another into place i */
|
||||
else i++;
|
||||
else if (s && s->kind == KIND_IMPORT) {
|
||||
t->line = t->ref[i].line;
|
||||
fail(t, "an import is declared before it is used", t->pool + t->ref[i].name);
|
||||
} else i++;
|
||||
}
|
||||
t->nlabel = 0;
|
||||
}
|
||||
@@ -350,6 +373,10 @@ static void define(v4_text *t, const char *name, unsigned char kind, v4_cell val
|
||||
t->sym[t->nsym].kind = kind;
|
||||
t->sym[t->nsym].value = value;
|
||||
t->sym[t->nsym].addr = 0;
|
||||
t->sym[t->nsym].ports = 0;
|
||||
t->sym[t->nsym].attr = 0;
|
||||
t->sym[t->nsym].req = t->sym[t->nsym].nreq = 0;
|
||||
t->sym[t->nsym].train = 0;
|
||||
if (!t->failed) t->nsym++;
|
||||
}
|
||||
|
||||
@@ -524,6 +551,48 @@ static void start_directive(v4_text *t, reader *r)
|
||||
t->start = s->value;
|
||||
}
|
||||
|
||||
/* After `: NAME`: one of its attributes (§10.2, §10.5). 1 if `tok` was one. */
|
||||
static int word_attr(v4_text *t, reader *r, const char *tok)
|
||||
{
|
||||
v4_text_symbol *w = &t->sym[t->attr_open];
|
||||
char num[V4_TEXT_MAX_TOKEN + 1u];
|
||||
v4_cell f = flag_of(tok), v;
|
||||
int big;
|
||||
|
||||
if (f) { w->attr |= f; return 1; }
|
||||
if (strcmp(tok, "pinned") == 0) { w->attr |= 64; return 1; }
|
||||
if (strcmp(tok, "ports") == 0) { w->ports = 1; return 1; }
|
||||
if (strcmp(tok, "mode") == 0 || strcmp(tok, "request") == 0) {
|
||||
if (!next_token(t, r, num) || !number_of(num, &v, &big) || big) { fail(t, "mode and request need a number", tok); return 1; }
|
||||
if (tok[0] == 'm') {
|
||||
if (v < 0 || v > 255) { fail(t, "a mode is 0 to 255", num); return 1; }
|
||||
w->attr = (v4_cell)(((v4_ucell)w->attr & ~(v4_ucell)0xFF00u) | ((v4_ucell)v << 8));
|
||||
return 1;
|
||||
}
|
||||
if (t->nreqs == V4_TEXT_MAX_REQS) { fail(t, "too many requests", num); return 1; }
|
||||
if (w->nreq == 0) w->req = t->nreqs;
|
||||
else if (w->req + w->nreq != t->nreqs) { fail(t, "a word's requests are written together", num); return 1; }
|
||||
t->reqs[t->nreqs++] = v;
|
||||
w->nreq++;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* `import TRAIN NAME`: NAME is a word of the train TRAIN (§10.3). */
|
||||
static void import_directive(v4_text *t, reader *r)
|
||||
{
|
||||
char train[V4_TEXT_MAX_TOKEN + 1u], name[V4_TEXT_MAX_TOKEN + 1u];
|
||||
if (r->depth != 1) { fail(t, "not allowed in a macro", "import"); return; }
|
||||
if (!next_token(t, r, train) || !next_token(t, r, name)) { if (!t->failed) fail(t, "import needs a train and a name", NULL); return; }
|
||||
if (t->nimport == V4_TEXT_MAX_IMPORTS) { fail(t, "too many imports", name); return; }
|
||||
define(t, name, KIND_IMPORT, V4_TEXT_IMPORT_ADDR(t->nimport));
|
||||
if (t->failed) return;
|
||||
t->sym[t->nsym - 1u].req = t->nimport;
|
||||
t->sym[t->nsym - 1u].train = intern(t, train);
|
||||
t->import_sym[t->nimport++] = t->nsym - 1u;
|
||||
}
|
||||
|
||||
static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
{
|
||||
const v4_text_symbol *s;
|
||||
@@ -532,6 +601,13 @@ static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
int op, big;
|
||||
|
||||
if (t->start >= 0) { fail(t, "text after start", tok); return; }
|
||||
if (t->attr_open >= 0 && word_attr(t, r, tok)) return;
|
||||
t->attr_open = -1;
|
||||
if (strcmp(tok, "pinned") == 0 || strcmp(tok, "mode") == 0 || strcmp(tok, "ports") == 0 || strcmp(tok, "request") == 0) {
|
||||
fail(t, "a word's attribute must follow its : name", tok);
|
||||
return;
|
||||
}
|
||||
if (strcmp(tok, "import") == 0) { import_directive(t, r); return; }
|
||||
if (strcmp(tok, "header") == 0) { header(t, r); return; }
|
||||
if (t->header_pending) {
|
||||
v4_cell f = flag_of(tok);
|
||||
@@ -544,6 +620,7 @@ static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
if (!next_token(t, r, name)) { fail(t, ": without a name", NULL); return; }
|
||||
end_word(t);
|
||||
define(t, name, KIND_WORD, v4_asm_label(as));
|
||||
if (!t->failed && !t->header_pending) t->attr_open = (int)t->nsym - 1;
|
||||
t->header_pending = 0;
|
||||
return;
|
||||
}
|
||||
@@ -621,6 +698,7 @@ static void one_token(v4_text *t, reader *r, const char *tok)
|
||||
|
||||
if (tok[0] == '&' && tok[1] != 0) { /* the address of a word, as a literal */
|
||||
s = find(t, tok + 1);
|
||||
if (s && s->kind == KIND_IMPORT) { lit_addr_of(t, s->value, (int)s->req); check_asm(t, tok); return; }
|
||||
if (!s || s->kind != KIND_WORD) { fail(t, "& needs a word that is already defined", tok + 1); return; }
|
||||
lit_addr(t, s->value);
|
||||
check_asm(t, tok);
|
||||
@@ -682,6 +760,9 @@ void v4_text_begin(v4_text *t, v4_node *n, v4_cell origin)
|
||||
t->rec = NULL;
|
||||
t->rec_cap = 0;
|
||||
t->nrec = 0;
|
||||
t->nreqs = 0;
|
||||
t->nimport = 0;
|
||||
t->attr_open = -1;
|
||||
}
|
||||
|
||||
void v4_text_record(v4_text *t, v4_text_addr *buf, unsigned cap)
|
||||
@@ -696,6 +777,54 @@ unsigned v4_text_addresses(const v4_text *t)
|
||||
return t->nrec;
|
||||
}
|
||||
|
||||
static void info_of(const v4_text *t, const v4_text_symbol *s, v4_text_word_info *out)
|
||||
{
|
||||
out->name = t->pool + s->name;
|
||||
out->addr = s->value;
|
||||
out->flags = s->attr;
|
||||
out->ports = s->ports;
|
||||
out->req = t->reqs + s->req;
|
||||
out->nreq = s->nreq;
|
||||
}
|
||||
|
||||
int v4_text_word_attrs(const v4_text *t, const char *name, v4_text_word_info *out)
|
||||
{
|
||||
const v4_text_symbol *s = find(t, name);
|
||||
if (!s || s->kind != KIND_WORD) return 0;
|
||||
info_of(t, s, out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned v4_text_word_count(const v4_text *t)
|
||||
{
|
||||
unsigned i, c = 0;
|
||||
for (i = 0; i < t->nsym; i++) if (t->sym[i].kind == KIND_WORD) c++;
|
||||
return c;
|
||||
}
|
||||
|
||||
int v4_text_word_at(const v4_text *t, unsigned i, v4_text_word_info *out)
|
||||
{
|
||||
unsigned j;
|
||||
for (j = 0; j < t->nsym; j++)
|
||||
if (t->sym[j].kind == KIND_WORD && i-- == 0) { info_of(t, &t->sym[j], out); return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned v4_text_import_count(const v4_text *t)
|
||||
{
|
||||
return t->nimport;
|
||||
}
|
||||
|
||||
int v4_text_import_at(const v4_text *t, unsigned k, const char **train, const char **name)
|
||||
{
|
||||
const v4_text_symbol *s;
|
||||
if (k >= t->nimport) return 0;
|
||||
s = &t->sym[t->import_sym[k]];
|
||||
*train = t->pool + s->train;
|
||||
*name = t->pool + s->name;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void v4_text_constant(v4_text *t, const char *name, v4_cell value)
|
||||
{
|
||||
if (t->failed) return;
|
||||
@@ -761,6 +890,8 @@ int v4_text_finish(v4_text *t)
|
||||
resolve(t, 0, s->value);
|
||||
}
|
||||
if (!t->failed && !v4_asm_ok(&t->as)) fail(t, "does not fit: memory is full", NULL);
|
||||
if (!t->failed && t->nimport && t->as.here > V4_TEXT_IMPORT_ADDR(t->nimport - 1u))
|
||||
fail(t, "the code reaches the imports' placeholders", NULL);
|
||||
return !t->failed;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user