Browse code

Added better templating

neauoire authored on 22/04/2021 21:29:48
Showing 2 changed files
... ...
@@ -6,6 +6,8 @@
6 6
 %--  { #0001 SUB2 }
7 7
 %ABS2 { DUP2 #000f SFT2 EQU #04 JNZ #ffff MUL2 }
8 8
 
9
+%%->x { .%/x PEK2 }
10
+
9 11
 ( devices )
10 12
 
11 13
 |00 @System     [ &vector $2 &pad     $6 &r      $2 &g     $2 &b      $2 ]
... ...
@@ -26,15 +28,12 @@
26 28
 |0100 ( -> )
27 29
 
28 30
 	( theme ) 
29
-	#0ffd .System/r DEO2 
30
-	#0f03 .System/g DEO2 
31
-	#0f02 .System/b DEO2
31
+	#f0fd .System/r DEO2 
32
+	#f003 .System/g DEO2 
33
+	#f002 .System/b DEO2
32 34
 
33 35
 	( vectors ) ;on-mouse .Mouse/vector DEO2
34 36
 
35
-	#0010 #0030 #0050 #0070 #01 ;hairline JSR2
36
-	#0000 #0000 #0020 #0090 #01 ;hairline JSR2
37
-
38 37
 BRK
39 38
 
40 39
 @on-mouse ( -> )
... ...
@@ -56,25 +55,28 @@ BRK
56 55
 
57 56
 @on-mouse-down ( -> )
58 57
 
59
-	( record last position )
60
-	.Mouse/x DEI2 .pointer/x POK2 
61
-	.Mouse/y DEI2 .pointer/y POK2
62
-
63
-	.Mouse/x DEI2 .pointer/lastx POK2 
64
-	.Mouse/y DEI2 .pointer/lasty POK2
65
-
58
+	( record start position )
59
+	.Mouse/x DEI2 DUP2 .pointer/x POK2 .pointer/lastx POK2 
60
+	.Mouse/y DEI2 DUP2 .pointer/y POK2 .pointer/lasty POK2
66 61
 	.Mouse/state DEI .pointer/state POK
62
+	
63
+	pointer->x .Console/short DEO2
67 64
 
68 65
 BRK
69 66
 
70 67
 @on-mouse-drag ( -> )
71 68
 	
72
-	.pointer/lastx PEK2 .pointer/lasty PEK2 .pointer/x PEK2 .pointer/y PEK2 #01 .Mouse/state DEI #10 EQU #02 MUL ADD ;hairline JSR2
69
+	( draw line )
70
+	.pointer/lastx PEK2 
71
+	.pointer/lasty PEK2 
72
+	.pointer/x PEK2 
73
+	.pointer/y PEK2 
74
+	#01 ( add mouse state ) [ .Mouse/state DEI #10 EQU #02 MUL ADD ] 
75
+	;hairline JSR2
73 76
 
74 77
 	( record last position )
75 78
 	.Mouse/x DEI2 .pointer/lastx POK2 
76 79
 	.Mouse/y DEI2 .pointer/lasty POK2
77
-
78 80
 	.Mouse/state DEI .pointer/state POK
79 81
 
80 82
 BRK
... ...
@@ -48,10 +48,12 @@ char ops[][4] = {
48 48
 
49 49
 int   scin(char *s, char c) { int i = 0; while(s[i]) if(s[i++] == c) return i - 1; return -1; } /* string char index */
50 50
 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 */
51
-int   slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
52 51
 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 */
52
+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 */
53 53
 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 */
54
+int   slen(char *s) { int i = 0; while(s[i] && s[++i]) ; return i; } /* string length */
54 55
 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 */
56
+char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
55 57
 
56 58
 #pragma mark - Helpers
57 59
 
... ...
@@ -90,6 +92,10 @@ findmacro(char *name)
90 92
 	for(i = 0; i < p.mlen; ++i)
91 93
 		if(scmp(p.macros[i].name, name, 64))
92 94
 			return &p.macros[i];
95
+	/* look for templated */
96
+	for(i = 0; i < p.mlen; ++i)
97
+		if(p.macros[i].name[0] == '%' && ssin(name, p.macros[i].name + 1) != -1)
98
+			return &p.macros[i];
93 99
 	return NULL;
94 100
 }
95 101
 
... ...
@@ -126,6 +132,19 @@ findopcode(char *s)
126 132
 	return 0;
127 133
 }
128 134
 
135
+char *template(char *src, char *dst, char *w, Macro *m)
136
+{
137
+	char input[64];
138
+	if(scin(src, '%') == -1)
139
+		return src;
140
+	scpy(w, input, ssin(w, m->name + 1) + 1);
141
+	scpy(src, dst, scin(src, '%') + 1);
142
+	scat(dst, input);
143
+	scat(dst, src + scin(src, '%') + 1);
144
+	printf("  Templated %s, to %s\n", w, dst);
145
+	return dst;
146
+}
147
+
129 148
 char *
130 149
 sublabel(char *src, char *scope, char *name)
131 150
 {
... ...
@@ -220,8 +239,9 @@ walktoken(char *w)
220 239
 	}
221 240
 	if((m = findmacro(w))) {
222 241
 		int i, res = 0;
242
+		char templated[64];
223 243
 		for(i = 0; i < m->len; ++i)
224
-			res += walktoken(m->items[i]);
244
+			res += walktoken(template(m->items[i], templated, w, m));
225 245
 		return res;
226 246
 	}
227 247
 	return error("Unknown label in first pass", w);
... ...
@@ -270,11 +290,12 @@ parsetoken(char *w)
270 290
 		return 1;
271 291
 	} else if((m = findmacro(w))) {
272 292
 		int i;
273
-		m->refs++;
274
-		for(i = 0; i < m->len; ++i)
275
-			if(!parsetoken(m->items[i]))
293
+		for(i = 0; i < m->len; ++i) {
294
+			char templated[64];
295
+			if(!parsetoken(template(m->items[i], templated, w, m)))
276 296
 				return 0;
277
-		return 1;
297
+		}
298
+		return ++m->refs;
278 299
 	} else if(sihx(w)) {
279 300
 		if(slen(w) == 2)
280 301
 			pushbyte(shex(w), 0);