I was playing with an assembly time SWITCH macro and got stuck. Assembly fails for ml, while JWasm assembles the code but runs the non-cases, too. Really tricky. Knowing that some here know a lot more about macros than I do, I post the testbed hoping to get some feedback...
I am not sure if the idea is feasible at all, but am curious to see what you think.
.nolist
include \masm32\include\masm32rt.inc
.listall
aSwitch MACRO arg ; assembly time switch
SwitchArg = arg
ctcase = 0
echo ------------- Switch -------------
ENDM
aCase MACRO carg
LOCAL SwON
if SwitchArg eq carg
SwON = 1
else
SwON = 0
endif
tmp$ CATSTR <** aCASE=>, <carg>, <, flagged >, %SwON, < with ct=>, %ctcase, < **>
% echo tmp$
if ctcase
tmp$ CATSTR <elseif >, %SwON
% echo tmp$
tmp$
echo BLAAAAAAAAAAAAAAA never seen
else
tmp$ CATSTR <if >, %SwON
% echo tmp$
tmp$
echo BLAAA #2, visible for ML but not JWasm
endif
ctcase = ctcase + 1
ENDM
aEndsw MACRO
tmp$ equ <endif>
echo endif ------------
tmp$ ; if commented, the code will assemble in ml but run both cases
ENDM
.code
start:
aSwitch 1
aCase 0
print "Case 0", 13, 10
aCase 1
print "Case 1", 13, 10
aEndsw
exit
end start
To illustrate the problem further, here the bottleneck:
include \masm32\include\masm32rt.inc
.code
start:
MyEndif equ endif
if 1
MsgBox 0, "Test", "Hi", MB_OK
MyEndif
exit
end start
This assembles fine, in ml and Jwasm. Replace the if 1 with if 0, and an error will be thrown by both assemblers: unmatched block nesting. In other words, the if 0 prevents the equate or macro from being used.
... and the solution:
include \masm32\include\masm32rt.inc
SwitchArg = 1 ; the option we choose
UseNothing = 0 ; option 0: do nothing
UseEdit = 1 ; option 1: create an edit control
UseListbox = 2 ; option 2: create a listbox
_Switch equ if 0
_Case equ elseif SwitchArg eq
_Endsw equ endif
.code
start:
%_Switch
%_Case UseEdit
print "Case Edit", 13, 10
%_Case UseListbox
print "Case Listbox", 13, 10
%_Case UseNothing
print "Case nothing", 13, 10
%_Endsw
exit
end start
Might look nice inside a WM_CREATE handler :bg
Works fine with ml 6.14, ml 6.15 and ml 9.0 but unfortunately not with JWasm.
Happy New MMX (http://www.masm32.com/board/index.php?topic=13012.msg100841#msg100841) Year,
Jochen