NEW
  PREFIX
AUTO 4,1
*---------------------------------------
MAC.Init		>SYSCALL SListNew
				bcs .9

				>STA.G MAC.hList
						
				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPMacroBuf
				txa
				>STA.G MAC.hBuf

				>LDYAI 256
				>SYSCALL getmem
				bcs .9
				
				>STYA ZPMacroStk
				txa
				>STA.G MAC.hStk
				
.9				rts					
*---------------------------------------
MAC.Reset		>STZ.G MAC.StkPtr
				>STA.G MAC.CtxID
				rts
*---------------------------------------
MAC.Quit		>LDA.G MAC.hStk
				beq .1

				>SYSCALL FreeMem
				
.1				>LDA.G MAC.hBuf
				beq .3

				>SYSCALL FreeMem
				
.3				>LDA.G MAC.hList
				beq .8
				>SYSCALL SListFree

.8				clc
				rts			
*---------------------------------------
MAC.AddChar		tax
				
				>LDA.G ASM.PASS		If Pass#2, ignore
				bne .8
				
				>LDA.G MAC.BufPtr
				tay
				
				txa
				sta (ZPMacroBuf),y
				>INC.G MAC.BufPtr
				
				txa
				beq .1

				cmp #C.CR
				bne .8
				
.1				>LDA.G MAC.BufPtr
				tay
				lda #0
				
				>PUSHYA
				>PUSHW ZPMacroBuf
				>PUSHW.G MAC.ID
				>LDA.G MAC.hList
				>SYSCALL SListAddData
				rts
				
.8				clc
MAC.AddChar.RTS	rts
*---------------------------------------
MAC.ReadLine	>LDA.G MAC.StkPtr

				tay
				dey
				lda (ZPMacroStk),y
				sta ZPPtr1
				dey
				lda (ZPMacroStk),y
				sta ZPPtr1+1			pop mac ID
				
				dey
				lda (ZPMacroStk),y
				sta ZPPtr2
				dey
				lda (ZPMacroStk),y
				sta ZPPtr2+1			pop offset
				
				dey
				lda (ZPMacroStk),y
				sta ZPMacroBuf
				dey
				lda (ZPMacroStk),y
				sta ZPMacroBuf+1		pop args

				tya
				>STA.G MAC.StkPtr
				
				>PUSHW ZPPtr2			offset
				>PUSHWI 0				len = 0 (string mode)
				>PUSHW ZPLineBuf
				>PUSHW ZPPtr1			Mac ID	
				>LDA.G MAC.hList
				>SYSCALL SListGetData
				bcs MAC.AddChar.RTS

				pha
				tya
*				clc
				adc ZPPtr2
				sta ZPPtr2
				
				pla
				adc ZPPtr2+1
				sta ZPPtr2+1
				
				>LDYA ZPMacroBuf
				>SYSCALL puts
				
				>LDA.G MAC.StkPtr
				tay
				
				lda ZPLinePtr+1		push args
				sta (ZPMacroStk),y
				iny
				
				lda ZPLinePtr
				sta (ZPMacroStk),y
				iny
*---------------------------------------
MAC.Push		lda ZPPtr2+1		push offset
				sta (ZPMacroStk),y
				iny
				
				lda ZPPtr2
				sta (ZPMacroStk),y
				iny					
				
				lda ZPPtr1+1		push MAC.ID
				sta (ZPMacroStk),y
				iny
				
				lda ZPPtr1
				sta (ZPMacroStk),y
				iny					
				
				tya
				>STA.G MAC.StkPtr
				
				clc
				rts
*---------------------------------------
MAC.Exec		jsr SRC.GetNextChar		skip '>'
				bcs .90

				>PUSHW ZPLinePtr
				>LDA.G MAC.hList
				>SYSCALL SListLookup
				bcs .90
				>STYA ZPPtr1			MAC.ID
				
				txa
*				clc
				adc ZPLinePtr
				sta ZPLinePtr
				bcc .1
				
				inc ZPLinePtr+1
				
.1				jsr SRC.GetChar
				bcs .8
				
				cmp #' '
				bne .92
				
				jsr SRC.GetNextChar
				beq .8					eol..
				
				cmp #' '				some arg...
				bne .8
					
.2				jsr SRC.GetNextChar		comment....skip
				bcc .2
				
.8				>LDA.G MAC.StkPtr
				cmp #MA.MAXDEPTH*8
				beq .91
				
				stz ZPPtr2
				stz ZPPtr2+1			offset = 0
				
				>INC.G MAC.CtxID
				>INC.G MAC.Depth
				
				>LDA.G MAC.StkPtr
				tay
				
				lda ZPLinePtr+1			push args
				sta (ZPMacroStk),y
				iny
				
				lda ZPLinePtr
				sta (ZPMacroStk),y
				iny

				jmp MAC.Push
							
.90				lda #E.MACRO.INV
				sec
				rts	
				
.91				lda #E.MACRO.TOO.MANY
				sec
				rts
				
.92				lda #E.SYNTAX.ERROR
				sec
				rts	
*---------------------------------------
MAN
SAVE USR/SRC/BIN/ASM.S.MAC
LOAD USR/SRC/BIN/ASM.S
ASM
