[cc65] Addenum: if/else/endif macros

From: 42Bastian Schick <bastian421monlynx.de>
Date: 2005-05-11 08:23:19
Hi,

.exitmacro is your friend:

	.macro if arg
	.scope
	_if arg,""
	.endmacro
	
	.macro _if arg,arg1
	.if .blank(arg)
	.error "Macro 'if' needs parameter(s) !"
	.exitmacro
	.endif
	.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) !"
	  .exitmacro
	  .endif
	  beq .ident(.concat("_else",arg1))
	  .exitmacro
         .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"
	        .exitmacro
	      .endif
	      bne .ident(.concat("_else",arg1))
	      .exitmacro
	   .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"
		.exitmacro
	      .endif
	      beq .ident(.concat("_else",arg1))
	      .exitmacro
	   .else
		.error "Wrong if usage"
		.exitmacro
	    .endif
	.else
	.error "Wrong if usage"
	.endif
	.endmacro


Note: As soon as .set works, _if can be expanded to support elseif :-)

Have fun,

-- 
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 08:23:28 2005

This archive was generated by hypermail 2.1.8 : 2005-05-11 08:23:31 CEST