Browse code

Implementing better macros

neauoire authored on 13/03/2021 18:31:29
Showing 3 changed files
... ...
@@ -24,17 +24,17 @@ typedef struct {
24 24
 typedef struct {
25 25
 	Uint8 len, length[16], size, refs;
26 26
 	char name[64], params[16][64];
27
-} Macro;
27
+} Template;
28 28
 
29 29
 typedef struct {
30 30
 	Uint8 len, offset, refs;
31 31
 	Uint16 addr;
32 32
 	char name[64];
33
-	Macro *macro;
33
+	Template *template;
34 34
 } Label;
35 35
 
36
-int macroslen;
37
-Macro macros[256];
36
+int templateslen;
37
+Template templates[256];
38 38
 
39 39
 int labelslen;
40 40
 Label labels[256];
... ...
@@ -91,13 +91,13 @@ pushtext(char *s, int lit)
91 91
 		pushbyte(c, 0);
92 92
 }
93 93
 
94
-Macro *
95
-findmacro(char *s)
94
+Template *
95
+findtemplate(char *s)
96 96
 {
97 97
 	int i;
98
-	for(i = 0; i < macroslen; ++i)
99
-		if(scmp(macros[i].name, s, 64))
100
-			return &macros[i];
98
+	for(i = 0; i < templateslen; ++i)
99
+		if(scmp(templates[i].name, s, 64))
100
+			return &templates[i];
101 101
 	return NULL;
102 102
 }
103 103
 
... ...
@@ -122,12 +122,12 @@ findlabeladdr(char *s)
122 122
 	if(scin(s, '.') < 1)
123 123
 		return l->addr;
124 124
 	param = s + scin(s, '.') + 1;
125
-	for(i = 0; i < l->macro->len; ++i) {
126
-		if(scmp(l->macro->params[i], param, 64))
125
+	for(i = 0; i < l->template->len; ++i) {
126
+		if(scmp(l->template->params[i], param, 64))
127 127
 			return l->addr + o;
128
-		o += l->macro->length[i];
128
+		o += l->template->length[i];
129 129
 	}
130
-	printf("!!! Warning %s.%s[%s]\n", l->name, param, l->macro->name);
130
+	printf("!!! Warning %s.%s[%s]\n", l->name, param, l->template->name);
131 131
 	return 0;
132 132
 }
133 133
 
... ...
@@ -140,10 +140,10 @@ findlabellen(char *s)
140 140
 	if(scin(s, '.') < 1)
141 141
 		return l->len;
142 142
 	param = s + scin(s, '.') + 1;
143
-	for(i = 0; i < l->macro->len; ++i)
144
-		if(scmp(l->macro->params[i], param, 64))
145
-			return l->macro->length[i];
146
-	printf("!!! Warning %s.%s[%s]\n", l->name, param, l->macro->name);
143
+	for(i = 0; i < l->template->len; ++i)
144
+		if(scmp(l->template->params[i], param, 64))
145
+			return l->template->length[i];
146
+	printf("!!! Warning %s.%s[%s]\n", l->name, param, l->template->name);
147 147
 	return 0;
148 148
 }
149 149
 
... ...
@@ -186,18 +186,18 @@ error(char *name, char *id)
186 186
 }
187 187
 
188 188
 int
189
-makemacro(char *name, FILE *f)
189
+maketemplate(char *name, FILE *f)
190 190
 {
191 191
 	Uint8 mode = 0;
192
-	Macro *m;
192
+	Template *m;
193 193
 	char wv[64];
194
-	if(findmacro(name))
195
-		return error("Macro duplicate", name);
194
+	if(findtemplate(name))
195
+		return error("Template duplicate", name);
196 196
 	if(sihx(name) && slen(name) % 2 == 0)
197
-		return error("Macro name is hex number", name);
197
+		return error("Template name is hex number", name);
198 198
 	if(findopcode(name))
199
-		return error("Macro name is invalid", name);
200
-	m = &macros[macroslen++];
199
+		return error("Template name is invalid", name);
200
+	m = &templates[templateslen++];
201 201
 	scpy(name, m->name, 64);
202 202
 	while(fscanf(f, "%s", wv)) {
203 203
 		if(wv[0] == '{')
... ...
@@ -213,12 +213,12 @@ makemacro(char *name, FILE *f)
213 213
 		}
214 214
 		mode = !mode;
215 215
 	}
216
-	printf("New macro: %s[%d:%d]\n", name, m->len, m->size);
216
+	printf("New template: %s[%d:%d]\n", name, m->len, m->size);
217 217
 	return 1;
218 218
 }
219 219
 
220 220
 int
