[cc65] if/else/endif macros

From: 42Bastian Schick <bastian421monlynx.de>
Date: 2005-05-11 07:29:26
Hi all (cc65 and lynx group),

I played a bit with the .scope feature of ca65 and found it can be nicely 
used
to implement if/else/endif macros.
Even more, the .match,.tcount etc. functions allow more nice things.

Here a very simple one as a starter:
	.macro ifne arg
	.scope
	cmp arg
	beq	_else
	.endmacro

	.macro else
	bra	_endif
_else:
	.endmacro
	
	.macro endif
  .ifndef _else
_else:
  .endif
_endif:
	.endscope
	.endmacro

For a test:
	ifne 10
	ldx #10
	else
	ldx #20
	endif

I am working on a more complicated version of the if, but currently get an
"Range Error" I cannot solve ...

	.macro if arg
	.scope
	.if .blank(arg)
	.error "Macro 'if' needs parameter(s) !"
	.endif
	.out .string(=.tcount(arg))
	.if .tcount(arg) = 1
	  .if (.match(.mid(0,1,arg),A))
	  cmp #0
	  .elseif .match(.mid(0,1,arg),X)
	  cpx #0
	  .elseif .match(.mid(0,1,arg),Y)
	  cpy #0
	  .else
	  .error "Syntax: if (A|X|Y) !"
	  .endif
	  beq _else
         .elseif .tcount(arg) >= 3
	   .if .match(.mid(1,1,arg),=)
	      .if .match(.mid(0,1,arg),A)
		cmp .right(.tcount(arg)-2,arg)
	      .elseif .match(.mid(0,1,arg),X)
		cpx .right(.tcount(arg)-2,arg)
	      .elseif .match(.mid(0,1,arg),Y)
		cpy .right(.tcount(arg)-2,arg)
	      .else
		.error "Syntax: if (A|X|Y) = expr"
	      .endif
	      bne _else
	   .elseif .match(.mid(1,1,arg),!) .and .match(.mid(2,1,arg),=)
	      .if .match(.mid(0,1,arg),A)
		cmp .right(.tcount(arg)-3,arg)
	      .elseif .match(.mid(0,1,arg),X)
		cpx .right(.tcount(arg)-3,arg)
	      .elseif .match(.mid(0,1,arg),Y)
		cpy .right(.tcount(arg)-3,arg)
	      .else
		.error "Syntax: if (A|X|Y) != expr"
	      .endif
	      beq _else
	   .else
		.error "Wrong if usage"
	    .endif
	.else
	.error "Wrong if usage"
	.endif
	.endmacro

With this following should work:
	if A
	if X
	if Y
or
	if A = 10
or
	if a = #10

But again, with the .tcount(arg) >= 3 clause it does not work ...

-- 
42Bastian Schick
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Wed May 11 07:33:57 2005

This archive was generated by hypermail 2.1.8 : 2005-05-11 07:33:59 CEST