Browse code

Implemented includes in asma

Andrew Alderwick authored on 06/10/2021 05:14:18
Showing 1 changed files
... ...
@@ -99,6 +99,9 @@
99 99
 	;asma/src-filename LDA2 ;asma-assemble-file-pass JSR2
100 100
 	asma-IF-ERROR ,&error JCN
101 101
 
102
+	( flush output buffer )
103
+	;asma-output/ptr LDA2 ;asma-write-buffer SUB2 ;asma/flush-fn LDA2 JSR2
104
+
102 105
 	;asma-trees/labels ;asma-print-labels JSR2 ( DEBUG )
103 106
 	;asma-print-line-count JSR2 ( DEBUG )
104 107
 	;asma-print-heap-usage JSR2 ( DEBUG )
... ...
@@ -270,9 +273,6 @@
270 273
 
271 274
 	asma-IF-ERROR ,&error JCN
272 275
 
273
-	( flush output buffer )
274
-	;asma-output/ptr LDA2 ;asma-write-buffer SUB2 ;asma/flush-fn LDA2 JSR2
275
-
276 276
 	&error
277 277
 	POP2 POP2 POP2 POP2 POP2
278 278
 	JMP2r
... ...
@@ -288,45 +288,56 @@ include projects/library/file-read-chunks.tal
288 288
 	of the last token in the chunk.
289 289
 )
290 290
 
291
-@asma-assemble-chunk ( line^ ptr* len^ -- assembled-up-to-ptr* )
292
-	ROT2k ( line^ ptr* len^ ptr* len^ line^ )
293
-	;asma/line STA2 ( line^ ptr* len^ ptr* len^ )
294
-	ADD2 #0001 SUB2 NIP2 ( line^ ptr* last-ptr* )
295
-	SWP2 STH2k ( line^ last-ptr* ptr* / ptr* )
296
-	,&loop JMP
297
-
298
-	&next-char-pop ( line^ last-ptr* ptr* char / start-of-token* )
299
-	POP
300
-	INC2
301
-	&loop ( line^ last-ptr* ptr* / start-of-token* )
302
-	LTH2k ,&end JCN
303
-	LDAk ( line^ last-ptr* ptr* char / start-of-token* )
304
-	DUP #20 GTH ,&next-char-pop JCN
305
-
306
-	#00 OVR2 ( line^ last-ptr* ptr* char 00 ptr* / start-of-token* )
307
-	STA STH2r ( line^ last-ptr* ptr* char start-of-token* )
308
-	,asma-assemble-token JSR ( line^ last-ptr* ptr* char )
291
+@asma-assemble-chunk ( line^ chunk* len^ -- line^ assembled-up-to-chunk* )
292
+	ROT2 STH2 ( chunk* len^ / line^ )
293
+	OVR2 ADD2 ( chunk* end-chunk* / line^ )
294
+	OVR2 ;asma-read-buffer EQU2 STH
295
+	DUP2 ;asma-read-buffer/end NEQ2
296
+	STHr AND ;asma/eof STA
297
+	SWP2 STH2k ( end-chunk* chunk* / line^ start-of-token* )
298
+
299
+	&loop ( end-chunk* char* / line^ start-of-token* )
300
+	LDAk #21 LTH ,&whitespace JCN
301
+	INC2 ,&loop JMP
302
+
303
+	&whitespace ( end-chunk* ws-char* / line^ start-of-token* )
304
+	GTH2k ,&within-chunk JCN
305
+	;asma/eof LDA ,&eof JCN
306
+
307
+	( reached the end of the chunk, start-of-token* is where we assembled up to )
308
+	POP2 POP2 STH2r STH2r SWP2 JMP2r
309
+
310
+	&within-chunk ( end-chunk* ws-char* / line^ start-of-token* )
311
+	LDAk #0a NEQ ( end-chunk* ws-char* not-newline / line^ start-of-token* )
312
+	#00 OVR2 STA
313
+	STH2r ,asma-assemble-token JSR ( end-chunk* ws-char* not-newline / line^ )
314
+	JMP INC2r ( end-chunk* ws-char* / line^ )
309 315
 	asma-IF-ERROR ,&error JCN
310
-
311
-	#0a NEQ ,&not-newline JCN ( line^ last-ptr* ptr* )
312
-	ROT2 INC2 ( last-ptr* ptr* line+1^ )
313
-	DUP2 ;asma/line STA2
314
-	ROT2 ROT2 ( line^ last-ptr* ptr* )
315
-	&not-newline
316
-
317
-	INC2 DUP2 STH2 ( line^ last-ptr* ptr* / start-of-token* )
316
+	;asma/break LDA ,&break JCN
317
+	INC2 STH2k ( end-chunk* start-of-token* / line^ start-of-token* )
318 318
 	,&loop JMP
319 319
 
320
-	&end
321
-	POP2 POP2 STH2r
322
-	JMP2r
320
+	&break ( end-chunk* ws-char* / line^ )
321
+	( the read buffer has been invalidated, ws-char* plus one is where we assembled up to )
322
+	;asma/break LDA #01 SUB ;asma/break STA
323
+	INC2 NIP2 ( assembled-up-to-ptr* / line^ )
324
+	STH2r SWP2 JMP2r
323 325
 