221
-makelabel(char *name, Uint16 addr, Uint8 len, Macro *m)
221
+makelabel(char *name, Uint16 addr, Uint8 len, Template *m)
222 222
 {
223 223
 	Label *l;
224 224
 	if(findlabel(name))
... ...
@@ -233,35 +233,27 @@ makelabel(char *name, Uint16 addr, Uint8 len, Macro *m)
233 233
 	l->refs = 0;
234 234
 	scpy(name, l->name, 64);
235 235
 	if(m)
236
-		l->macro = m;
236
+		l->template = m;
237 237
 	printf("New label: %s, at 0x%04x[%d]\n", l->name, l->addr, l->len);
238 238
 	return 1;
239 239
 }
240 240
 
241
-int
242
-makeconst(char *id, FILE *f)
243
-{
244
-	char wv[64];
245
-	fscanf(f, "%s", wv);
246
-	return makelabel(id, shex(wv), 1, 0);
247
-}
248
-
249 241
 int
250 242
 makevariable(char *id, Uint16 *addr, FILE *f)
251 243
 {
252 244
 	char wv[64];
253 245
 	Uint16 origin;
254 246
 	Uint8 len;
255
-	Macro *m = NULL;
247
+	Template *m = NULL;
256 248
 	fscanf(f, "%s", wv);
257 249
 	origin = *addr;
258 250
 	if(sihx(wv))
259 251
 		len = shex(wv);
260
-	else if((m = findmacro(wv))) {
252
+	else if((m = findtemplate(wv))) {
261 253
 		len = m->size;
262 254
 		m->refs++;
263 255
 	} else
264
-		return error("Invalid macro", wv);
256
+		return error("Invalid template", wv);
265 257
 	*addr += len;
266 258
 	return makelabel(id, origin, len, m);
267 259
 }
... ...
@@ -305,10 +297,7 @@ pass1(FILE *f)
305 297
 			if(!makevariable(w + 1, &addr, f))
306 298
 				return error("Pass1 failed", w);
307 299
 		} else if(w[0] == '&') {
308
-			if(!makemacro(w + 1, f))
309
-				return error("Pass1 failed", w);
310
-		} else if(w[0] == ':') {
311
-			if(!makeconst(w + 1, f))
300
+			if(!maketemplate(w + 1, f))
312 301
 				return error("Pass1 failed", w);
313 302
 		} else if(findopcode(w) || scmp(w, "BRK", 4))
314 303
 			addr += 1;
... ...
@@ -338,7 +327,7 @@ pass1(FILE *f)
338 327
 int
339 328
 pass2(FILE *f)
