(uxnasm) Lambdas support

This commit is contained in:
Devine Lu Linvega 2023-07-29 16:14:19 -07:00
parent e2e5e86531
commit 32cc690539
1 changed files with 27 additions and 14 deletions

View File

@ -35,8 +35,9 @@ typedef struct {
typedef struct { typedef struct {
Uint8 data[LENGTH]; Uint8 data[LENGTH];
Uint8 lambda_stack[0x100], lambda_ptr, lambda_count;
unsigned int ptr, length; unsigned int ptr, length;
Uint16 llen, mlen, rlen; Uint16 label_len, macro_len, refs_len;
Label labels[0x400]; Label labels[0x400];
Macro macros[0x100]; Macro macros[0x100];
Reference refs[0x800]; Reference refs[0x800];
@ -87,7 +88,7 @@ static Macro *
findmacro(char *name) findmacro(char *name)
{ {
int i; int i;
for(i = 0; i < p.mlen; i++) for(i = 0; i < p.macro_len; i++)
if(scmp(p.macros[i].name, name, 0x40)) if(scmp(p.macros[i].name, name, 0x40))
return &p.macros[i]; return &p.macros[i];
return NULL; return NULL;
@ -97,7 +98,7 @@ static Label *
findlabel(char *name) findlabel(char *name)
{ {
int i; int i;
for(i = 0; i < p.llen; i++) for(i = 0; i < p.label_len; i++)
if(scmp(p.labels[i].name, name, 0x40)) if(scmp(p.labels[i].name, name, 0x40))
return &p.labels[i]; return &p.labels[i];
return NULL; return NULL;
@ -139,9 +140,9 @@ makemacro(char *name, FILE *f)
return error("Macro name is hex number", name); return error("Macro name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error("Macro name is invalid", name); return error("Macro name is invalid", name);
if(p.mlen == 0x100) if(p.macro_len == 0x100)
return error("Macros limit exceeded", name); return error("Macros limit exceeded", name);
m = &p.macros[p.mlen++]; m = &p.macros[p.macro_len++];
scpy(name, m->name, 0x40); scpy(name, m->name, 0x40);
while(fscanf(f, "%63s", word) == 1) { while(fscanf(f, "%63s", word) == 1) {
if(word[0] == '{') continue; if(word[0] == '{') continue;
@ -165,9 +166,9 @@ makelabel(char *name)
return error("Label name is hex number", name); return error("Label name is hex number", name);
if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name)) if(findopcode(name) || scmp(name, "BRK", 4) || !slen(name))
return error("Label name is invalid", name); return error("Label name is invalid", name);
if(p.llen == 0x400) if(p.label_len == 0x400)
return error("Labels limit exceeded", name); return error("Labels limit exceeded", name);
l = &p.labels[p.llen++]; l = &p.labels[p.label_len++];
l->addr = p.ptr; l->addr = p.ptr;
l->refs = 0; l->refs = 0;
scpy(name, l->name, 0x40); scpy(name, l->name, 0x40);
@ -179,9 +180,9 @@ makereference(char *scope, char *label, char rune, Uint16 addr)
{ {
char subw[0x40], parent[0x40]; char subw[0x40], parent[0x40];
Reference *r; Reference *r;
if(p.rlen >= 0x800) if(p.refs_len >= 0x800)
return error("References limit exceeded", label); return error("References limit exceeded", label);
r = &p.refs[p.rlen++]; r = &p.refs[p.refs_len++];
if(label[0] == '&') { if(label[0] == '&') {
if(!sublabel(subw, scope, label + 1)) if(!sublabel(subw, scope, label + 1))
return error("Invalid sublabel", label); return error("Invalid sublabel", label);
@ -351,6 +352,18 @@ parse(char *w, FILE *f)
while((c = w[++i])) while((c = w[++i]))
if(!writebyte(c)) return 0; if(!writebyte(c)) return 0;
break; break;
case '{': /* lambda start */
char laba[8] = "lambda0\0";
p.lambda_stack[p.lambda_ptr++] = p.lambda_count;
laba[6] = '0' + p.lambda_count++;
makereference(p.scope, laba, ' ', p.ptr + 1);
return writebyte(0x60) && writeshort(0xffff, 0);
case '}': /* lambda end */
char labb[8] = "lambda0\0";
labb[6] = '0' + p.lambda_stack[--p.lambda_ptr];
if(!makelabel(labb))
return error("Invalid label", labb);
return writebyte(0x6f);
case '[': case '[':
case ']': case ']':
if(slen(w) == 1) break; /* else fallthrough */ if(slen(w) == 1) break; /* else fallthrough */
@ -384,7 +397,7 @@ resolve(void)
Label *l; Label *l;
int i; int i;
Uint16 a; Uint16 a;
for(i = 0; i < p.rlen; i++) { for(i = 0; i < p.refs_len; i++) {
Reference *r = &p.refs[i]; Reference *r = &p.refs[i];
switch(r->rune) { switch(r->rune) {
case '_': case '_':
@ -443,7 +456,7 @@ static void
review(char *filename) review(char *filename)
{ {
int i; int i;
for(i = 0; i < p.llen; i++) for(i = 0; i < p.label_len; i++)
if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z') if(p.labels[i].name[0] >= 'A' && p.labels[i].name[0] <= 'Z')
continue; /* Ignore capitalized labels(devices) */ continue; /* Ignore capitalized labels(devices) */
else if(!p.labels[i].refs) else if(!p.labels[i].refs)
@ -453,8 +466,8 @@ review(char *filename)
filename, filename,
p.length - TRIM, p.length - TRIM,
(p.length - TRIM) / 652.80, (p.length - TRIM) / 652.80,
p.llen, p.label_len,
p.mlen); p.macro_len);
} }
static void static void
@ -467,7 +480,7 @@ writesym(char *filename)
return; return;
fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w"); fp = fopen(scat(scpy(filename, symdst, slen(filename) + 1), ".sym"), "w");
if(fp != NULL) { if(fp != NULL) {
for(i = 0; i < p.llen; i++) { for(i = 0; i < p.label_len; i++) {
Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff; Uint8 hb = p.labels[i].addr >> 8, lb = p.labels[i].addr & 0xff;
fwrite(&hb, 1, 1, fp); fwrite(&hb, 1, 1, fp);
fwrite(&lb, 1, 1, fp); fwrite(&lb, 1, 1, fp);