324
-	&error
325
-	POP POP2 POP2
326
+	&error ( end-chunk* ws-char* / line^ )
327
+	( return no progress with assembly to make file-read-chunks exit )
328
+	POP2 POP2 STH2r ;asma-read-buffer
326 329
 	JMP2r
327 330
 
331
+	&eof ( end-chunk* ws-char* / line^ start-of-token* )
332
+	( reached the end of file, end-chunk* is safe to write since the buffer is bigger )
333
+	( return no progress with assembly to make file-read-chunks exit )
334
+	POP2 ( end-chunk* / line^ start-of-token* )
335
+	#00 ROT ROT STA ( / line^ start-of-token* )
336
+	STH2r ,asma-assemble-token JSR ( / line^ )
337
+	STH2r ;asma-read-buffer JMP2r
338
+
328 339
 @asma [
329
-	&pass $1 &state $1 &line $2
340
+	&pass $1 &state $1 &line $2 &break $1 &eof $1
330 341
 	&token $2 &orig-token $2
331 342
 	&heap $2 &addr $2 &written-addr $2 &flush-fn $2
332 343
 	&src-filename $2 &dest-filename $2
... ...
@@ -338,10 +349,11 @@ include projects/library/file-read-chunks.tal
338 349
 	The main routine to assemble a single token.
339 350
 	asma/state contains several meaningful bits:
340 351
 	0x02 we are in a comment,
341
-	0x04 we are in a macro body, and
342
-	0x08 we are in a macro body that we are ignoring
352
+	0x04 we are in a macro body,
353
+	0x08 we are expecting an include filename, and
354
+	0x10 we are in a macro body that we are ignoring
343 355
 	   (because the macro was already defined in a previous pass).
344
-	Since 0x08 never appears without 0x04, the lowest bit set in asma/state is
356
+	Since 0x10 never appears without 0x04, the lowest bit set in asma/state is
345 357
 	always 0x00, 0x02, or 0x04, which is very handy for use with jump tables.
346 358
 	The lowest bit set can be found easily by #00 (n) SUBk AND.
347 359
 )
... ...
@@ -384,11 +396,15 @@ include projects/library/file-read-chunks.tal
384 396
 		:asma-first-char-normal/_entry
385 397
 		:asma-first-char-comment/_entry
386 398
 		:asma-first-char-macro/_entry
399
+		$2 ( invalid position )
400
+		$2 ( empty tree for include )
387 401
 
388 402
 	&body-routines
389 403
 		:asma-normal-body
390 404
 		:asma-ignore
391 405
 		:asma-macro-body
406
+		$2 ( invalid position )
407
+		:asma-include-filename
392 408
 
393 409
 @asma-parse-hex-digit ( charcode -- 00-0f if valid hex
394 410
                                  OR 10-ff otherwise )
... ...
@@ -644,18 +660,18 @@ include projects/library/file-read-chunks.tal
644 660
 	JMP2r
645 661
 
646 662
 	&ignore-macro
647
-	#0c asma-STATE-SET
663
+	#14 asma-STATE-SET
648 664
 	JMP2r
649 665
 
650 666
 @asma-macro-body
651
-	;asma/state LDA #08 AND ,&skip JCN
667
+	;asma/state LDA #10 AND ,&skip JCN
652 668
 	;asma/token LDA2 ;asma-append-heap-string JSR2
653 669
 	&skip
654 670
 	JMP2r
655 671
 
656 672
 @asma-macro-end
657 673
 	#00 ;asma-append-heap-byte JSR2
658
-	#0c asma-STATE-CLEAR
674
+	#14 asma-STATE-CLEAR
659 675
 	JMP2r
660 676
 
661 677
 @asma-label-define
... ...
@@ -706,6 +722,7 @@ include projects/library/file-read-chunks.tal
706 722
 	,&valid JCN
707 723
 
708 724
 	;asma-msg-hex ;asma/error STA2
725
+	POP2
709 726
 	JMP2r
710 727
 
711 728
 	&valid
... ...
@@ -860,14 +877,30 @@ include projects/library/file-read-chunks.tal
860 877
 	,&macro-loop JMP
861 878
 
862 879
 	&not-macro
880
+	POP2
881
+	;&include-string ;asma-strcmp-tree JSR2 ,&not-include JCN
882
+	POP2 ( discard dummy after-node-key* )
883
+	#08 asma-STATE-SET
884
+	JMP2r
885
+
886
+	&not-include
863 887
 	;asma-msg-label ;asma/error STA2
864 888
 	&error
865
-	POP2
866 889
 	JMP2r
867 890
 
868 891
 	&too-deep
869
-	;asma-msg-too-deep ;asma/error STA2
870 892
 	POP2
893
+	;asma-msg-too-deep ;asma/error STA2
894
+	JMP2r
895
+
896
+	&include-string "include 00
897
+
898
+@asma-include-filename
899
+	#08 asma-STATE-CLEAR
900
+	;asma/heap LDA2
901
+		;asma/token LDA2 ;asma-append-heap-string JSR2
902
+		;asma-assemble-file-pass JSR2
903
+	;asma/break LDAk INC ROT ROT STA
871 904
 	JMP2r
872 905
 
873 906
 ( Error messages )