340 329
 {
341
-	int ccmnt = 0, cbits = 0, cmacro = 0;
330
+	int ccmnt = 0, cbits = 0, ctemplate = 0;
342 331
 	char w[64], scope[64], subw[64];
343 332
 	printf("Pass 2\n");
344 333
 	while(fscanf(f, "%s", w) == 1) {
... ...
@@ -355,7 +344,7 @@ pass2(FILE *f)
355 344
 			scpy(subw, w + 1, 64);
356 345
 		}
357 346
 		if(skipblock(w, &ccmnt, '(', ')')) continue;
358
-		if(skipblock(w, &cmacro, '{', '}')) continue;
347
+		if(skipblock(w, &ctemplate, '{', '}')) continue;
359 348
 		/* clang-format off */
360 349
 		if(skipblock(w, &cbits, '[', ']')) {
361 350
 			if(w[0] == '[' || w[0] == ']') { continue; }
... ...
@@ -395,9 +384,9 @@ cleanup(void)
395 384
 	for(i = 0; i < labelslen; ++i)
396 385
 		if(!labels[i].refs)
397 386
 			printf("--- Unused label: %s\n", labels[i].name);
398
-	for(i = 0; i < macroslen; ++i)
399
-		if(!macros[i].refs)
400
-			printf("--- Unused macro: %s\n", macros[i].name);
387
+	for(i = 0; i < templateslen; ++i)
388
+		if(!templates[i].refs)
389
+			printf("--- Unused template: %s\n", templates[i].name);
401 390
 }
402 391
 
403 392
 int
... ...
@@ -21,4 +21,4 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr
21 21
 
22 22
 # run
23 23
 ./bin/assembler projects/software/left.usm bin/boot.rom
24
-./bin/emulator bin/boot.rom
24
+# ./bin/emulator bin/boot.rom
... ...
@@ -6,6 +6,9 @@
6 6
 		- Double-click select word
7 7
 		- Right-click find next instance of selection
8 8
 		- Draw tab characters
9
+		- Don't redraw if nothing has changed
10
+		- Input on selection should erase selection range
11
+		- Erase on selection should erase selection range
9 12
 		- Scrollbar
10 13
 			- Don't scroll past oef
11 14
 			- Hor scroll
... ...
@@ -33,7 +36,6 @@
33 36
 ;i 2 ;j 2 ;k 1 ;l 1 ;addr 2
34 37
 
35 38
 ;selection Range2d ;position Point2d ;scroll Point2d
36
-
37 39
 ;pt Point2d ;mouse Point2d ;touch Touch2d
38 40
 ;textarea Textarea2d
39 41
 ;label Label2d ( remove )
... ...
@@ -42,7 +44,6 @@
42 44
 	
43 45
 	( load file )
44 46
 	,filepath ,load-file JSR2
45
-
46 47
 	( place textarea )
47 48
 	#0018 =textarea.x1 ~SCRN.height #0008 SUB2 =textarea.y2
48 49
 
... ...
@@ -72,11 +73,11 @@ BRK
72 73
 		,no-ctrl-left ~CTRL #40 NEQ JMP2? POP2
73 74
 			( clamp ) ,no-ctrl-left ~selection.from ,document.body EQU2 JMP2? POP2
74 75
 			~selection.from #0001 SUB2 DUP2 =selection.from #0001 ADD2 =selection.to
75
-			,clamp-selection JSR2,follow-selection JSR2 ,redraw JSR2 ,ctrl-end JMP2
76
+			,clamp-selection JSR2 ,follow-selection JSR2 ,redraw JSR2 ,ctrl-end JMP2
76 77
 		@no-ctrl-left
77 78
 		,no-ctrl-right ~CTRL #80 NEQ JMP2? POP2
78 79
 			~selection.from #0001 ADD2 DUP2 =selection.from #0001 ADD2 =selection.to
79
-			,clamp-selection JSR2,follow-selection JSR2 ,redraw JSR2 ,ctrl-end JMP2
80
+			,clamp-selection JSR2 ,follow-selection JSR2 ,redraw JSR2 ,ctrl-end JMP2
80 81
 		@no-ctrl-right
81 82
 		( alt )
82 83
 		,no-alt ~CTRL #0f AND #02 NEQ JMP2? POP2
... ...
@@ -119,27 +120,33 @@ BRK
119 120
 
120 121
 	( keys )
121 122
 
122
-	,keys-end ~KEYS #00 EQU JMP2? POP2
123
+	,no-keys ~KEYS #00 EQU JMP2? POP2
123 124
 
124
-		,no-backspace ~KEYS #08 NEQ JMP2? POP2
125
+		,$no-backspace ~KEYS #08 NEQ JMP2? POP2
125 126
 			( erase )
126
-			~selection.to ~selection.from SUB2 ,shift-left JSR2
127
+			,$erase-multiple ~selection.to ~selection.from SUB2 #0001 NEQ2 JMP2? POP2
128
+			$erase-single
129
+				~selection.to ~selection.from SUB2 ,shift-left JSR2
130
+				,$erase-end JMP2
131
+			$erase-multiple
132
+				~selection.from #0001 ADD2 =selection.from
133
+				~selection.to ~selection.from SUB2 #0001 ADD2 ,shift-left JSR2
134
+			$erase-end
127 135
 			~selection.from #0001 SUB2 =selection.from
128
-			~selection.from #0001 ADD2 =selection.to
129
-			( release ) #00 =KEYS
130
-			,redraw JSR2
131
-			,keys-end JMP2
132
-		@no-backspace
136
+			,$keys-end JMP2
137
+		$no-backspace
133 138
 
134 139
 		( insert )
135 140
 		~selection.to ~selection.from SUB2 ,shift-right JSR2
136 141
 		~KEYS ~selection.from STR
137 142
 		~selection.from #0001 ADD2 =selection.from
138
-		~selection.from #0001 ADD2 =selection.to
139
-		( release ) #00 =KEYS
140
-		,redraw JSR2
141 143
 
142
-	@keys-end
144
+		$keys-end
145
+			~selection.from #0001 ADD2 =selection.to
146
+			( release ) #00 =KEYS
147
+			,redraw JSR2
148
+
149
+	@no-keys
143 150
 
144 151
 	( mouse )
145 152
 
... ...
@@ -532,7 +539,7 @@ RTS
532 539
 		( draw ) #01 
533 540
 		~i ~selection.from #0001 SUB2 GTH2 
534 541
 		~i ~selection.to LTH2 #0101 EQU2
535
-		#05 MUL ADD =SPRT.color
542
+		#05 MUL ADD ~i ~selection.from EQU2 ADD =SPRT.color
536 543
 
537 544
 		,$no-linebreak ~i LDR #0a NEQ ~i LDR #0d NEQ #0101 EQU2 JMP2? POP2
538 545
 			( draw linebreak )
... ...
@@ -578,10 +585,10 @@ RTS
578 585
 	#0000 =SPRT.y
579 586
 	,scrollbar_bg =SPRT.addr
580 587
 
581
-	$loop
588
+	$loop NOP
582 589
 	( draw ) #08 =SPRT.color
583 590
 	( incr ) ~SPRT.y #0008 ADD2 =SPRT.y
584
-	,$loop ~SPRT.y ~SCRN.height LTH2 JMP2? POP2
591
+	~SPRT.y ~SCRN.height LTH2 ^$loop MUL JMPS
585 592
 
586 593
 	#0000 =SPRT.y
587 594
 	,arrowup_icn =SPRT.addr