Added better templating

This commit is contained in:
neauoire 2021-04-22 14:29:48 -07:00
parent b752f5f2bc
commit 62e3d75883
2 changed files with 44 additions and 21 deletions

View File

@ -6,6 +6,8 @@
%-- { #0001 SUB2 }
%ABS2 { DUP2 #000f SFT2 EQU #04 JNZ #ffff MUL2 }
%%->x { .%/x PEK2 }
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 ]
@ -26,15 +28,12 @@
|0100 ( -> )
( theme )
#0ffd .System/r DEO2
#0f03 .System/g DEO2
#0f02 .System/b DEO2
#f0fd .System/r DEO2
#f003 .System/g DEO2
#f002 .System/b DEO2
( vectors ) ;on-mouse .Mouse/vector DEO2
#0010 #0030 #0050 #0070 #01 ;hairline JSR2
#0000 #0000 #0020 #0090 #01 ;hairline JSR2
BRK
@on-mouse ( -> )
@ -56,25 +55,28 @@ BRK
@on-mouse-down ( -> )
( record last position )
.Mouse/x DEI2 .pointer/x POK2
.Mouse/y DEI2 .pointer/y POK2
.Mouse/x DEI2 .pointer/lastx POK2
.Mouse/y DEI2 .pointer/lasty POK2
( record start position )
.Mouse/x DEI2 DUP2 .pointer/x POK2 .pointer/lastx POK2
.Mouse/y DEI2 DUP2 .pointer/y POK2 .pointer/lasty POK2
.Mouse/state DEI .pointer/state POK
pointer->x .Console/short DEO2
BRK
@on-mouse-drag ( -> )
.pointer/lastx PEK2 .pointer/lasty PEK2 .pointer/x PEK2 .pointer/y PEK2 #01 .Mouse/state DEI #10 EQU #02 MUL ADD ;hairline JSR2
( draw line )
.pointer/lastx PEK2
.pointer/lasty PEK2
.pointer/x PEK2
.pointer/y PEK2
#01 ( add mouse state ) [ .Mouse/state DEI #10 EQU #02 MUL ADD ]
;hairline JSR2
( record last position )
.Mouse/x DEI2 .pointer/lastx POK2
.Mouse/y DEI2 .pointer/lasty POK2
.Mouse/state DEI .pointer/state POK
BRK

View File

@ -48,10 +48,12 @@ char ops[][4] = {
int scin(char *s, char c) { int i = 0; while(s[i]) if(s[i++] == c) return i - 1; return -1; } /* string char index */
int scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i] && i < len) if(!a[i++]) return 1; return 0; } /* string compare */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
int sihx(char *s) { int i = 0; char c; while((c = s[i++])) if(!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f')) return 0; return 1; } /* string is hexadecimal */
int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */
int shex(char *s) { int n = 0, i = 0; char c; while((c = s[i++])) if(c >= '0' && c <= '9') n = n * 16 + (c - '0'); else if(c >= 'a' && c <= 'f') n = n * 16 + 10 + (c - 'a'); return n; } /* string to num */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
#pragma mark - Helpers
@ -90,6 +92,10 @@ findmacro(char *name)
for(i = 0; i < p.mlen; ++i)
if(scmp(p.macros[i].name, name, 64))
return &p.macros[i];
/* look for templated */
for(i = 0; i < p.mlen; ++i)
if(p.macros[i].name[0] == '%' && ssin(name, p.macros[i].name + 1) != -1)
return &p.macros[i];
return NULL;
}
@ -126,6 +132,19 @@ findopcode(char *s)
return 0;
}
char *template(char *src, char *dst, char *w, Macro *m)
{
char input[64];
if(scin(src, '%') == -1)
return src;
scpy(w, input, ssin(w, m->name + 1) + 1);
scpy(src, dst, scin(src, '%') + 1);
scat(dst, input);
scat(dst, src + scin(src, '%') + 1);
printf(" Templated %s, to %s\n", w, dst);
return dst;
}
char *
sublabel(char *src, char *scope, char *name)
{
@ -220,8 +239,9 @@ walktoken(char *w)
}
if((m = findmacro(w))) {
int i, res = 0;
char templated[64];
for(i = 0; i < m->len; ++i)
res += walktoken(m->items[i]);
res += walktoken(template(m->items[i], templated, w, m));
return res;
}
return error("Unknown label in first pass", w);
@ -270,11 +290,12 @@ parsetoken(char *w)
return 1;
} else if((m = findmacro(w))) {
int i;
m->refs++;
for(i = 0; i < m->len; ++i)
if(!parsetoken(m->items[i]))
for(i = 0; i < m->len; ++i) {
char templated[64];
if(!parsetoken(template(m->items[i], templated, w, m)))
return 0;
return 1;
}
return ++m->refs;
} else if(sihx(w)) {
if(slen(w) == 2)
pushbyte(shex(w), 0);